OAuth2Configurer.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.diagbot.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.io.ClassPathResource;
  7. import org.springframework.security.authentication.AuthenticationManager;
  8. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  10. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  11. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  12. import org.springframework.security.oauth2.provider.token.TokenEnhancer;
  13. import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
  14. import org.springframework.security.oauth2.provider.token.TokenStore;
  15. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  16. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  17. import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
  18. import java.util.Arrays;
  19. /**
  20. * @Description: OAuth2授权认证配置类
  21. * @author: gaodm
  22. * @time: 2018/8/2 14:24
  23. */
  24. @Configuration
  25. @EnableAuthorizationServer
  26. public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
  27. @Override
  28. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  29. clients.inMemory()
  30. .withClient("uaa-service")
  31. .secret("{noop}123456")
  32. .scopes("service")
  33. .autoApprove(true)
  34. .authorizedGrantTypes("implicit", "refresh_token", "password", "authorization_code")
  35. .accessTokenValiditySeconds(30 * 24 * 3600)
  36. .refreshTokenValiditySeconds(30 * 24 * 3600);//todo gaodm 现改为365天,正式改为24小时过期
  37. }
  38. /**
  39. * 注入自定义token生成方式
  40. *
  41. * @return
  42. */
  43. @Bean
  44. public TokenEnhancer customerEnhancer() {
  45. return new CustomTokenEnhancer();
  46. }
  47. @Override
  48. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  49. //指定认证管理器
  50. endpoints.authenticationManager(authenticationManager);
  51. //指定token存储位置
  52. endpoints.tokenStore(tokenStore());
  53. // 自定义token生成方式
  54. TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
  55. tokenEnhancerChain.setTokenEnhancers(Arrays.asList(customerEnhancer(), jwtTokenEnhancer()));
  56. endpoints.tokenEnhancer(tokenEnhancerChain);
  57. }
  58. @Autowired
  59. @Qualifier("authenticationManagerBean")
  60. private AuthenticationManager authenticationManager;
  61. @Bean
  62. public TokenStore tokenStore() {
  63. return new JwtTokenStore(jwtTokenEnhancer());
  64. }
  65. @Bean
  66. protected JwtAccessTokenConverter jwtTokenEnhancer() {
  67. KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("diagbot-jwt.jks"), "diagbot123456".toCharArray());
  68. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  69. converter.setKeyPair(keyStoreKeyFactory.getKeyPair("diagbot-jwt"));
  70. return converter;
  71. }
  72. }