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());
我表示看不懂,先記下來,以后再說