2013-10-25 60 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

好的謝謝你的寶貴幫助:) –