Commit ae2cf8b2 by yuwei

项目初始化

parent e4d65d2f
......@@ -118,10 +118,10 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
return (accessToken, authentication) -> {
final Map<String, Object> additionalInfo = new HashMap<>();
DataUser user = (DataUser) authentication.getUserAuthentication().getPrincipal();
additionalInfo.put(DataConstant.UserAdditionalInfo.LICENSE.getKey(), "datax");
additionalInfo.put(DataConstant.UserAdditionalInfo.USERID.getKey(), user.getId());
additionalInfo.put(DataConstant.UserAdditionalInfo.USERNAME.getKey(), user.getUsername());
additionalInfo.put(DataConstant.UserAdditionalInfo.NICKNAME.getKey(), user.getNickname());
additionalInfo.put(DataConstant.DETAILS_SECURITY_LICENSE, "datax");
additionalInfo.put(DataConstant.DETAILS_SECURITY_USER_ID, user.getId());
additionalInfo.put(DataConstant.DETAILS_SECURITY_USERNAME, user.getUsername());
additionalInfo.put(DataConstant.DETAILS_SECURITY_NICKNAME, user.getNickname());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
};
......
......@@ -34,6 +34,7 @@ public class DataUserDetailService implements UserDetailsService {
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//远程获取用户
R result = userServiceFeign.loginByUsername(s);
log.info(JSON.toJSONString(result));
if(result == null || ObjectUtil.isEmpty(result.getData())){
throw new UsernameNotFoundException(StrUtil.format("{}用户不存在", s));
}
......@@ -53,10 +54,10 @@ public class DataUserDetailService implements UserDetailsService {
UserVo userVo = userInfo.getUserVo();
List<RoleVo> roles = userVo.getRoles();
if (CollUtil.isNotEmpty(roles)) {
roles.stream().filter(roleVo -> DataConstant.EnableState.ENABLE.getKey() == roleVo.getStatus() && StrUtil.isNotBlank(roleVo.getRoleCode())).forEach(roleVo -> authsSet.add(DataConstant.Security.ROLEPREFIX.getVal() + roleVo.getRoleCode()));
roles.stream().filter(roleVo -> DataConstant.TRUE == roleVo.getStatus() && StrUtil.isNotBlank(roleVo.getRoleCode())).forEach(roleVo -> authsSet.add(DataConstant.ROLE + roleVo.getRoleCode()));
}
if(CollUtil.isEmpty(authsSet)){
authsSet.add(DataConstant.Security.ROLEPREFIX.getVal() + "VISITOR");
authsSet.add(DataConstant.ROLE + "USER");
}
Collection<? extends GrantedAuthority> authorities
= AuthorityUtils.createAuthorityList(authsSet.toArray(new String[0]));
......
......@@ -3,106 +3,33 @@ package cn.datax.common.core;
public class DataConstant {
/**
* Oauth2安全相关常量
* Gateway请求头TOKEN名称(不要有空格)
*/
public static enum Security{
//请求头TOKEN名称
TOKENHEADER("tokenHeader", "gatewayToken"),
//请求头TOKEN值
TOKENVALUE("tokenValue", "datax:gateway:123456"),
//OAUTH2令牌类型
TOKENTYPE("tokenType", "bearer "),
//security授权角色前缀
ROLEPREFIX("rolePrefix", "ROLE_");
Security(String key, String val){
this.key = key;
this.val = val;
}
private final String key;
private final String val;
public String getKey() {
return key;
}
public String getVal() {
return val;
}
}
public static final String GATEWAY_TOKEN_HEADER = "GatewayToken";
/**
* 通用的是否
* Gateway请求头TOKEN值
*/
public static enum TrueOrFalse{
FALSE(0,false),
TRUE(1,true);
TrueOrFalse(Integer key, boolean val){
this.key = key;
this.val = val;
}
private final Integer key;
private final boolean val;
public Integer getKey() {
return key;
}
public boolean getVal() {
return val;
}
}
public static final String GATEWAY_TOKEN_VALUE = "datax:gateway:123456";
/**
* 用户认证返回额外信息
* OAUTH2 令牌类型
*/
public static enum UserAdditionalInfo{
LICENSE("license", "许可证"),
USER("user", "用户"),
USERID("user_id", "用户ID"),
USERNAME("username", "用户名"),
NICKNAME("nickname", "用户昵称"),
DEPT("user_dept", "用户部门"),
ROLE("user_role", "用户角色"),
POST("user_post", "用户岗位");
UserAdditionalInfo(String key, String val){
this.key = key;
this.val = val;
}
private final String key;
private final String val;
public String getKey() {
return key;
}
public static final String OAUTH2_TOKEN_TYPE = "bearer ";
public String getVal() {
return val;
}
}
public static String ROLE = "ROLE_";
/**
* 通用的启用禁用状态
*/
public static enum EnableState{
DISABLE(0,"禁用"),
ENABLE(1,"启用");
EnableState(Integer key, String val){
this.key = key;
this.val = val;
}
public static int TRUE = 1;
private final Integer key;
private final String val;
public static int FALSE = 0;
public Integer getKey() {
return key;
}
public static String DETAILS_SECURITY_LICENSE = "license";
public static String DETAILS_SECURITY_DATAX_USER = "datax_user";
public static String DETAILS_SECURITY_USER_ID = "user_id";
public static String DETAILS_SECURITY_USERNAME = "username";
public static String DETAILS_SECURITY_NICKNAME = "nickname";
public static String DETAILS_SECURITY_USER_DEPT = "user_dept";
public static String DETAILS_SECURITY_USER_ROLE = "user_role";
public static String DETAILS_SECURITY_USER_POST = "user_post";
public String getVal() {
return val;
}
}
}
......@@ -16,13 +16,13 @@ public class DataFeignRequestInterceptor {
public RequestInterceptor oauth2FeignRequestInterceptor() {
return requestTemplate -> {
// 请求头中添加 Gateway Token
String tokenValue = new String(Base64Utils.encode(DataConstant.Security.TOKENVALUE.getVal().getBytes()));
requestTemplate.header(DataConstant.Security.TOKENHEADER.getVal(), tokenValue);
String zuulToken = new String(Base64Utils.encode(DataConstant.GATEWAY_TOKEN_VALUE.getBytes()));
requestTemplate.header(DataConstant.GATEWAY_TOKEN_HEADER, zuulToken);
// 请求头中添加原请求头中的 Token
Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
if (details instanceof OAuth2AuthenticationDetails) {
String authorizationToken = ((OAuth2AuthenticationDetails) details).getTokenValue();
requestTemplate.header(HttpHeaders.AUTHORIZATION, DataConstant.Security.TOKENTYPE.getVal() + authorizationToken);
requestTemplate.header(HttpHeaders.AUTHORIZATION, DataConstant.OAUTH2_TOKEN_TYPE + authorizationToken);
}
};
}
......
......@@ -16,11 +16,11 @@ public class DataServerProtectInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
// 从请求头中获取Token
String token = request.getHeader(DataConstant.Security.TOKENVALUE.getVal());
String gatewayToken = new String(Base64Utils.encode(DataConstant.Security.TOKENHEADER.getVal().getBytes()));
// 校验Token的正确性
if (StrUtil.equals(gatewayToken, token)) {
// 从请求头中获取 Zuul Token
String token = request.getHeader(DataConstant.GATEWAY_TOKEN_HEADER);
String zuulToken = new String(Base64Utils.encode(DataConstant.GATEWAY_TOKEN_VALUE.getBytes()));
// 校验 Zuul Token的正确性
if (StrUtil.equals(zuulToken, token)) {
return true;
} else {
ResponseUtil.makeResponse(
......
......@@ -42,9 +42,9 @@ public class DataUserAuthenticationConverter implements UserAuthenticationConver
Object principal = map.get(USERNAME);
Collection<? extends GrantedAuthority> authorities = this.getAuthorities(map);
String id = (String) map.get(DataConstant.UserAdditionalInfo.USERID.getKey());
String username = (String) map.get(DataConstant.UserAdditionalInfo.USERNAME.getKey());
String nickname = (String) map.get(DataConstant.UserAdditionalInfo.NICKNAME.getKey());
String id = (String) map.get(DataConstant.DETAILS_SECURITY_USER_ID);
String username = (String) map.get(DataConstant.DETAILS_SECURITY_USERNAME);
String nickname = (String) map.get(DataConstant.DETAILS_SECURITY_USERNAME);
DataUser user = new DataUser(id, nickname, username, N_A, true
, true, true, true, authorities);
return new UsernamePasswordAuthenticationToken(user, N_A, authorities);
......
......@@ -31,9 +31,9 @@ public class DataGatewayRequestFilter implements GlobalFilter {
printLog(exchange);
byte[] token = Base64Utils.encode((DataConstant.Security.TOKENVALUE.getVal()).getBytes());
byte[] token = Base64Utils.encode((DataConstant.GATEWAY_TOKEN_VALUE).getBytes());
String[] headerValues = {new String(token)};
ServerHttpRequest build = request.mutate().header(DataConstant.Security.TOKENHEADER.getVal(), headerValues).build();
ServerHttpRequest build = request.mutate().header(DataConstant.GATEWAY_TOKEN_HEADER, headerValues).build();
ServerWebExchange newExchange = exchange.mutate().request(build).build();
return chain.filter(newExchange);
}
......
......@@ -24,7 +24,7 @@ public class LoginController extends BaseController {
@Autowired
private TokenStore tokenStore;
@GetMapping("/user")
@GetMapping("/token")
public R getTokenUser() {
DataUser user = SecurityUtil.getDataUser();
return R.ok().setData(user);
......
......@@ -148,7 +148,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserDao, UserEntity> implem
if(CollUtil.isNotEmpty(userVo.getRoles())){
Set<String> permissions = new HashSet<>();
List<String> roleIds = userVo.getRoles().stream()
.filter(roleVo -> DataConstant.EnableState.ENABLE.getKey() == roleVo.getStatus())
.filter(roleVo -> DataConstant.TRUE == roleVo.getStatus())
.map(RoleVo::getId).collect(Collectors.toList());
List<MenuEntity> menuEntitys = menuDao.selectMenuByRoleIds(roleIds);
if(CollUtil.isNotEmpty(menuEntitys)){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment