02. pigx微信小程序登录——用户名变token
总体流程一览
参考文章:spring security oauth2 登录过程详解
在这篇文章里面主要提到了三个类:MobileAuthenticationFilter
、MobileAuthenticationProvider
、MobileLoginSuccessHandler
我对它的理解是这样的
类名 | 作用 |
---|---|
MobileAuthenticationFilter |
1. 拦截匹配的URL,使其进入attemptAuthentication 方法2. attemptAuthentication 方法从HttpServletRequest 中获取登录参数,构造出一个MobileAuthenticationToken 3. 将这个token交给 this.getAuthenticationManager().authenticate() 处理4. authenticate() 方法会返回带用户认证信息的MobileAuthenticationToken |
MobileAuthenticationProvider |
1. 接收authenticate() 方法传递过来的MobileAuthenticationToken 2. 从 MobileAuthenticationToken 里面取出账号信息,比方说pigx微信小程序登录的时候用的就是MINI@xxxxxxxx 3. 使用这个账号从 sys_user 用户表里面获取出用户详情4. 使用用户详情和用户授权信息,构造出一个 MobileAuthenticationToken ,并给它的details里面设置先前传过来的token的详情。这里为什么要token换token呢?我认为第一个token是没有授权信息的,第二个token才有授权信息5. 返回token给filter |
MobileLoginSuccessHandler |
1. 经过MobileAuthenticationFilter 之后,springsecurity发现认证成功了,所以会进入到该方法中。为什么是这个方法呢?因为MobileSecurityConfigurer 是这样配置的2. 从header中取出Authorization信息,然后解码得到 pigx:pigx 3. 检验client_secret是否正确 4. 使用空Map、clientId、scope、grantType,构造出一个TokenRequest 4. 校验scope是否正确,正确的话使用client详情构造出一个 OAuth2Request 5. 使用 OAuth2Request 和MobileAuthenticationToken 构造出一个OAuth2Authentication 6. 使用 OAuth2Authentication 构造出OAuth2AccessToken |
这里还要额外提到MobileSecurityConfigurer
,这个类里面设置了MobileAuthenticationProvider
和MobileAuthenticationFilter
。在MobileAuthenticationProvider
类里面设置了用户详情服务;在MobileAuthenticationFilter
里面设置了认证管理器、登录成功的句柄、异常处理。
MobileSecurityConfigurer
自己是在pigx-auth模块的WebSecurityConfigurer
进行配置的。WebSecurityConfigurer
里面有这样一段配置
http.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form")
.successHandler(tenantSavedRequestAwareAuthenticationSuccessHandler())
.failureHandler(authenticationFailureHandler()).and().logout()
.logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true)
.and().authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll()
.anyRequest().authenticated().and().csrf().disable().apply(mobileSecurityConfigurer());
我表示看不懂,先记下来,以后再说