57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
package com.adc.da.util;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.apache.shiro.UnavailableSecurityManagerException;
|
|
import org.apache.shiro.session.InvalidSessionException;
|
|
import org.apache.shiro.subject.Subject;
|
|
|
|
import java.util.Map;
|
|
|
|
public class LoginUserUtil {
|
|
/**
|
|
* 获取当前登录用户ID
|
|
*
|
|
*/
|
|
public static String getUserId() {
|
|
String userId = "WY8J26MH23";
|
|
try {
|
|
if(!StringUtils.isNotBlank(userId)){
|
|
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;
|
|
}
|
|
|
|
public static String getUserParamValue() {
|
|
String paramValue = 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);
|
|
paramValue=userInfo.get("paramValue").toString();
|
|
}
|
|
}
|
|
} catch (UnavailableSecurityManagerException e) {
|
|
} catch (InvalidSessionException e) {
|
|
}
|
|
return paramValue;
|
|
}
|
|
|
|
|
|
}
|