`
guoyiqi
  • 浏览: 966402 次
社区版块
存档分类
最新评论

springsecurity的URL过滤和全局过滤

阅读更多

springsecurity的URL过滤和全局过滤

最近有个需求,需要配置一个登陆用户才能访问项目的资源,也就是说要配置一个/*形式的链接,保护项目中所有的资源,要登录了的用户才能访问,
当然是要除开登陆页面之外。

springsecurity对URL过滤是采用的正则表达式,其实一直就在使用springsecurity,只是最近才引起重视。
参考文章:
http://www.family168.com/oa/springsecurity/html/ch215-url-matcher.html

我采用了AntUrlPathMatcher简单路径匹配,也不去弄RegexUrlPathMatcher这个正则这个东东,有个简单路径匹配应该也够了。三个原则:
通配符:?   匹配任意一个字符
通配符:*   匹配任意多个字符,但不能跨越目录
通配符:** 可以匹配任意多个字符,可以跨越目录

我这里只需要这样使用就行了。
匹配上我不拦截的:
<intercept-url pattern="/common/**" filters="none"/>
<intercept-url pattern="/images/**" filters="none"/>
<intercept-url pattern="/css/**" filters="none"/>
<intercept-url pattern="/js/**" filters="none"/>
<intercept-url pattern="/index.jsp" filters="none"/>
<intercept-url pattern="/login.action*" filters="none"/>
<intercept-url pattern="/" filters="none"/>

数据库里面增加一条我要拦截的:
String localAntPath8 = "/**";
List<ConfigAttribute> configList8 = new LinkedList<ConfigAttribute>();
configList8.add(new SecurityConfig("ROLE_ALL_LOGON"));
ConfigAttributeDefinition cad8 = new ConfigAttributeDefinition(configList8);
RequestKey requestKey8 = new RequestKey(localAntPath8);

OK了,可以保护/**下所有的资源,不登陆就没有的玩了,当然,还要保证的就是所有登陆用户都有ROLE_ALL_LOGON这个权限。

另外这次配置还了解到不少东东。
1./j_spring_security_logout和/j_spring_security_check是不在这拦截之列的。
2.mini-web项目的日志的级别,请在文件logback.xml中修改,我也是增加了如下一行,才能看到springsecurity的详细日志:
<logger name="org.springframework.security">
<level value="DEBUG" />
</logger>
3.配置匿名访问用户可以这样配置(虽然这次没有用上)
<intercept-url pattern="/common/**" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/images/**" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/css/**" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/js/**" access="ROLE_ANONYMOUS" />
4.在这里类里面org.springside.examples.miniweb.service.security.RequestMapFactoryBean的LinkedHashMap是要顺序输出的
所以,在权限里面添加的顺序是按照先入先拦截的几率去拦截的。
我的测试类:
public static void main(String[] args) {
Map<String, String> map1 = new LinkedHashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
for (int i = 0; i < 10; i++) {
   map1.put(i + "", i + "");
}
for (int i = 0; i < 10; i++) {
   map2.put(i + "", i + "");
}
 
System.out.println(map1.toString());
System.out.println(map2.toString());
}
打印出来是:
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
{3=3, 5=5, 7=7, 2=2, 0=0, 9=9, 4=4, 8=8, 6=6, 1=1}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics