publicclassFilterSecurityInterceptorextendsAbstractSecurityInterceptorimplementsFilter{ publicvoiddoFilter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException, ServletException { //创建了一个FilterInvocation对象,这个FilterInvocation对象你可以当作它封装了request,它的主要工作就是拿请求里面的信息,比如请求的URI。 FilterInvocation fi = new FilterInvocation(request, response, chain); //调用了自身的invoke方法,并将FilterInvocation对象传入。 invoke(fi); }
publicvoidinvoke(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 && observeOncePerRequest) { fi.getRequest().setAttribute(FILTER_APPLIED, Boolean.TRUE); }
if (!getSecureObjectClass().isAssignableFrom(object.getClass())) { thrownew IllegalArgumentException( "Security invocation attempted for object " + object.getClass().getName() + " but AbstractSecurityInterceptor only configured to support secure objects of type: " + getSecureObjectClass()); }
publicvoiddecide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)throws AccessDeniedException { //拒绝数量 int deny = 0; //遍历从构造传入的决策投票器集合 for (AccessDecisionVoter voter : getDecisionVoters()) { //调用该投票器的投票逻辑方法进行判断(AffirmativeBased默认传入WebExpressionVoter) int result = voter.vote(authentication, object, configAttributes);
switch (result) { //赞成 case AccessDecisionVoter.ACCESS_GRANTED: return; //拒绝 case AccessDecisionVoter.ACCESS_DENIED: deny++; break; default: break; } }
//拒绝数量大于0,抛出AccessDeniedException异常 if (deny > 0) { thrownew AccessDeniedException(messages.getMessage( "AbstractAccessDecisionManager.accessDenied", "Access is denied")); }
//如果全部都弃权,进入此方法,判断此决策器是否允许全部弃权,不允许则抛出AccessDeniedException异常 // To get this far, every AccessDecisionVoter abstained checkAllowIfAllAbstainDecisions(); } }