簡體   English   中英

Liferay Hook-獲取用戶IP登錄

[英]Liferay Hook - Get the User IP logging in

我有一個Liferay-Hook可以ovrride AuthenticateByEmailAddress(...)方法。

我想獲取正在登錄的用戶的IP地址,並基於它來限制訪問。

如何通過這種方法獲取用戶IP?
我使用ServiceBuilder制作portlet。
我在Tomcat上工作。

這是在Liferay 6.0.6中對我有用的東西。

我還改變了默認的login.jsp。 這實際上是默認的login.jsp,其中還有兩點。 首先,從請求中獲取IP:

<%String ip = PortalUtil.getHttpServletRequest(renderRequest).getRemoteAddr();%>

並添加額外的參數:

<portlet:actionURL secure="<%= PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS || request.isSecure() %>" var="loginURL">
            <portlet:param name="saveLastPath" value="0" />
            <portlet:param name="struts_action" value="/login/login" />
            <portlet:param name="requestIp" value="<%= ip %>" />
        </portlet:actionURL>

現在,在您覆蓋的AuthenticateByEmailAddress()中,您可以從parameterMap中獲取它:

String ip = parameterMap.get("requestIp")[0];

只是想一想,但是如果您想基於IP進行限制,而不是在應用程序中進行限制,則可以在正在部署應用程序的Web服務器中進行限制。

例如,在Tomcat中,您可以將安全性約束放在web.xml中。

通過ip限制訪問也可以在JBoss中完成,這實際上取決於您使用的Web服務器,但這似乎是最簡單的解決方案,而不是在應用程序中進行過濾。

編輯:在下面的評論之后,只需執行PortalUtil.getHttpServletRequest(request).getRemoteAddr(); 在您要覆蓋的方法中,將您的業務邏輯放在此處,以查看用戶是否更改了ip(將此IP與保留的IP進行比較,等等)。 Liferay 在這里已經具有一些此功能

我想這些類是由Service Builder制作的。 然后,您可以簡單地使用:

String user = request.getRemoteUser();//take current logged in user ID
int userId = Integer.valueOf(user);//convert it into integer
User newUser = UserLocalServiceUtil.getUser(userId);//get the Liferay user using the user ID you get in the line above
userIp=newUser.getLoginIP();//after you have the current user take his LoginIP with the get method that Liferay has already

在Liferay數據庫中,您有一個名為user_表,其中Liferay保留有關其用戶的所有信息。 使用UserLocalServiceUtil您可以獲取此信息並根據需要使用它。

我希望這會有所幫助。 祝您的門戶網站好運:)

在Liferay Portal中,我通過以下方式獲得了正確的客戶端IP地址。

FacesContext facesContext = FacesContext.getCurrentInstance();
PortletRequest portletRequest = (PortletRequest) facesContext.getExternalContext().getRequest();
HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
HttpServletRequest originalServletRequest = PortalUtil.getOriginalServletRequest(request);
String ipAddress=originalServletRequest.getHeader("x-forwarded-for");

由於只有一個答案有效,而且我對該解決方案並不滿意,因此我做了一些深入的研究。

這是我想出的:

創建一個新的過濾器並將其注冊到您的liferay中。 在那里,您將請求存儲在本地線程中,並在請求結束時將其刪除。

之后,您可以僅從過濾器獲取請求。

RequestFilter.java

@WebFilter(urlPatterns = "/*")
public class RequestFilter implements Filter {

    private static final ThreadLocal<HttpServletRequest> REQUEST = new ThreadLocal<>();

    public static HttpServletRequest getRequest() {
        HttpServletRequest req = REQUEST.get();
        if (req == null) {
            throw new IllegalStateException("Attempt to fetch thread-local request outside of filter chain");
        }
        return req;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        REQUEST.set((HttpServletRequest)req);
        try {
            chain.doFilter(req, res);
        } finally {
            REQUEST.remove();
        }
    }

    @Override
    public void destroy() {
    }
}

MyAuthFailure.java

public class MyAuthFailure implements AuthFailure {
    private static final Log LOG = LogFactoryUtil.getLog(MyAuthFailure.class);

    @Override
    public void onFailureByEmailAddress(long companyId, String emailAddress, 
            Map<String, String[]> headers, Map<String, String[]> params) throws AuthException {
        logFailure(companyId, emailAddress);
    }

    @Override
    public void onFailureByScreenName(long companyId, String screenName, 
            Map<String, String[]> headers, Map<String, String[]> params) throws AuthException {
        logFailure(companyId, screenName);
    }

    @Override
    public void onFailureByUserId(long companyId, long userId, 
            Map<String, String[]> headers, Map<String, String[]> params) throws AuthException {
        logFailure(companyId, userId);
    }

    protected void logFailure(String user) {
        String ip = RequestFilter.getRequest().getRemoteAddr();
        LOG.error("User " + user + " tried to login from IP " + ip);
    }
}

liferay-hook.xml

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">

<hook>
    <portal-properties>portal.properties</portal-properties>
    <custom-jsp-dir>/WEB-INF/jsp</custom-jsp-dir>
    <servlet-filter>
        <servlet-filter-name>requestFilter</servlet-filter-name>
        <servlet-filter-impl>com.stackoverflow.RequestFilter</servlet-filter-impl>
    </servlet-filter>
</hook>

portal.properties

auth.failure=com.stackoverflow.MyAuthFailure

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM