spring-security(五)

spring-security(五)

摘自《pro spring security》基于spring security 4.2.2.RELEASE

ConfigAttribute

org.springframework.security.access.ConfigAttribute接口封装了可访问的受保护资源中的元数据(metadata),如ROLE_ADMIN就是一个ConfigAttribute,如一些ConfigAttribute的实现:

这里写图片描述

当方法上使用@Secured("ROLE_ADMIN")注解或者通过形如<security:intercept-url pattern="/hello" access="ROLE_SIMPSON_MEMBER" />拦截URL,那么spring security就开始工作了。当使用标签<security:intercept-url pattern="/x" access="ROLE_XX" />时,spring security使用FilterInvocationSecurityMetadataSourceParser去解析XML,在解析的过程中,调用private方法parseInterceptUrlsForFilterInvocationRequestMap,这个方法将标签中pattern对应的URL映射成了Ant风格的请求路径,如/*,这里的ROLE_XX就是ConfigAttribute,这个map初始化在FilterSecurityInterceptor内部的org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSourc实例中,当请求过来时,通过map中key去匹配请求的URL,找出对应的ConfigAttribute去校验这个请求认证对象(Authentication Object)的权限。解析流程:

这里写图片描述

如果要在方法上使用,则需要在security上下文中加上:

<global-method-security secured-annotations = "enabled"/>

猜你喜欢

转载自blog.csdn.net/u013887008/article/details/80919260