🐛 Fixing a bug. oauth server 用户ID没有注入,securityutils 不能获取

This commit is contained in:
冷冷 2019-07-11 15:49:29 +08:00
parent 51f95c7d8c
commit 033f76038c
3 changed files with 30 additions and 7 deletions

View File

@ -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<String, Object> 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;
};

View File

@ -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";
}

View File

@ -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<? extends GrantedAuthority> 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);