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