1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package com.diagbot.config;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
- import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
- import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
- import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
- import org.springframework.security.oauth2.provider.token.TokenEnhancer;
- import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
- import org.springframework.security.oauth2.provider.token.TokenStore;
- import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
- import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
- import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
- import java.util.Arrays;
- /**
- * @Description: OAuth2授权认证配置类
- * @author: gaodm
- * @time: 2018/8/2 14:24
- */
- @Configuration
- @EnableAuthorizationServer
- public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
- @Override
- public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
- clients.inMemory()
- .withClient("uaa-service")
- .secret("{noop}123456")
- .scopes("service")
- .autoApprove(true)
- .authorizedGrantTypes("implicit", "refresh_token", "password", "authorization_code")
- .accessTokenValiditySeconds(30 * 24 * 3600)
- .refreshTokenValiditySeconds(30 * 24 * 3600);//todo gaodm 现改为365天,正式改为24小时过期
- }
- /**
- * 注入自定义token生成方式
- *
- * @return
- */
- @Bean
- public TokenEnhancer customerEnhancer() {
- return new CustomTokenEnhancer();
- }
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
- //指定认证管理器
- endpoints.authenticationManager(authenticationManager);
- //指定token存储位置
- endpoints.tokenStore(tokenStore());
- // 自定义token生成方式
- TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
- tokenEnhancerChain.setTokenEnhancers(Arrays.asList(customerEnhancer(), jwtTokenEnhancer()));
- endpoints.tokenEnhancer(tokenEnhancerChain);
- }
- @Autowired
- @Qualifier("authenticationManagerBean")
- private AuthenticationManager authenticationManager;
- @Bean
- public TokenStore tokenStore() {
- return new JwtTokenStore(jwtTokenEnhancer());
- }
- @Bean
- protected JwtAccessTokenConverter jwtTokenEnhancer() {
- KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("diagbot-jwt.jks"), "diagbot123456".toCharArray());
- JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
- converter.setKeyPair(keyStoreKeyFactory.getKeyPair("diagbot-jwt"));
- return converter;
- }
- }
|