mirror of https://gitee.com/maxjhandsome/pig
新增获取userId
This commit is contained in:
parent
fa1df09150
commit
4f82d4801f
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package com.github.pig.auth.config;
|
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.CommonConstant;
|
||||||
import com.github.pig.common.constant.SecurityConstants;
|
import com.github.pig.common.constant.SecurityConstants;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -134,8 +135,12 @@ public class PigAuthorizationConfig extends AuthorizationServerConfigurerAdapter
|
||||||
@Bean
|
@Bean
|
||||||
public TokenEnhancer tokenEnhancer() {
|
public TokenEnhancer tokenEnhancer() {
|
||||||
return (accessToken, authentication) -> {
|
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);
|
additionalInfo.put("license", SecurityConstants.PIG_LICENSE);
|
||||||
|
UserDetailsImpl user = (UserDetailsImpl) authentication.getUserAuthentication().getPrincipal();
|
||||||
|
if(user!=null) {
|
||||||
|
additionalInfo.put("userId", user.getUserId());
|
||||||
|
}
|
||||||
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
|
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
|
||||||
return accessToken;
|
return accessToken;
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,13 +36,14 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class UserDetailsImpl implements UserDetails {
|
public class UserDetailsImpl implements UserDetails {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
private Integer userId;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String status;
|
private String status;
|
||||||
private List<SysRole> roleList;
|
private List<SysRole> roleList;
|
||||||
|
|
||||||
public UserDetailsImpl(UserVO userVo) {
|
public UserDetailsImpl(UserVO userVo) {
|
||||||
|
this.userId=userVo.getUserId();
|
||||||
this.username = userVo.getUsername();
|
this.username = userVo.getUsername();
|
||||||
this.password = userVo.getPassword();
|
this.password = userVo.getPassword();
|
||||||
this.status = userVo.getDelFlag();
|
this.status = userVo.getDelFlag();
|
||||||
|
@ -109,6 +110,10 @@ public class UserDetailsImpl implements UserDetails {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
public void setStatus(String status) {
|
public void setStatus(String status) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,20 @@ public class UserUtils {
|
||||||
return roleNames;
|
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
|
* 获取请求中token
|
||||||
*
|
*
|
||||||
|
|
|
@ -41,5 +41,14 @@ public class BaseController {
|
||||||
return UserUtils.getRole(request);
|
return UserUtils.getRole(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据请求heard中的token获取用户ID
|
||||||
|
*
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public Integer getUserId() {
|
||||||
|
return UserUtils.getUserId(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue