spring boot 學習三:OAuth2 認證


1:  代碼地址: https://github.com/liufeiSAP/uaa-zuul

2:     安裝: postgres

  下載 https://www.openscg.com/bigsql/postgresql/installers.jsp/

        具體安裝過程參考: https://www.cnblogs.com/winkey4986/p/5360551.html

3:  

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
@EnableOAuth2Sso //這個注解會幫我們完成跳轉到授權服務器,當然要些配置application.yml
@Configuration
@EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter{}

ZuulFallbackProvider : // 是當我們的zuul進行路由分發時,如果后端服務沒有啟動,或者調用超時,這時候我們希望Zuul提供一種降級功能,而不是將異常暴露出來。
// 這個接口就是做這個的, 實現public ClientHttpResponse fallbackResponse() 就可以


,快過期了用 refresh token 刷一下,就省去讓用戶再輸入帳號密碼登錄然后授權你的這一步了。
spring:
  profiles:
    active: ${SPRING_PROFILES_ACTIVE:dev}
  application:
      name: api-gateway
#  cloud:
#    config:
#      uri: http://${config.host:192.168.1.140}:${config.port:8888}

server:
  port: 8080


eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

zuul: 
  routes:                            // 定義路由
    uaa:
      path: /uaa/**                    
      sensitiveHeaders:
      serviceId: auth-server          // 服務名稱,注冊到注冊中心的服務名
    order:
      path: /order/**
      sensitiveHeaders:
      serviceId: order-service
  add-proxy-headers: true

security:
  oauth2:
    client:
      access-token-uri: http://localhost:8080/uaa/oauth/token
      user-authorization-uri: http://localhost:8080/uaa/oauth/authorize
      client-id: webapp
    resource:
      user-info-uri: http://localhost:8080/uaa/user
      prefer-token-info: false

 

  • ClientDetailsServiceConfigurer:用來配置客戶端詳情服務(ClientDetailsService),客戶端詳情信息在這里進行初始化,你能夠把客戶端詳情信息寫死在這里或者是通過數據庫來存儲調取詳情信息。(就是把client信息保存在memory, jdbc等)
  • UserDetailsService驗證用戶名、密碼和授權: 這個是驗證用戶明和密碼等;
  • ClientDetailsService: 這個驗證的是client, 比如android, webapp, tsk等。
 
        
 An OAuth 2 authentication token can contain two authentications: one for the client and one for the user. Since some
* OAuth authorization grants don't require user authentication, the user authentication may be null.
*
* @author Ryan Heaton
*/
public class OAuth2Authentication extends AbstractAuthenticationToken {

   

網上關於OAUTH2的都是講一本原理,大部分沒講oauth2的token到底如何校驗的,這個spring 項目中用redis存儲的token,

RedisTokenStore.java的源碼看了一下,就知道是如何根據token得到所有相關信息了。

     eids中有如下keys:

1) "auth:9c380ddb-9cfc-4035-81e3-4bb6e0280084"                       //  principal信息(用戶名); 
2) "refresh_to_access:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
3) "uname_to_access:android:admin"
4) "refresh_auth:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
5) "auth_to_access:12d0c29da8b9ebf45ab14ec4b48a8a1e"
6) "access_to_refresh:9c380ddb-9cfc-4035-81e3-4bb6e0280084"
7) "client_id_to_access:android"
8) "refresh:73fcc7fc-4349-4b90-bc33-7d1533c24f96"
9) "access:9c380ddb-9cfc-4035-81e3-4bb6e0280084"                 //  這個存儲的是token相關信息(何時過期, 類型 , refresh_token,  scope, )

 

 

DefaultTokenServices.java  的createAccessToken()的代碼展示了,在確認了用戶信息后生成token的邏輯。
看源碼分析出來: token就是一個UUID,不帶任何信息,reids存儲的時候把token做成可以,把Authentication做成value)



Refresh_token: 快過期了用 refresh token 刷一下,就省去讓用戶再輸入帳號密碼登錄然后授權你的這一步了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM