zoukankan      html  css  js  c++  java
  • JHipster技术栈理解

    本文简要分析了UAA的认证机制和部分源码功能。
    UAA全称User Account and Authentication。
    相关源码都是通过Jhipster生成,包括UAA,Gateway,Identity。Jhipster简介请参考这里

    1 OAuth2认证模式

    1.1 密码模式

    密码模式(Resource Owner Password Credentials)中,用户向客户端提供自己的用户名和密码。客户端使用这些信息,向"认证服务器"进行认证。在这种模式中,用户必须把自己的密码给客户端,但是客户端不得储存密码。
    流程如下:
    a, 用户向客户端提供用户名和密码。
    b, 客户端将用户名和密码发给认证服务器,向后者请求令牌。
    c, 认证服务器确认无误后,向客户端提供访问令牌。
    d, 客户端之后所有访问都会传递令牌。

    1.2 客户端模式

    客户端模式(Client Credentials)指客户端以服务自身的名义,而不是以用户的名义,向"认证服务器"进行认证。
    流程如下:
    a, 客户端从配置文件或者数据库获取认证信息。
    b, 客户端将认证信息发给认证服务器,并请求返回一个访问令牌。
    c, 认证服务器确认认证信息无误后,向客户端提供访问令牌。
    d, 客户端之后的所有访问不会传递这个令牌。

    2 UAA认证方式

    2.1 用户调用

    oauth2认证模式: 密码模式

    配置文件相关内容

    oauth2:     
      web-client-configuration:        
        #change client secret in production, keep in sync with UAA configuration        
        client-id: web_app        
        secret: changeit
    

    时序图

    说明:这种认证方式是用在用户访问的场景,也就是服务间调用时总是带着用户名和密码信息。

    2.2 机器调用

    oauth2认证模式: 密码模式

    配置文件相关内容

    jhipster:    
      security:        
        client-authorization:            
          client-id: internal            
          client-secret: internal
    

    时序图

    说明:这种认证方式是用在内部服务之间调用的场景,也就是服务间调用时是没有用户名和密码信息的。JHipster生成的UAA是没有这部分的代码的,需要自己实现,参见JHipster技术栈定制 - 基于UAA的微服务之间安全调用

    3 源码分析

    3.1 UAA

    com.yourcompany.uaa.config.UaaConfiguration

    注册为认证服务器,注册用户调用客户端(web_app)和机器调用客户端(internal)。目前都是写死的,如果需要保存所有客户端到数据库,需要修改方法configure(ClientDetailsServiceConfigurer clients)。

    com.yourcompany.uaa.config.UaaProperties

    uaa相关配置属性和值。

    com.yourcompany.uaa.config.UaaWebSecurityConfiguration

    配置不需要认证的url。

    com.yourcompany.uaa.security.DomainUserDetailsService

    查询数据库返回包含完整用户信息的对象。

    com.yourcompany.uaa.security.IatTokenEnhancer

    添加iat到token中,即token创建时间。

    com.yourcompany.uaa.security.SecurityUtils

    spring security 工具类,获取当前线程用户的登录名,判断当前登录用户是否认证过,判断当前用户是否具有指定的权限。

    com.yourcompany.uaa.security.SpringSecurityAuditorAware

    获取当前线程用户的登录名,调用SecurityUtils的方法。

    com.yourcompany.uaa.security.TokenProvider

    创建token工具类。

    org.springframework.security.core.userdetails.User

    内置用户类,保存用户名,密码,账号是否过期,账号是否锁定,账号凭证是否过期,是否可用。

    org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

    内置过滤器,调用AuthenticationManager校验form表单提交的用户名密码。

    org.springframework.security.oauth2.provider.endpoint.TokenEndpoint

    内置端点,接受客户端请求,认证后返回token。

    org.springframework.security.oauth2.provider.endpoint.TokenKeyEndpoint

    内置端点,接受客户端请求,返回验证公钥。

    3.2 Gateway

    com.yourcompany.gateway.web.filter.RefreshTokenFilter

    过滤器,过滤传入的请求并刷新到期之前的访问令牌。

    com.yourcompany.gateway.web.filter.RefreshTokenFilterConfigurer

    配置类,配置refreshtokenfilter到工程里。

    com.yourcompany.gateway.config.MicroserviceSecurityConfiguration

    注册为资源服务器,保护/api/和/management/等资源。

    com.yourcompany.gateway.config.oauth2.OAuth2AuthenticationConfiguration

    注册为oauth2的资源服务器,保护/auth/logout, 注册RefreshTokenFilterConfigurer到应用过滤链中。

    com.yourcompany.gateway.config.oauth2.OAuth2JwtAccessTokenConverter

    access token解码器,将token解码为身份对象;获取uaa公钥并配置为密钥签名保存在内存中。

    com.yourcompany.gateway.config.oauth2.OAuth2Properties

    保存配置文件中oauth2部分的属性。

    com.yourcompany.gateway.security.AuthoritiesConstants

    对应uaa数据表jhi_authority。

    com.yourcompany.gateway.security.SecurityUtils

    spring security 工具类,获取当前登录用户的登录名,判断当前登录用户是否认证过,判断当前用户是否具有指定的权限。

    com.yourcompany.gateway.security.oauth2.CookieTokenExtractor

    从cookie中解析出access token,使用了OAuth2CookeiHelper。

    com.yourcompany.gateway.security.oauth2.OAuth2CookieHelper

    cookie帮助类。 getClaim()方法可以从token中获取明文信息。

    com.yourcompany.gateway.security.oauth2.OAuth2AuthenticationService

    管理OAuth2的身份验证情况,保存(更新)access token和refresh token的Cookie。

    com.yourcompany.gateway.security.oauth2.OAuth2Cookies

    保存access token和refresh token。

    com.yourcompany.gateway.security.oauth2.CookiesHttpServletRequestWrapper

    请求映射器,用于修改原始请求中的cookie。

    com.yourcompany.gateway.security.oauth2.CookieCollection

    允许修改的Cookie集合,与单纯的数组不同。

    com.yourcompany.gateway.security.oauth2.OAuth2SignatureVerifierClient

    接口, 定义方法getSignatureVerifier(), 表示创建一个SignatureVerifier,用于获取公钥并验证JWT令牌。

    com.yourcompany.gateway.security.oauth2.UaaSignatureVerifierClient

    客户端从UAA获取公钥并返回SignatureVerifier。实现了OAuth2SignatureVerifierClient接口的getSignatureVerifier()方法。

    com.yourcompany.gateway.security.oauth2.OAuth2TokenEndpointClient

    接口, 作为客户端与OAuth2授权服务器的令牌终端通信。2个重要方法: sendPasswordGrant()和sendRefreshGrant()。

    com.yourcompany.gateway.security.oauth2.OAuth2TokenEndpointClientAdapter

    抽象类, 实现了OAuth2TokenEndpointClient的2个重要方法。定义了一个给请求头中添加身份信息的抽象方法addAuthentication()。

    com.yourcompany.gateway.security.oauth2.UaaTokenEndpointClient

    继承OAuth2TokenEndpointClientAdapter,实现OAuth2TokenEndpointClient。 作为客户端与UAA服务器的令牌终端通信,实现了addAuthentication()方法,从配置文件中获取如下配置,并放到请求头中:

    oauth2:
      web-client-configuration:
        client-id: web_app
        secret: changeit
    

    注意:

    • 如果用户登录没有勾选“记住我”,cookie里面的刷新令牌的key为: cookie_token;如果勾选了“记住我”,cookie里面的刷新令牌的key为: refresh_token
    • 如果要严格判断登出时间,需要通过缓存中间件保存logout登出信息。

    3.3 Identity

    com.yourcompany.identity.client.AuthorizedFeignClient

    注解。 为机器调用添加认证信息。默认注册拦截器OAuth2FeignRequestInterceptor。

    com.yourcompany.identity.client.AuthorizedUserFeignClient

    注解。为用户调用添加认证信息。默认注册拦截器UserFeignClientInterceptor

    com.yourcompany.identity.client.OAuth2InterceptedFeignConfiguration

    注册Oauth2RequestInterceptor。

    com.yourcompany.identity.client.OAuth2UserClientFeignConfiguration

    注册UserFeignClientInterceptor。

    com.yourcompany.identity.client.UserFeignClientInterceptor

    拦截器, 给resttemplate的请求头中添加认证信息。

    com.yourcompany.identity.config.MicroserviceSecurityConfiguration

    注册为资源服务器,保护/api/和/management/等资源。

    com.yourcompany.identity.security.oauth2.OAuth2SignatureVerifierClient

    接口, 定义方法getSignatureVerifier(), 表示创建一个SignatureVerifier,用于获取公钥并验证JWT令牌。

    com.yourcompany.identity.security.oauth2.UaaSignatureVerifierClient

    客户端从UAA获取公钥并返回SignatureVerifier。实现了OAuth2SignatureVerifierClient接口的getSignatureVerifier()方法。

  • 相关阅读:
    A1066 Root of AVL Tree (25 分)
    A1099 Build A Binary Search Tree (30 分)
    A1043 Is It a Binary Search Tree (25 分) ——PA, 24/25, 先记录思路
    A1079; A1090; A1004:一般树遍历
    A1053 Path of Equal Weight (30 分)
    A1086 Tree Traversals Again (25 分)
    A1020 Tree Traversals (25 分)
    A1091 Acute Stroke (30 分)
    A1103 Integer Factorization (30 分)
    A1032 Sharing (25 分)
  • 原文地址:https://www.cnblogs.com/yorkwu/p/9572151.html
Copyright © 2011-2022 走看看