【问题标题】:Whitelist user IPs in ShiroShiro 中的白名单用户 IP
【发布时间】:2015-06-06 08:06:35
【问题描述】:
我想启用 Facebook 来抓取我的网站,但它需要用户身份验证。 Facebook 表示,解决这个问题的一种方法是将他们的 ip 列入白名单。我正在使用 Apache Shiro,我知道您可以通过从 BasicHttpAuthenticationFilter 调用 getHost 来获取客户端的 ip,但是我不知道如何让某些 ip 地址通过身份验证。
【问题讨论】:
标签:
apache
facebook-graph-api
authentication
shiro
【解决方案1】:
您可能需要构建 Shrio 的自定义实现
org.apache.shiro.web.filter.authc.AuthenticatingFilter
至少,如果请求来自列入白名单的 IP 地址,您必须通过扩展 BasicHttpAuthenticationFilter 并添加逻辑以跳过 BasicHttpAuthenticationFilter 来自定义 BasicHttpAuthenticationFilter。
package com.acme.web.filter.authc;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class WhitelistedBasicHttpAuthenticationFilter extends BasicHttpAuthenticationFilter {
private Set<String> whitelist = Collections.emptySet();
public void setWhitelist(String list) {
whitelist = new HashSet<String>();
Collections.addAll(whitelist, list.split(",")); //make sure there are no spaces in the string!!!!
}
@Override
protected boolean isEnabled (ServletRequest request, ServletResponse response) throws ServletException, IOException
{
if (whitelist.contains(request.getRemoteAddr())) {
return false;
}
return super.isEnabled(request, response);
}
}
在你的“shiro.ini”中
authc=com.acme.web.filter.authc.WhitelistedBasicHttpAuthenticationFilter
authc.whitelist=192.168.1.1,192.168.1.2,192.168.2.3