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

dep_wangsr
RuoYi 6 years ago committed by Limy
parent bdd6e54918
commit 5b49dbe524

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

@ -1,6 +1,13 @@
package com.ruoyi.common.utils.security; 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.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.constant.PermissionConstants;
import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.MessageUtils;
@ -11,6 +18,8 @@ import com.ruoyi.common.utils.MessageUtils;
*/ */
public class PermissionUtils public class PermissionUtils
{ {
private static final Logger log = LoggerFactory.getLogger(PermissionUtils.class);
/** /**
* *
*/ */
@ -74,4 +83,36 @@ public class PermissionUtils
} }
return msg; 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.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.security.PermissionUtils;
/** /**
* RuoYi js thymeleaf * RuoYi js thymeleaf
@ -43,4 +44,14 @@ public class PermissionService
return SecurityUtils.getSubject().hasRole(role); return SecurityUtils.getSubject().hasRole(role);
} }
/**
*
*
* @param property
* @return
*/
public Object getPrincipalProperty(String property)
{
return PermissionUtils.getPrincipalProperty(property);
}
} }

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

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

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

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

Loading…
Cancel
Save