|
1
2
3
4
|
package com.huaheng.common.utils.security;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
|
|
5
6
|
import com.huaheng.pc.config.warehouse.domain.Warehouse;
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
|
import javax.annotation.Resource;
|
|
17
18
19
20
21
22
23
24
25
26
|
import java.util.List;
/**
* shiro 工具类
*
* @author huaheng
*/
public class ShiroUtils
{
|
|
27
28
29
|
@Resource
private WarehouseService warehouseService;
|
|
30
31
|
public static String warehouseCode;
|
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public static Subject getSubjct()
{
return SecurityUtils.getSubject();
}
public static Session getSession()
{
return SecurityUtils.getSubject().getSession();
}
public static void logout()
{
getSubjct().logout();
}
public static User getUser()
{
|
|
49
50
51
52
53
54
55
56
57
58
59
|
try {
Object obj = getSubjct().getPrincipal();
if (StringUtils.isNotNull(obj)) {
User user = new User();
BeanUtils.copyBeanProp(user, obj);
return user;
}
else {
return null;
}
} catch (Exception e){
|
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
return null;
}
}
public static void setUser(User user)
{
Subject subject = getSubjct();
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
}
public static void clearCachedAuthorizationInfo()
{
RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
realm.clearCachedAuthorizationInfo();
}
public static Integer getUserId()
{
|
|
83
84
85
|
if(getUser() == null) {
return null;
}
|
|
86
87
88
89
90
|
return getUser().getId();
}
public static String getLoginName()
{
|
|
91
92
93
|
if(getUser() == null) {
return null;
}
|
|
94
95
96
|
return getUser().getLoginName();
}
|
|
97
98
99
100
101
102
103
104
|
public static String getUserName()
{
if(getUser() == null) {
return null;
}
return getUser().getUserName();
}
|
|
105
106
|
public static String getWarehouseCode()
{
|
|
107
108
109
|
if(getUser()==null){
return warehouseCode;
}
|
|
110
|
return getUser().getWarehouseCode();
|
|
111
112
113
114
115
116
117
118
119
120
121
|
}
public static List<Integer> getCompanyIdList()
{
return getUser().getCompanyIdList();
}
public static List<String> getCompanyCodeList()
{
return getUser().getCompanyCodeList();
}
|
|
122
123
124
125
126
127
128
|
public static String getCompanyCode()
{
if(getUser()!=null&&getUser().getCompanyCodeList()!=null){
return getUser().getCompanyCodeList().get(0);
}
return null;
}
|
|
129
130
131
132
133
134
135
136
137
138
139
|
public static String getIp()
{
return getSubjct().getSession().getHost();
}
public static String getSessionId()
{
return String.valueOf(getSubjct().getSession().getId());
}
}
|