新增getPrincipalProperty方法,用于前端及其他模块获取当前用户信息
This commit is contained in:
@ -1,6 +1,13 @@
|
||||
package com.ruoyi.common.utils.security;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.ruoyi.common.constant.PermissionConstants;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
|
||||
@ -11,6 +18,8 @@ import com.ruoyi.common.utils.MessageUtils;
|
||||
*/
|
||||
public class PermissionUtils
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(PermissionUtils.class);
|
||||
|
||||
/**
|
||||
* 查看数据的权限
|
||||
*/
|
||||
@ -74,4 +83,36 @@ public class PermissionUtils
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回用户属性值
|
||||
*
|
||||
* @param property 属性名称
|
||||
* @return 用户属性值
|
||||
*/
|
||||
public static Object getPrincipalProperty(String property)
|
||||
{
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
if (subject != null)
|
||||
{
|
||||
Object principal = subject.getPrincipal();
|
||||
try
|
||||
{
|
||||
BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
|
||||
for (PropertyDescriptor pd : bi.getPropertyDescriptors())
|
||||
{
|
||||
if (pd.getName().equals(property) == true)
|
||||
{
|
||||
return pd.getReadMethod().invoke(principal, (Object[]) null);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Error reading property [{}] from principal of type [{}]", property,
|
||||
principal.getClass().getName());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user