新增获取userId

This commit is contained in:
glpyh 2018-07-01 12:47:35 +08:00
parent fa1df09150
commit 4f82d4801f
4 changed files with 35 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package com.github.pig.auth.config;
import com.github.pig.auth.util.UserDetailsImpl;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.constant.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
@ -134,8 +135,12 @@ public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter
@Bean
public TokenEnhancer tokenEnhancer() {
return (accessToken, authentication) -> {
final Map<String, Object> additionalInfo = new HashMap<>(1);
final Map<String, Object> additionalInfo = new HashMap<>(2);
additionalInfo.put("license", SecurityConstants.PIG_LICENSE);
UserDetailsImpl user = (UserDetailsImpl) authentication.getUserAuthentication().getPrincipal();
if(user!=null) {
additionalInfo.put("userId", user.getUserId());
}
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
};

View File

@ -36,13 +36,14 @@ import java.util.List;
*/
public class UserDetailsImpl implements UserDetails {
private static final long serialVersionUID = 1L;
private Integer userId;
private String username;
private String password;
private String status;
private List<SysRole> roleList;
public UserDetailsImpl(UserVO userVo) {
this.userId=userVo.getUserId();
this.username = userVo.getUsername();
this.password = userVo.getPassword();
this.status = userVo.getDelFlag();
@ -109,6 +110,10 @@ public class UserDetailsImpl implements UserDetails {
return status;
}
public Integer getUserId() {
return userId;
}
public void setStatus(String status) {
this.status = status;
}

View File

@ -55,6 +55,20 @@ public class UserUtils {
return roleNames;
}
/**
* 根据header中的token获取用户ID
*
* @param httpServletRequest
* @return 用户ID
*/
public static Integer getUserId(HttpServletRequest httpServletRequest) {
String token = getToken(httpServletRequest);
String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
Integer userId = (Integer) claims.get("userId");
return userId;
}
/**
* 获取请求中token
*

View File

@ -41,5 +41,14 @@ public class BaseController {
return UserUtils.getRole(request);
}
/**
* 根据请求heard中的token获取用户ID
*
* @return 用户ID
*/
public Integer getUserId() {
return UserUtils.getUserId(request);
}
}