【问题标题】:does apache shiro restrict users based on ip address and country?apache shiro 会根据 IP 地址和国家/地区限制用户吗?
【发布时间】:2013-11-24 04:45:08
【问题描述】:

我想知道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() 方法对我有用吗?

【问题讨论】:

    标签: shiro


    【解决方案1】:

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

    对于国家/地区,您可以使用如下国家/地区查询服务扩展上述方法:http://freegeoip.net/

    【讨论】:

    • 我可以在我的自定义 jdbcRealm 中进行操作吗?如果是怎么办?
    • 你可以重写doGetAuthenticationInfo方法。该方法中的 AuthenticationToken 很可能是包含 getHost 方法的 UsernamePasswordToken。
    猜你喜欢
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多