2013-10-25 407 views
2

我想知道apache shiro是否提供这样的服务来根据用户IP地址和用户国进行认证?做apache shiro限制用户基于ip地址和国家?


是,@Wouter感谢信息,但我有尝试过也重写此方法,但我读http://www.mkyong.com/java/how-to-get-client-ip-address-in-java/文章,以避免代理的IP地址。

Shiro API gethost()方法医生说

"Returns the host name of the client from where the authentication attempt originates or if the Shiro environment cannot or chooses not to resolve the hostname to improve performance, this method returns the String representation of the client's IP address. 
When used in web environments, this value is usually the same as the ServletRequest.getRemoteHost() value." 

那么有没有什么方法可以检查客户端是否是代理服务器旁边里面境界doGetAuthenticationInfo()方法类似

//is client behind something? 
    String ipAddress = request.getHeader("X-FORWARDED-FOR"); 
    if (ipAddress == null) { 
     ipAddress = request.getRemoteAddr(); 
    } 

...或者这个getHost()方法将做的工作为了我?

回答

1

您可以创建自己的AuthenticatingFilter版本(扩展它)并检查IP地址。如果您使用标准的用户名/密码认证,您将获得一个包含主机(IP地址)的UsernamePasswordToken实例。您可以使用它来创建您自己的自定义身份验证逻辑。

+0

我可以做我的自定义jdbcRealm内:

至于国家,你可以用这样的国家查找服务扩展上面的方法?如果是的话该怎么做? –

+1

您可以覆盖doGetAuthenticationInfo方法。该方法中的AuthenticationToken很可能是一个包含getHost方法的UsernamePasswordToken。 – Wouter

+0

好的谢谢你的宝贵帮助:) –