AuthorizationServerConfig.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.yihu.base.security.config;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.base.security.properties.AccessTokenPorperties;
  4. import com.yihu.base.security.properties.SecurityProperties;
  5. import com.yihu.base.security.rbas.ClientServiceProvider;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.Primary;
  11. import org.springframework.data.redis.connection.RedisConnectionFactory;
  12. import org.springframework.security.authentication.AuthenticationManager;
  13. import org.springframework.security.core.userdetails.UserDetailsService;
  14. import org.springframework.security.crypto.password.PasswordEncoder;
  15. import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
  16. import org.springframework.security.oauth2.common.OAuth2AccessToken;
  17. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  18. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  19. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  20. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  21. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  22. import org.springframework.security.oauth2.provider.ClientDetailsService;
  23. import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
  24. import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
  25. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  26. import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
  27. import org.springframework.security.oauth2.provider.token.TokenStore;
  28. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  29. import javax.annotation.Resource;
  30. import javax.sql.DataSource;
  31. /**
  32. * Created by chenweida on 2017/12/4.
  33. */
  34. @Configuration
  35. @EnableAuthorizationServer //开启授权服务器
  36. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  37. @Autowired
  38. private UserDetailsService userDetailsService;
  39. @Autowired
  40. private ClientServiceProvider clientDetailsService;
  41. @Autowired
  42. private RedisConnectionFactory redisConnectionFactory;
  43. @Autowired
  44. private PasswordEncoder passwordEncoder;
  45. @Autowired
  46. private DataSource dataSource;
  47. @Autowired
  48. private AccessTokenPorperties accessTokenPorperties;
  49. @Override
  50. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  51. security.passwordEncoder(passwordEncoder);
  52. }
  53. @Override
  54. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  55. endpoints.authenticationManager(oAuth2AuthenticationManager())
  56. .tokenStore(tokenStore())
  57. .userDetailsService(userDetailsService)
  58. .tokenServices(defaultTokenServices());
  59. //endpoints.setClientDetailsService(clientDetailsService);
  60. }
  61. @Override
  62. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  63. //.jdbc(dataSource).passwordEncoder(passwordEncoder) .clients(clientDetailsService)
  64. clients.withClientDetails(clientDetailsService) ;
  65. ;
  66. }
  67. @Bean
  68. ObjectMapper objectMapper() {
  69. ObjectMapper objectMapper = new ObjectMapper();
  70. return objectMapper;
  71. }
  72. @Bean
  73. @Primary
  74. OAuth2AuthenticationManager oAuth2AuthenticationManager() {
  75. OAuth2AuthenticationManager oAuth2AuthenticationManager = new OAuth2AuthenticationManager();
  76. oAuth2AuthenticationManager.setClientDetailsService(clientDetailsService);
  77. oAuth2AuthenticationManager.setTokenServices(defaultTokenServices());
  78. return oAuth2AuthenticationManager;
  79. }
  80. //==========================token相关配置=================================
  81. @Bean
  82. @Primary
  83. DefaultTokenServices defaultTokenServices() {
  84. DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
  85. defaultTokenServices.setTokenStore(tokenStore());
  86. defaultTokenServices.setAccessTokenValiditySeconds(60 * 60 * accessTokenPorperties.getAccessTokenValidityHours()); //默认2小时
  87. defaultTokenServices.setRefreshTokenValiditySeconds(60 * 60 * accessTokenPorperties.getRefreshTokenValidityHours());//默认2小时
  88. return defaultTokenServices;
  89. }
  90. @Bean
  91. @Primary
  92. TokenStore tokenStore() {
  93. RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
  94. redisTokenStore.setPrefix(SecurityProperties.prefix_accesstoken);
  95. return redisTokenStore;
  96. }
  97. }