AuthorizationServerConfig.java 4.4 KB

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