AuthServerConfig.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.ruoyi.auth.config;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import javax.sql.DataSource;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.http.HttpMethod;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
  13. import org.springframework.security.oauth2.common.OAuth2AccessToken;
  14. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  15. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  16. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  17. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  18. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  19. import org.springframework.security.oauth2.provider.OAuth2Authentication;
  20. import org.springframework.security.oauth2.provider.token.TokenEnhancer;
  21. import org.springframework.security.oauth2.provider.token.TokenStore;
  22. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  23. import com.ruoyi.auth.exception.CustomWebResponseExceptionTranslator;
  24. import com.ruoyi.common.core.constant.CacheConstants;
  25. import com.ruoyi.common.core.constant.SecurityConstants;
  26. import com.ruoyi.common.security.domain.LoginUser;
  27. import com.ruoyi.common.security.service.RedisClientDetailsService;
  28. /**
  29. * OAuth2 认证服务配置
  30. *
  31. * @author ruoyi
  32. */
  33. @Configuration
  34. @EnableAuthorizationServer
  35. public class AuthServerConfig extends AuthorizationServerConfigurerAdapter
  36. {
  37. @Autowired
  38. private AuthenticationManager authenticationManager;
  39. @Autowired
  40. private DataSource dataSource;
  41. @Autowired
  42. private RedisConnectionFactory redisConnectionFactory;
  43. @Autowired
  44. private UserDetailsService userDetailsService;
  45. @Autowired
  46. private TokenEnhancer tokenEnhancer;
  47. /**
  48. * 定义授权和令牌端点以及令牌服务
  49. */
  50. @Override
  51. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  52. {
  53. endpoints
  54. // 请求方式
  55. .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
  56. // 指定token存储位置
  57. .tokenStore(tokenStore())
  58. // 自定义生成令牌
  59. .tokenEnhancer(tokenEnhancer)
  60. // 用户账号密码认证
  61. .userDetailsService(userDetailsService)
  62. // 指定认证管理器
  63. .authenticationManager(authenticationManager)
  64. // 是否重复使用 refresh_token
  65. .reuseRefreshTokens(false)
  66. // 自定义异常处理
  67. .exceptionTranslator(new CustomWebResponseExceptionTranslator());
  68. }
  69. /**
  70. * 配置令牌端点(Token Endpoint)的安全约束
  71. */
  72. @Override
  73. public void configure(AuthorizationServerSecurityConfigurer oauthServer)
  74. {
  75. oauthServer.allowFormAuthenticationForClients().checkTokenAccess("permitAll()");
  76. }
  77. /**
  78. * 声明 ClientDetails实现
  79. */
  80. public RedisClientDetailsService clientDetailsService()
  81. {
  82. RedisClientDetailsService clientDetailsService = new RedisClientDetailsService(dataSource);
  83. return clientDetailsService;
  84. }
  85. /**
  86. * 配置客户端详情
  87. */
  88. @Override
  89. public void configure(ClientDetailsServiceConfigurer clients) throws Exception
  90. {
  91. clients.withClientDetails(clientDetailsService());
  92. }
  93. /**
  94. * 基于 Redis 实现,令牌保存到缓存
  95. */
  96. @Bean
  97. public TokenStore tokenStore()
  98. {
  99. RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
  100. tokenStore.setPrefix(CacheConstants.OAUTH_ACCESS);
  101. return tokenStore;
  102. }
  103. /**
  104. * 自定义生成令牌
  105. */
  106. @Bean
  107. public TokenEnhancer tokenEnhancer()
  108. {
  109. return new TokenEnhancer()
  110. {
  111. @Override
  112. public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication)
  113. {
  114. if (accessToken instanceof DefaultOAuth2AccessToken)
  115. {
  116. DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
  117. LoginUser user = (LoginUser) authentication.getUserAuthentication().getPrincipal();
  118. Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
  119. additionalInformation.put(SecurityConstants.DETAILS_USERNAME, authentication.getName());
  120. additionalInformation.put(SecurityConstants.DETAILS_USER_ID, user.getUserId());
  121. token.setAdditionalInformation(additionalInformation);
  122. }
  123. return accessToken;
  124. };
  125. };
  126. }
  127. }