AuthorizationServerConfig.java 4.3 KB

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