From 033f76038ca8030275c2e6216a24b6f69dbaccff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Thu, 11 Jul 2019 15:49:29 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20Fixing=20a=20bug.=20oauth=20server=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7ID=E6=B2=A1=E6=9C=89=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=EF=BC=8Csecurityutils=20=E4=B8=8D=E8=83=BD=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/AuthorizationServerConfig.java | 7 ++++++- .../core/constant/SecurityConstants.java | 20 +++++++++++++++++++ .../PigUserAuthenticationConverter.java | 10 ++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java index 7ad5bcb3..be8a8b7e 100755 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java @@ -19,6 +19,7 @@ package com.pig4cloud.pig.auth.config; import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.security.component.PigWebResponseExceptionTranslator; import com.pig4cloud.pig.common.security.service.PigClientDetailsService; +import com.pig4cloud.pig.common.security.service.PigUser; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; @@ -95,7 +96,11 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap public TokenEnhancer tokenEnhancer() { return (accessToken, authentication) -> { final Map additionalInfo = new HashMap<>(1); - additionalInfo.put("license", SecurityConstants.PROJECT_LICENSE); + PigUser pigUser = (PigUser) authentication.getUserAuthentication().getPrincipal(); + additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PROJECT_LICENSE); + additionalInfo.put(SecurityConstants.DETAILS_USER_ID, pigUser.getId()); + additionalInfo.put(SecurityConstants.DETAILS_USERNAME, pigUser.getUsername()); + additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, pigUser.getDeptId()); ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; }; diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/SecurityConstants.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/SecurityConstants.java index eac115d2..835d152b 100755 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/SecurityConstants.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/SecurityConstants.java @@ -100,4 +100,24 @@ public interface SecurityConstants { * 资源服务器默认bean名称 */ String RESOURCE_SERVER_CONFIGURER = "resourceServerConfigurerAdapter"; + + /** + * 用户ID字段 + */ + String DETAILS_USER_ID = "user_id"; + + /** + * 用户名字段 + */ + String DETAILS_USERNAME = "username"; + + /** + * 用户部门字段 + */ + String DETAILS_DEPT_ID = "dept_id"; + + /** + * 协议字段 + */ + String DETAILS_LICENSE = "license"; } diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigUserAuthenticationConverter.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigUserAuthenticationConverter.java index fe16b27f..65f91e97 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigUserAuthenticationConverter.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigUserAuthenticationConverter.java @@ -16,6 +16,7 @@ package com.pig4cloud.pig.common.security.component; +import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.security.service.PigUser; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -35,9 +36,6 @@ import java.util.Map; * 根据checktoken 的结果转化用户信息 */ public class PigUserAuthenticationConverter implements UserAuthenticationConverter { - private static final String USER_ID = "user_id"; - private static final String DEPT_ID = "dept_id"; - private static final String TENANT_ID = "tenant_id"; private static final String N_A = "N/A"; /** @@ -67,9 +65,9 @@ public class PigUserAuthenticationConverter implements UserAuthenticationConvert if (map.containsKey(USERNAME)) { Collection authorities = getAuthorities(map); - String username = (String) map.get(USERNAME); - Integer id = (Integer) map.get(USER_ID); - Integer deptId = (Integer) map.get(DEPT_ID); + String username = (String) map.get(SecurityConstants.DETAILS_USERNAME); + Integer id = (Integer) map.get(SecurityConstants.DETAILS_USER_ID); + Integer deptId = (Integer) map.get(SecurityConstants.DETAILS_DEPT_ID); PigUser user = new PigUser(id, deptId, username, N_A, true , true, true, true, authorities); return new UsernamePasswordAuthenticationToken(user, N_A, authorities);