|
| 1 | +package cn.datax.auth.config; |
| 2 | + |
| 3 | +import cn.datax.auth.service.DataUserDetailService; |
| 4 | +import cn.datax.auth.translator.DataWebResponseExceptionTranslator; |
| 5 | +import cn.datax.common.core.DataConstant; |
| 6 | +import cn.datax.common.core.DataUser; |
| 7 | + |
| 8 | +import cn.datax.common.security.utils.RedisTokenStore; |
| 9 | +import cn.hutool.core.collection.CollUtil; |
| 10 | +import cn.hutool.core.util.StrUtil; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 15 | +import org.springframework.security.authentication.AuthenticationManager; |
| 16 | +import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; |
| 17 | +import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; |
| 18 | +import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; |
| 19 | +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; |
| 20 | +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; |
| 21 | +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; |
| 22 | +import org.springframework.security.oauth2.provider.ClientDetailsService; |
| 23 | +import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; |
| 24 | +import org.springframework.security.oauth2.provider.token.*; |
| 25 | +import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; |
| 26 | +import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; |
| 27 | + |
| 28 | +import javax.sql.DataSource; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +@Configuration |
| 33 | +@EnableAuthorizationServer |
| 34 | +public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private DataSource dataSource; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private AuthenticationManager authenticationManager; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private DataUserDetailService userDetailService; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private RedisConnectionFactory redisConnectionFactory; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private DataWebResponseExceptionTranslator exceptionTranslator; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * 配置客户端详情服务 |
| 55 | + * @param clients |
| 56 | + * @throws Exception |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public void configure(ClientDetailsServiceConfigurer clients) throws Exception { |
| 60 | + // 默认数据库的配置 |
| 61 | + // datax:123456 |
| 62 | + // normal-app:normal-app |
| 63 | + // trusted-app:trusted-app |
| 64 | + clients.jdbc(dataSource).clients(clientDetails()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 用来配置令牌端点(Token Endpoint)的安全约束. |
| 69 | + * @param security |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public void configure(AuthorizationServerSecurityConfigurer security) { |
| 74 | + security.tokenKeyAccess("permitAll()") |
| 75 | + .checkTokenAccess("isAuthenticated()") |
| 76 | + .allowFormAuthenticationForClients(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 用来配置授权(authorization)以及令牌(token)的访问端点和令牌服务(token services) |
| 81 | + * @param endpoints |
| 82 | + * @throws Exception |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void configure(AuthorizationServerEndpointsConfigurer endpoints) { |
| 86 | + endpoints.tokenStore(tokenStore()) |
| 87 | + .tokenServices(tokenServices()) |
| 88 | + .userDetailsService(userDetailService) |
| 89 | + .authenticationManager(authenticationManager) |
| 90 | + .exceptionTranslator(exceptionTranslator); |
| 91 | + } |
| 92 | + |
| 93 | + @Bean |
| 94 | + public ClientDetailsService clientDetails() { |
| 95 | + return new JdbcClientDetailsService(dataSource); |
| 96 | + } |
| 97 | + |
| 98 | + @Bean |
| 99 | + public TokenStore tokenStore(){ |
| 100 | + return new JwtTokenStore(accessTokenConverter()); |
| 101 | + } |
| 102 | + |
| 103 | + @Bean |
| 104 | + public JwtAccessTokenConverter accessTokenConverter() { |
| 105 | + JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); |
| 106 | + converter.setSigningKey("123"); |
| 107 | + return converter; |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + public DefaultTokenServices tokenServices(){ |
| 112 | + DefaultTokenServices tokenServices = new DefaultTokenServices(); |
| 113 | + tokenServices.setTokenStore(tokenStore()); |
| 114 | + tokenServices.setSupportRefreshToken(true); |
| 115 | + tokenServices.setClientDetailsService(clientDetails()); |
| 116 | + tokenServices.setTokenEnhancer(tokenEnhancer()); |
| 117 | +// // token有效期自定义设置,默认12小时 设置为24小时86400 |
| 118 | +// tokenServices.setAccessTokenValiditySeconds(60 * 60 * 24 * 1); |
| 119 | +// // refresh_token默认30天 设置为7天604800 |
| 120 | +// tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7); |
| 121 | + return tokenServices; |
| 122 | + } |
| 123 | + |
| 124 | + @Bean |
| 125 | + public TokenEnhancer tokenEnhancer() { |
| 126 | + return (accessToken, authentication) -> { |
| 127 | + final Map<String, Object> additionalInfo = new HashMap<>(); |
| 128 | + DataUser user = (DataUser) authentication.getUserAuthentication().getPrincipal(); |
| 129 | + additionalInfo.put(DataConstant.UserAdditionalInfo.LICENSE.getKey(), DataConstant.UserAdditionalInfo.LICENSE.getVal()); |
| 130 | + additionalInfo.put(DataConstant.UserAdditionalInfo.USERID.getKey(), user.getId()); |
| 131 | + additionalInfo.put(DataConstant.UserAdditionalInfo.USERNAME.getKey(), user.getUsername()); |
| 132 | + additionalInfo.put(DataConstant.UserAdditionalInfo.NICKNAME.getKey(), user.getNickname()); |
| 133 | + |
| 134 | + if (StrUtil.isNotBlank(user.getDept())){ |
| 135 | + additionalInfo.put(DataConstant.UserAdditionalInfo.DEPT.getKey(), user.getDept()); |
| 136 | + } |
| 137 | + if (CollUtil.isNotEmpty(user.getRoles())){ |
| 138 | + additionalInfo.put(DataConstant.UserAdditionalInfo.ROLE.getKey(), user.getRoles()); |
| 139 | + } |
| 140 | + if (CollUtil.isNotEmpty(user.getPosts())){ |
| 141 | + additionalInfo.put(DataConstant.UserAdditionalInfo.POST.getKey(), user.getPosts()); |
| 142 | + } |
| 143 | + ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); |
| 144 | + return accessToken; |
| 145 | + }; |
| 146 | + } |
| 147 | +} |
0 commit comments