// Attempt to run as a different user AuthenticationrunAs=this.runAsManager.buildRunAs(authenticated, object, attributes); if (runAs != null) { SecurityContextorigCtx= SecurityContextHolder.getContext(); SecurityContextnewCtx= SecurityContextHolder.createEmptyContext(); newCtx.setAuthentication(runAs); SecurityContextHolder.setContext(newCtx);
// need to revert to token.Authenticated post-invocation returnnewInterceptorStatusToken(origCtx, true, attributes, object); } this.logger.trace("Did not switch RunAs authentication since RunAsManager returned null"); // no further work post-invocation returnnewInterceptorStatusToken(SecurityContextHolder.getContext(), false, attributes, object);
publicvoiddecide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException { intdeny=0; for (AccessDecisionVoter voter : getDecisionVoters()) { //voter:WebExpressionVoter //进行投票 intresult= voter.vote(authentication, object, configAttributes); switch (result) { case AccessDecisionVoter.ACCESS_GRANTED: return; case AccessDecisionVoter.ACCESS_DENIED: deny++; break; default: break; } } if (deny > 0) { thrownewAccessDeniedException( this.messages.getMessage("AbstractAccessDecisionManager.accessDenied", "Access is denied")); } // To get this far, every AccessDecisionVoter abstained checkAllowIfAllAbstainDecisions(); }
进入vote
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicintvote(Authentication authentication, FilterInvocation filterInvocation, Collection<ConfigAttribute> attributes) { Assert.notNull(authentication, "authentication must not be null"); Assert.notNull(filterInvocation, "filterInvocation must not be null"); Assert.notNull(attributes, "attributes must not be null"); WebExpressionConfigAttributewebExpressionConfigAttribute=this.findConfigAttribute(attributes); if (webExpressionConfigAttribute == null) { this.logger.trace("Abstained since did not find a config attribute of instance WebExpressionConfigAttribute"); return0; } else { EvaluationContextctx= webExpressionConfigAttribute.postProcess(this.expressionHandler.createEvaluationContext(authentication, filterInvocation), filterInvocation); booleangranted= ExpressionUtils.evaluateAsBoolean(webExpressionConfigAttribute.getAuthorizeExpression(), ctx); if (granted) { return1; } else { this.logger.trace("Voted to deny authorization"); return -1; } } }