™技术博客

框架 | 2.oauth2登录流程处理

2021年8月8日

登录逻辑


验证码 行为验证码AJ-Captcha

网关部分

1. 验证码生成逻辑(网关)


  • 模块 aiops-gateway
  • 接口 GET aiops-gateway/code
  • 处理类 ImageCodeCreateHandler

2. 登录参数(认证中心)


接口
/auth/oauth/token

请求体

  • username 用户名
  • password 密码(密文) AES加密;U2FsdGVkX19zqXDDWDmHtyPjTfv3nT3wNNYINJ8z54I=
  • code 验证码
  • randomStr 随机参数
  • grant_type:password
  • scope: server

请求头

  • TENANT-ID: 默认为1
  • Authorization: Basic信息 后台的base64(client_id:client_secret)
    YWlvcHM6YWlvcHM=

3. 验证码校验逻辑(网关)


  • 拦截器 ValidateCodeGatewayFilter
  • 如何处理验证码不校验的客户端 数据库表 sys_oauth_client_details 字段 additional_information 给值 {"captcha_flag":"0"}
  • 前端密码解密逻辑 PasswordDecoderFilter 密钥 gateway.encodeKey
  • 登录为何post body为空,只能url params形式;
  • *因为 webflux 获取post body很麻烦,且会出现丢包的现象**;

如何不想放url,就传明文

认证中心

1. 生成token


  • 入口: TokenEndpoint /oauth/token (spring-security.jar包下)
  • 校验clientId: ClientDetailsService 实现类 AiopsClientDetailsServiceImpl.loadClientByClientId 查库 sys_oauth_client_details 与传入校验对比
  • 授权:ResourceOwnerPasswordTokenGranter.getOAuth2Authentication
  • ProviderManager
  • DaoAuthenticationProvider
  • 校验用户:UserDetailsService 实现类 AiopsUserDetailsServiceImpl.loadUserByUsername方法
  • 调用 用户中心 remoteUserService 查数据库 sys_user 用户信息
  • 生成token AbstractTokenGranter.grant
  • DefaultTokenServices.createAccessToken
  • AiopsRedisTokenStore.getAccessToken

注意点 租户处理 如 两个不同租户下,有相同的用户 但密码不一样
即:服务之间如何传递租户信息

  • TenantContextHolderFilter 放入ttl中

资源服务器

1. 校验token

  • 入口拦截器 OAuth2AuthenticationProcessingFilter
  • OAuth2AuthenticationManager.authenticate
  • RemoteTokenServices.loadAuthentication 通过token换用户信息;调用认证中心的 /check_token 接口
  • check_token地址: CheckTokenEndpoint
    • DefaultTokenServices.readAccessToken
    • AiopsRedisTokenStore.readAccessToken
1
2
3
security:
resource:
token-info-uri: http://aiops-auth/oauth/check_token

扫描二维码,分享此文章