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 刷一下,就省去让用户再输入帐号密码登录然后授权你的这一步了。
原文地址:https://www.cnblogs.com/liufei1983/p/8721551.html