|
1
2
|
package com.huaheng.common.utils.security;
|
|
3
4
5
6
|
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
|
|
7
8
9
10
11
12
13
14
15
|
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import com.huaheng.framework.shiro.realm.UserRealm;
import com.huaheng.pc.system.user.domain.User;
|
|
16
17
18
|
import javax.annotation.Resource;
import java.util.List;
|
|
19
20
|
/**
* shiro 工具类
|
|
21
|
*
|
|
22
23
|
* @author huaheng
*/
|
|
24
|
public class ShiroUtils {
|
|
25
|
|
|
26
27
28
|
@Resource
private WarehouseService warehouseService;
|
|
29
|
public static Subject getSubjct() {
|
|
30
31
32
|
return SecurityUtils.getSubject();
}
|
|
33
|
public static Session getSession() {
|
|
34
35
36
|
return SecurityUtils.getSubject().getSession();
}
|
|
37
|
public static void logout() {
|
|
38
39
40
|
getSubjct().logout();
}
|
|
41
|
public static User getUser() {
|
|
42
43
|
try {
Object obj = getSubjct().getPrincipal();
|
|
44
|
if (StringUtils.isNotNull(obj)) {
|
|
45
46
47
|
User user = new User();
BeanUtils.copyBeanProp(user, obj);
return user;
|
|
48
|
} else {
|
|
49
50
|
return null;
}
|
|
51
|
} catch (Exception e) {
|
|
52
53
54
55
|
return null;
}
}
|
|
56
|
public static void setUser(User user) {
|
|
57
58
59
60
61
62
63
64
|
Subject subject = getSubjct();
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
}
|
|
65
|
public static void clearCachedAuthorizationInfo() {
|
|
66
67
|
RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
|
|
68
69
70
|
realm.clearCachedAuthorizationInfo();
}
|
|
71
|
public static Integer getUserId() {
|
|
72
73
74
|
return getUser().getId();
}
|
|
75
76
|
public static String getLoginName() {
if (getUser() == null) {
|
|
77
78
|
return null;
}
|
|
79
80
81
|
return getUser().getLoginName();
}
|
|
82
83
84
85
86
87
88
|
public static String getName() {
if (getUser() == null) {
return null;
}
return getUser().getEmail();
}
|
|
89
|
public static String getWarehouseCode() {
|
|
90
91
92
|
if (null == getUser()) {
return QuantityConstant.DEFAULT_WAREHOUSE;
}
|
|
93
|
return getUser().getWarehouseCode();
|
|
94
95
|
}
|
|
96
97
98
99
|
public static String getWarehouseCodeCS0001() {
return "CS0001";
}
|
|
100
|
public static List<Integer> getCompanyIdList() {
|
|
101
102
103
|
return getUser().getCompanyIdList();
}
|
|
104
|
public static List<String> getCompanyCodeList() {
|
|
105
106
107
|
return getUser().getCompanyCodeList();
}
|
|
108
|
public static String getIp() {
|
|
109
110
111
|
return getSubjct().getSession().getHost();
}
|
|
112
|
public static String getSessionId() {
|
|
113
114
115
|
return String.valueOf(getSubjct().getSession().getId());
}
}
|