02. pigx微信小程序登录——用户名变token


02. pigx微信小程序登录——用户名变token

总体流程一览

参考文章:spring security oauth2 登录过程详解

在这篇文章里面主要提到了三个类:MobileAuthenticationFilterMobileAuthenticationProviderMobileLoginSuccessHandler

我对它的理解是这样的

类名 作用
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. 使用OAuth2RequestMobileAuthenticationToken构造出一个OAuth2Authentication
6. 使用OAuth2Authentication构造出OAuth2AccessToken

这里还要额外提到MobileSecurityConfigurer,这个类里面设置了MobileAuthenticationProviderMobileAuthenticationFilter。在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());

我表示看不懂,先记下来,以后再说


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM