AuthorizationServerConfig.java 4.8 KB

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