AuthorizationServerConfig.java 3.9 KB

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