feat;增加角色模块
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
package com.tiesheng.util.config;
|
||||
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTValidator;
|
||||
import com.tiesheng.util.ServletKit;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.pojos.TokenBean;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties("tiesheng.token")
|
||||
public class TsTokenConfig {
|
||||
|
||||
private Map<String, TokenBean> testMap = MapUtil.newHashMap();
|
||||
private String encryptKey = "%kIp9frQCu";
|
||||
private Integer expireHours = 48;
|
||||
private String[] ignorePaths;
|
||||
private boolean validLoginSign = false;
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登录的token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static TokenBean get() {
|
||||
TsTokenConfig tokenConfig = SpringUtil.getBean(TsTokenConfig.class);
|
||||
return tokenConfig.validToken(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登录的token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static TokenBean getWithoutThr() {
|
||||
TsTokenConfig tokenConfig = SpringUtil.getBean(TsTokenConfig.class);
|
||||
return tokenConfig.validToken(false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public TokenBean isTestToken(String token) {
|
||||
if (testMap == null) {
|
||||
return null;
|
||||
}
|
||||
return testMap.get(token);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
*
|
||||
* @param thrExp
|
||||
* @return
|
||||
*/
|
||||
public TokenBean validToken(boolean thrExp) {
|
||||
String token = ServletUtil.getHeader(ServletKit.getRequest(), "token", "utf-8");
|
||||
return validToken(token, thrExp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TokenBean validToken(String token, boolean thrExp) {
|
||||
TokenBean tokenBean = isTestToken(token);
|
||||
if (tokenBean != null) {
|
||||
return tokenBean;
|
||||
}
|
||||
|
||||
try {
|
||||
JWT decode = JWT.of(token);
|
||||
JWTValidator.of(decode).validateDate();
|
||||
String id = decode.getPayload("id").toString();
|
||||
String environmentType = decode.getPayload("environmentType").toString();
|
||||
String service = decode.getPayload("service").toString();
|
||||
String extra = StrUtil.toStringOrNull(decode.getPayload("extra"));
|
||||
String roleId = StrUtil.toStringOrNull(decode.getPayload("roleId"));
|
||||
tokenBean = new TokenBean(id, environmentType, service);
|
||||
tokenBean.setRoleId(roleId);
|
||||
tokenBean.setExtra(extra);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
if (tokenBean == null && thrExp) {
|
||||
throw new ApiException(StrUtil.isEmpty(token) ? 110 : 112,
|
||||
StrUtil.isEmpty(token) ? "请先登录" : "登录过期,请重新登陆");
|
||||
}
|
||||
|
||||
if (tokenBean == null) {
|
||||
tokenBean = new TokenBean();
|
||||
tokenBean.setId("");
|
||||
}
|
||||
|
||||
return tokenBean;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public Map<String, TokenBean> getTestMap() {
|
||||
return testMap;
|
||||
}
|
||||
|
||||
public void setTestMap(Map<String, TokenBean> testMap) {
|
||||
this.testMap = testMap;
|
||||
}
|
||||
|
||||
public String getEncryptKey() {
|
||||
return encryptKey;
|
||||
}
|
||||
|
||||
public void setEncryptKey(String encryptKey) {
|
||||
this.encryptKey = encryptKey;
|
||||
}
|
||||
|
||||
public Integer getExpireHours() {
|
||||
return expireHours;
|
||||
}
|
||||
|
||||
public void setExpireHours(Integer expireHours) {
|
||||
this.expireHours = expireHours;
|
||||
}
|
||||
|
||||
public String[] getIgnorePaths() {
|
||||
return ignorePaths;
|
||||
}
|
||||
|
||||
public void setIgnorePaths(String[] ignorePaths) {
|
||||
this.ignorePaths = ignorePaths;
|
||||
}
|
||||
|
||||
public boolean isValidLoginSign() {
|
||||
return validLoginSign;
|
||||
}
|
||||
|
||||
public void setValidLoginSign(boolean validLoginSign) {
|
||||
this.validLoginSign = validLoginSign;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.tiesheng.util.pojos;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import com.tiesheng.util.config.TsTokenConfig;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
public class TokenBean {
|
||||
|
||||
private String id;
|
||||
private String environmentType;
|
||||
private String service;
|
||||
private String roleId;
|
||||
private String extra;
|
||||
|
||||
public TokenBean() {
|
||||
}
|
||||
|
||||
public TokenBean(String id, String environmentType, String service) {
|
||||
this.id = id;
|
||||
this.environmentType = environmentType;
|
||||
this.service = service;
|
||||
this.roleId = "";
|
||||
this.extra = "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置token
|
||||
*/
|
||||
public String toToken() {
|
||||
TsTokenConfig tsTokenConfig = SpringUtil.getBean(TsTokenConfig.class);
|
||||
return JWT.create()
|
||||
.setExpiresAt(DateUtil.offsetHour(DateUtil.date(), tsTokenConfig.getExpireHours()))
|
||||
.setPayload("id", getId())
|
||||
.setPayload("environmentType", StrUtil.emptyToDefault(getEnvironmentType(), ""))
|
||||
.setPayload("service", StrUtil.emptyToDefault(getService(), ""))
|
||||
.setPayload("roleId", StrUtil.emptyToDefault(getRoleId(), ""))
|
||||
.setPayload("extra", StrUtil.emptyToDefault(getExtra(), ""))
|
||||
.setKey(tsTokenConfig.getEncryptKey().getBytes())
|
||||
.sign();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEnvironmentType() {
|
||||
return environmentType;
|
||||
}
|
||||
|
||||
public void setEnvironmentType(String environmentType) {
|
||||
this.environmentType = environmentType;
|
||||
}
|
||||
|
||||
public String getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public String getExtra() {
|
||||
return extra;
|
||||
}
|
||||
|
||||
public void setExtra(String extra) {
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
public String getRoleId() {
|
||||
return roleId;
|
||||
}
|
||||
|
||||
public void setRoleId(String roleId) {
|
||||
this.roleId = roleId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user