看了半天的文档及源码,终于理出了spring-security的一些总体思路,spring security主要分认证(authentication)和授权(authority)。
1.认证authentication
认证主要代码在spring-security-core下的包org.springframework.security.authentication下,主类:AuthenticationManager、AuthenticationProvider
其关系如下:
2.授权Authorization
授权也称Access Control,主要代码在spring-security-core下的包org.springframework.security.access下,主类:AccessDecisionManager、SecurityMetadataSource。它们的关系通过ConfigAttribute关联起来。
SecurityMetadataSource获取ConfigAttribute,方法:
Collection<ConfigAttribute> getAttributes(Object object)throws IllegalArgumentException;
AccessDecisionManager根据进行授权,方法:
void decide(Authentication authentication, Object object,Collection<ConfigAttribute> configAttributes) throws AccessDeniedException,InsufficientAuthenticationException;
其实现类:AffirmativeBased的授权逻辑如下:
/** * This concrete implementation simply polls all configured * {@link AccessDecisionVoter}s and grants access if any * <code>AccessDecisionVoter</code> voted affirmatively. Denies access only if there * was a deny vote AND no affirmative votes. * <p> * If every <code>AccessDecisionVoter</code> abstained from voting, the decision will * be based on the {@link #isAllowIfAllAbstainDecisions()} property (defaults to * false). * </p> * * @param authentication the caller invoking the method * @param object the secured object * @param configAttributes the configuration attributes associated with the method * being invoked * * @throws AccessDeniedException if access is denied */ public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException { int deny = 0; for (AccessDecisionVoter voter : getDecisionVoters()) { int result = voter.vote(authentication, object, configAttributes); if (logger.isDebugEnabled()) { logger.debug("Voter: " + voter + ", returned: " + result); } switch (result) { case AccessDecisionVoter.ACCESS_GRANTED: return; case AccessDecisionVoter.ACCESS_DENIED: deny++; break; default: break; } } if (deny > 0) { throw new AccessDeniedException(messages.getMessage( "AbstractAccessDecisionManager.accessDenied", "Access is denied")); } // To get this far, every AccessDecisionVoter abstained checkAllowIfAllAbstainDecisions(); }
从上文可以看出,真正的授权是通过AccessDecisionVoter来完成的。
3.认证和授权的集成AbstractSecurityInterceptor
AbstractSecurityInterceptor包含了四个instance及其get/set方法
private AccessDecisionManager accessDecisionManager; private AfterInvocationManager afterInvocationManager; private AuthenticationManager authenticationManager = new NoOpAuthenticationManager(); private RunAsManager runAsManager = new NullRunAsManager();
加一个抽象的方法:
/** * Indicates the type of secure objects the subclass will be presenting to the * abstract parent for processing. This is used to ensure collaborators wired to the * {@code AbstractSecurityInterceptor} all support the indicated secure object class. * * @return the type of secure object the subclass provides services for */ public abstract Class<?> getSecureObjectClass();
AbstractSecurityInterceptor的实现类有两个:
3.1 FilterSecurityInterceptor
定义:
/** * Performs security handling of HTTP resources via a filter implementation. * <p> * The <code>SecurityMetadataSource</code> required by this security interceptor is of * type {@link FilterInvocationSecurityMetadataSource}. * <p> * Refer to {@link AbstractSecurityInterceptor} for details on the workflow. * </p> * * @author Ben Alex * @author Rob Winch */ public class FilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {}
实现了标准的servlet的Filter接口,其逻辑如下:
/** * Method that is actually called by the filter chain. Simply delegates to the * {@link #invoke(FilterInvocation)} method. * * @param request the servlet request * @param response the servlet response * @param chain the filter chain * * @throws IOException if the filter chain fails * @throws ServletException if the filter chain fails */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { FilterInvocation fi = new FilterInvocation(request, response, chain); invoke(fi); }
最重要的实现invoke
public void invoke(FilterInvocation fi) throws IOException, ServletException { if ((fi.getRequest() != null) && (fi.getRequest().getAttribute(FILTER_APPLIED) != null) && observeOncePerRequest) { // filter already applied to this request and user wants us to observe // once-per-request handling, so don‘t re-do security checking fi.getChain().doFilter(fi.getRequest(), fi.getResponse()); } else { // first time this request being called, so perform security checking if (fi.getRequest() != null) { fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE); } InterceptorStatusToken token = super.beforeInvocation(fi); try { fi.getChain().doFilter(fi.getRequest(), fi.getResponse()); } finally { super.finallyInvocation(token); } super.afterInvocation(token, null); } }
3.2 MethodSecurityInterceptor
定义:
/** * Provides security interception of AOP Alliance based method invocations. * <p> * The <code>SecurityMetadataSource</code> required by this security interceptor is of * type {@link MethodSecurityMetadataSource}. This is shared with the AspectJ based * security interceptor (<code>AspectJSecurityInterceptor</code>), since both work with * Java <code>Method</code>s. * <p> * Refer to {@link AbstractSecurityInterceptor} for details on the workflow. * * @author Ben Alex * @author Rob Winch */ public class MethodSecurityInterceptor extends AbstractSecurityInterceptor implements MethodInterceptor {}
其invoke方法如下:
/** * This method should be used to enforce security on a <code>MethodInvocation</code>. * * @param mi The method being invoked which requires a security decision * * @return The returned value from the method invocation (possibly modified by the * {@code AfterInvocationManager}). * * @throws Throwable if any error occurs */ public Object invoke(MethodInvocation mi) throws Throwable { InterceptorStatusToken token = super.beforeInvocation(mi); Object result; try { result = mi.proceed(); } finally { super.finallyInvocation(token); } return super.afterInvocation(token, result); }
参考文献:
【1】https://spring.io/guides/topicals/spring-security-architecture/
原文:http://www.cnblogs.com/davidwang456/p/6522925.html