我有一个用于ovrride AuthenticateByEmailAddress(...)方法的Liferay-Hook。
我想要获得正在登录的用户的IP地址,并根据它限制访问。
如何通过这种方式获取用户IP?
我使用ServiceBuilder来制作portlets。
我在Tomcat工作。
发布于 2015-02-12 22:00:31
下面是我在Liferay 6.0.6中的工作原理。
在我的钩子中,我还更改了默认的login.jsp。这实际上是默认的login.jsp,还有两个额外的东西。首先,从request中获取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];发布于 2013-05-20 17:53:10
这只是一个想法,但是如果你想基于IP进行限制,而不是在你的应用程序中进行限制,你可以在你正在部署你的应用程序的web服务器中进行。
例如,在Tomcat中,您可以在web.xml中添加安全约束。
restrict access by ip这也可以在JBoss中完成,这真的取决于你使用的web服务器,但这似乎是最简单的解决方案,而不是在你的应用程序中进行过滤。
编辑:在下面的注释之后,只需在您要覆盖的方法中执行PortalUtil.getHttpServletRequest(request).getRemoteAddr();,并将您的业务逻辑放在那里,以查看用户是否更改了ip (将此IP与持久化的IP进行比较等)。Liferay已经有了一些这样的功能here
发布于 2013-05-22 15:41:51
我认为这些类是由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,您可以获取此信息并随心所欲地使用它。
我希望这能对你有所帮助。祝您的门户好运:)
https://stackoverflow.com/questions/16646715
复制相似问题