新增getPrincipalProperty方法,用于前端及其他模块获取当前用户信息

master
RuoYi 5 years ago committed by Limy
parent bdd6e54918
commit 5b49dbe524

@ -29,6 +29,12 @@
<artifactId>spring-web</artifactId>
</dependency>
<!--Shiro核心框架 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
@ -78,7 +84,7 @@
</dependency>
<!-- servlet包 -->
<dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>

@ -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;
}
}

@ -2,6 +2,7 @@ package com.ruoyi.framework.web.service;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.security.PermissionUtils;
/**
* RuoYi js thymeleaf
@ -43,4 +44,14 @@ public class PermissionService
return SecurityUtils.getSubject().hasRole(role);
}
/**
*
*
* @param property
* @return
*/
public Object getPrincipalProperty(String property)
{
return PermissionUtils.getPrincipalProperty(property);
}
}

@ -17,12 +17,6 @@
<dependencies>
<!--Shiro核心框架 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<!--velocity代码生成使用模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>

@ -17,12 +17,6 @@
<dependencies>
<!--Shiro核心框架 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<!-- 定时任务 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>

@ -6,6 +6,7 @@
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-job-add">
<input type="hidden" name="createBy" th:value="${@permission.getPrincipalProperty('loginName')}">
<div class="form-group">
<label class="col-sm-3 control-label">任务名称:</label>
<div class="col-sm-8">

@ -7,6 +7,7 @@
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-job-edit" th:object="${job}">
<input id="jobId" name="jobId" type="hidden" th:field="*{jobId}"/>
<input type="hidden" name="updateBy" th:value="${@permission.getPrincipalProperty('loginName')}">
<div class="form-group">
<label class="col-sm-3 control-label">任务名称:</label>
<div class="col-sm-8">

Loading…
Cancel
Save