AuthorizationServerConfig.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.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.core.userdetails.UserDetailsService;
  12. import org.springframework.security.crypto.password.PasswordEncoder;
  13. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  14. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  15. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  16. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  17. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  18. import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
  19. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  20. import org.springframework.security.oauth2.provider.token.TokenStore;
  21. import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
  22. import javax.annotation.Resource;
  23. import javax.sql.DataSource;
  24. /**
  25. * Created by chenweida on 2017/12/4.
  26. */
  27. @Configuration
  28. @EnableAuthorizationServer //开启授权服务器
  29. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  30. @Autowired
  31. private UserDetailsService userDetailsService;
  32. @Autowired
  33. private ClientServiceProvider clientDetailsService;
  34. @Autowired
  35. private RedisConnectionFactory redisConnectionFactory;
  36. @Autowired
  37. private PasswordEncoder passwordEncoder;
  38. @Autowired
  39. private AccessTokenPorperties accessTokenPorperties;
  40. @Override
  41. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
  42. security.passwordEncoder(passwordEncoder);
  43. }
  44. @Override
  45. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  46. endpoints.authenticationManager(oAuth2AuthenticationManager())
  47. .tokenStore(tokenStore())
  48. .userDetailsService(userDetailsService)
  49. .tokenServices(defaultTokenServices())
  50. // .pathMapping("/oauth/confirm_access", "/extenal/oauth/confirm_access");//授权码模式 授权页面转换
  51. ;
  52. //endpoints.setClientDetailsService(clientDetailsService);
  53. }
  54. @Override
  55. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  56. //.jdbc(dataSource).passwordEncoder(passwordEncoder) .clients(clientDetailsService)
  57. clients.withClientDetails(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. defaultTokenServices.setClientDetailsService(clientDetailsService);
  82. return defaultTokenServices;
  83. }
  84. @Bean
  85. @Primary
  86. TokenStore tokenStore() {
  87. RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
  88. redisTokenStore.setPrefix(SecurityProperties.prefix_accesstoken);
  89. return redisTokenStore;
  90. }
  91. }