server
1、访问过滤器
code模式访问:http://localhost:8000/oauth/authorize?response_type=code&client_id=test_client&scope=read&state=test&redirect_uri=http://baidu.com
AnonymousAuthenticationFilter拦截进入登陆页面
if (SecurityContextHolder.getContext().getAuthentication() == null) {
SecurityContextHolder.getContext().setAuthentication(
createAuthentication((HttpServletRequest) req));
}
2、AbstractSecurityInterceptor spring SPEL比对类方法信息,属性比较
this.accessDecisionManager.decide(authenticated, object, attributes);
( spel初始化见:ExpressionBasedFilterInvocationSecurityMetadataSource WebExpressionConfigAttribute)
代码例子
@Test
public void expressionParserTest() {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("permitAll()");
//使用自定义容器
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setRootObject(new SecurityExpressionRoot());
System.out.println(exp.getValue(ctx));
}
class SecurityExpressionRoot {
public final boolean permitAll() {
return true;
}
public final boolean denyAll() {
return false;
}
}
3、认证
AbstractUserDetailsAuthenticationProvider
用户信息校验
Oauth2.0
AuthorizationEndpoint
4、auth session认证管理
SecurityContextPersistenceFilter
key = SPRING_SECURITY_CONTEXT
client
1、初始化启动客户端服务
ResourceServerProperties implements InitializingBean
RestTemplate Get http://localhost:8888/oauth/token_key. base64(clientID+clientSecret)
2、服务端解析token_key参数
BasicAuthenticationFilter Base64.getDecoder().decode(base64Token)
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(tokens[0], tokens[1]);
认证用户名密码
Authentication authResult = this.authenticationManager.authenticate(authRequest);
认证通过访问TokenKeyEndpoint
返回jwt signingKey
3、客户端生成JwtAccessTokenConverter
4、第一次访问客户端资源
http://localhost:8090/crm/system/profile
5、客户端拦截
(
备注:
重定向策略代码DefaultRedirectStrategy sendRedirect
路径匹配:AntPathRequestMatcher
accessToken存储容器:DefaultOAuth2ClientContext
)
AnonymousAuthenticationFilter设备匿名认证信息
AbstractSecurityInterceptor拦截进入客户端登陆URL http://localhost:8090/crm/login
OAuth2ClientAuthenticationProcessingFilter
accessToken = restTemplate.getAccessToken();
OAuth2RestTemplate从DefaultOAuth2ClientContext获取accessToken
如果accessToken null,获取是否请求信息accessTokenRequest,null抛异常AccessTokenRequiredException重定向,否则AuthorizationCodeAccessTokenProvider获取accessToken = accessTokenProvider.obtainAccessToken(resource, accessTokenRequest);
客户端重定向认证URL
http://localhost:8888/oauth/authorize?client_id=crm&redirect_uri=http://localhost:8090/crm/login&response_type=code&state=aRXrJ9
服务端重定向登陆URL
http://localhost:8888/login
输入用户名密码
服务端重定向认证URL
http://localhost:8888/oauth/authorize?client_id=crm&redirect_uri=http://localhost:8090/crm/login&response_type=code&state=64Fy61
服务端重定向资源服务
http://localhost:8090/crm/login?code=T49ae2&state=64Fy61
后续客户端获取token
sesseion
SessionConfig getSessionCookieName
过滤器
AbstractAuthenticationProcessingFilter
if (!requiresAuthentication(request, response)) {
chain.doFilter(request, response);
return;
}