PermissionService.java
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.lhcredit.framework.web.service;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* js调用 thymeleaf 实现按钮权限可见性
*/
@Service("permission")
public class PermissionService {
private static final Logger log = LoggerFactory.getLogger(PermissionService.class);
public String hasPermi(String permission) {
return isPermittedOperator(permission) ? "" : "hidden";
}
public String hasRole(String role) {
return hasRoleOperator(role) ? "" : "hidden";
}
/**
* 判断用户是否拥有某个权限
*
* @param permission 权限字符串
* @return 结果
*/
private boolean isPermittedOperator(String permission) {
return SecurityUtils.getSubject().isPermitted(permission);
}
/**
* 判断用户是否拥有某个角色
*
* @param role 角色字符串
* @return 结果
*/
private boolean hasRoleOperator(String role) {
return SecurityUtils.getSubject().hasRole(role);
}
/**
* 返回用户属性值
*
* @param property 属性名称
* @return 用户属性值
*/
public 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;
}
}