feat: There is no way the, TO optimize the query
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.adc.da.util;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.ExpiredJwtException;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* token 工具类
|
||||
*
|
||||
* @author ch
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
* <p>
|
||||
* Created at 2020/7/30 2:23 下午
|
||||
*/
|
||||
@Component
|
||||
public class JwtSysUtils {
|
||||
|
||||
// 过期时间
|
||||
private static long expire = 6048000;
|
||||
// 秘钥
|
||||
private static String secret = "HSyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9";
|
||||
|
||||
/**
|
||||
* 创建一个token
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
public String generateToken(String userId) {
|
||||
Date now = new Date();
|
||||
Date expireDate = new Date(now.getTime() + expire);
|
||||
return Jwts.builder().setHeaderParam("type", "JWT").setSubject(userId).setIssuedAt(now)
|
||||
.setExpiration(expireDate).signWith(
|
||||
SignatureAlgorithm.HS512, secret).compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析token
|
||||
*/
|
||||
public Claims getClaimsByToken(String token) {
|
||||
Claims claims;
|
||||
try {
|
||||
claims = Jwts.parser()
|
||||
.setSigningKey(secret) // 设置标识名
|
||||
.parseClaimsJws(token) //解析token
|
||||
.getBody();
|
||||
} catch (ExpiredJwtException e) {
|
||||
claims = e.getClaims();
|
||||
}
|
||||
return claims;
|
||||
}
|
||||
|
||||
public String getUserIdByToken(){
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
String user = "";
|
||||
if(attributes != null){
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
Claims claim = getClaimsByToken(request.getHeader("token"));
|
||||
user = claim.getSubject();
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,21 +15,7 @@ public class LoginUserUtil {
|
||||
*
|
||||
*/
|
||||
public static String getUserId() {
|
||||
String userId = null;
|
||||
try {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
Object object=subject.getPrincipal();
|
||||
if(object!=null){
|
||||
String json = JSONObject.toJSONString(object);
|
||||
if(json!=null && json.length()>0){
|
||||
Map<String, Object> userInfo=JSONObject.parseObject(json, Map.class);
|
||||
userId=userInfo.get("id").toString();
|
||||
}
|
||||
}
|
||||
} catch (UnavailableSecurityManagerException e) {
|
||||
} catch (InvalidSessionException e) {
|
||||
}
|
||||
return userId;
|
||||
return UserSysUtils.getUserId();
|
||||
}
|
||||
|
||||
public static String getUserParamValue() {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.adc.da.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.UnavailableSecurityManagerException;
|
||||
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
||||
import org.apache.shiro.session.InvalidSessionException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserSysUtils {
|
||||
|
||||
private UserSysUtils() {
|
||||
super();
|
||||
}
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(UserSysUtils.class);
|
||||
|
||||
/**
|
||||
* 当前登陆用户
|
||||
*/
|
||||
public static final String CURRENT_USER = "currentUser";
|
||||
|
||||
/**
|
||||
* 角色信息
|
||||
*/
|
||||
public static final String CACHE_ROLE_LIST = "roleList";
|
||||
/**
|
||||
* 菜单信息
|
||||
*/
|
||||
public static final String CACHE_MENU_LIST = "menuList";
|
||||
public static final String CACHE_MENU_TREE = "menuTree";
|
||||
public static final String CACHE_AREA_LIST = "areaList";
|
||||
public static final String CACHE_OFFICE_LIST = "officeList";
|
||||
|
||||
/**
|
||||
* @see JwtSysUtils
|
||||
*/
|
||||
private static JwtSysUtils jwtUtils = SpringContextHolder1.getBean(JwtSysUtils.class);
|
||||
|
||||
/**
|
||||
* 退出
|
||||
*/
|
||||
public static void logout() {
|
||||
try {
|
||||
SecurityUtils.getSubject().logout();
|
||||
} catch (UnavailableSecurityManagerException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
} catch (InvalidSessionException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登陆用户ID
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getUserId() {
|
||||
return jwtUtils.getUserIdByToken();
|
||||
}
|
||||
|
||||
|
||||
public static void flush() {
|
||||
CacheUtils.removeCache(CURRENT_USER);
|
||||
}
|
||||
|
||||
private static final class CacheUtils {
|
||||
|
||||
public static Object getCache(String key) {
|
||||
return getCache(key, null);
|
||||
}
|
||||
|
||||
public static Object getCache(String key, Object defaultValue) {
|
||||
Object obj = getCacheMap().get(key);
|
||||
return obj == null ? defaultValue : obj;
|
||||
}
|
||||
|
||||
public static void putCache(String key, Object value) {
|
||||
getCacheMap().put(key, value);
|
||||
}
|
||||
|
||||
public static void removeCache(String key) {
|
||||
getCacheMap().remove(key);
|
||||
}
|
||||
|
||||
public static Map<String, Object> getCacheMap() {
|
||||
Map<String, Object> map = Maps.newHashMap();
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user