♻️ 重构代码。 重构token TokenEnhancer

This commit is contained in:
冷冷 committed 2019-09-18 17:27:54 +08:00
1 parent b7b414b3f2
commit 75a9e98c4b
9 files changed
+77 -39

No files matched your search

@@ -24,7 +24,6 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
import com.pig4cloud.pigx.common.security.component.PigxWebResponseExceptionTranslator;
import com.pig4cloud.pigx.common.security.service.PigxClientDetailsService;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
@@ -33,7 +32,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
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;
@@ -46,8 +44,6 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* @author lengleng
@@ -59,6 +55,7 @@ import java.util.Map;
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private final DataSource dataSource;
private final TokenEnhancer pigxTokenEnhancer;
private final PigxUserDetailsService pigxUserDetailsService;
private final AuthenticationManager authenticationManagerBean;
private final RedisConnectionFactory redisConnectionFactory;
@@ -84,7 +81,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
endpoints
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancer())
.tokenEnhancer(pigxTokenEnhancer)
.userDetailsService(pigxUserDetailsService)
.authenticationManager(authenticationManagerBean)
.reuseRefreshTokens(false)
@@ -105,30 +102,4 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
});
return tokenStore;
}
/**
* token增强,客户端模式不增强。
*
* @return TokenEnhancer
*/
@Bean
public TokenEnhancer tokenEnhancer() {
return (accessToken, authentication) -> {
if (SecurityConstants.CLIENT_CREDENTIALS
.equals(authentication.getOAuth2Request().getGrantType())) {
return accessToken;
}
final Map<String, Object> additionalInfo = new HashMap<>(8);
PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
additionalInfo.put(SecurityConstants.DETAILS_USER_ID, pigxUser.getId());
additionalInfo.put(SecurityConstants.DETAILS_USERNAME, pigxUser.getUsername());
additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, pigxUser.getDeptId());
additionalInfo.put(SecurityConstants.DETAILS_TENANT_ID, pigxUser.getTenantId());
additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PIGX_LICENSE);
additionalInfo.put(SecurityConstants.ACTIVE, Boolean.TRUE);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
};
}
}
+6
View File
@@ -51,5 +51,11 @@
<artifactId>elastic-job-lite-spring</artifactId>
<version>${elastic-job-lite.version}</version>
</dependency>
<!--xxl job-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl.job.version}</version>
</dependency>
</dependencies>
</project>
@@ -25,12 +25,12 @@ import java.lang.annotation.*;
/**
* @author lengleng
* @date 2018/7/24
* 开启pigx job
* 开启pigx elastic job
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ElasticJobAutoConfiguration.class})
public @interface EnablePigxJob {
public @interface EnablePigxElasticJob {
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.component;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import java.util.HashMap;
import java.util.Map;
/**
* @author lengleng
* @date 2019-09-17
* <p>
* token增强,客户端模式不增强。
*/
public class PigxTokenEnhancer implements TokenEnhancer {
/**
* Provides an opportunity for customization of an access token (e.g. through its additional information map) during
* the process of creating a new token for use by a client.
*
* @param accessToken the current access token with its expiration and refresh token
* @param authentication the current authentication including client and user details
* @return a new token enhanced with additional information
*/
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
if (SecurityConstants.CLIENT_CREDENTIALS
.equals(authentication.getOAuth2Request().getGrantType())) {
return accessToken;
}
final Map<String, Object> additionalInfo = new HashMap<>(8);
PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
additionalInfo.put(SecurityConstants.DETAILS_USER_ID, pigxUser.getId());
additionalInfo.put(SecurityConstants.DETAILS_USERNAME, pigxUser.getUsername());
additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, pigxUser.getDeptId());
additionalInfo.put(SecurityConstants.DETAILS_TENANT_ID, pigxUser.getTenantId());
additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PIGX_LICENSE);
additionalInfo.put(SecurityConstants.ACTIVE, Boolean.TRUE);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
@@ -35,7 +35,6 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collection;
@@ -48,7 +47,6 @@ import java.util.Set;
* @author lengleng
*/
@Slf4j
@Service
@AllArgsConstructor
public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
private final RemoteUserService remoteUserService;
@@ -50,8 +50,6 @@ public class SecurityUtils {
* @param authentication
* @return PigxUser
* <p>
* 获取当前用户的全部信息 EnablePigxResourceServer true
* 获取当前用户的用户名 EnablePigxResourceServer false
*/
public PigxUser getUser(Authentication authentication) {
Object principal = authentication.getPrincipal();
@@ -1,4 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.openfeign.PigxHystrixFeignTargeterConfiguration,\
com.pig4cloud.pigx.common.security.component.GlobalExceptionHandlerResolver,\
com.pig4cloud.pigx.common.security.component.PigxTokenEnhancer,\
com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl
@@ -17,7 +17,7 @@
package com.pig4cloud.pigx.daemon.elastic;
import com.pig4cloud.pigx.common.job.annotation.EnablePigxJob;
import com.pig4cloud.pigx.common.job.annotation.EnablePigxXxlJob;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxFeignClients;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer;
import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2;
@@ -29,7 +29,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
* @date 2018/7/24
* 分布式任务调度模块
*/
@EnablePigxJob
@EnablePigxXxlJob
@EnablePigxSwagger2
@EnablePigxFeignClients
@SpringCloudApplication
+1
View File
@@ -63,6 +63,7 @@
<minio.version>6.0.8</minio.version>
<fastjson.version>1.2.58</fastjson.version>
<elastic-job-lite.version>2.1.5</elastic-job-lite.version>
<xxl.job.version>2.1.0</xxl.job.version>
<security.oauth.version>2.3.6.RELEASE</security.oauth.version>
<activiti.version>5.22.0</activiti.version>
<docker.url>http://192.168.0.13:4243</docker.url>