|
1
2
|
package com.huaheng.common.utils.security;
|
|
3
4
5
6
|
import java.util.List;
import javax.annotation.Resource;
|
|
7
|
import org.apache.shiro.SecurityUtils;
|
|
8
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
|
9
10
11
12
13
|
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;
|
|
14
15
16
17
|
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.ZoneAreaUtils;
import com.huaheng.common.utils.bean.BeanUtils;
|
|
18
|
import com.huaheng.framework.shiro.realm.UserRealm;
|
|
19
20
|
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.system.dept.domain.Dept;
|
|
21
22
23
24
25
26
|
import com.huaheng.pc.system.user.domain.User;
/**
* shiro 工具类
* @author huaheng
*/
|
|
27
|
public class ShiroUtils {
|
|
28
|
|
|
29
30
31
|
@Resource
private WarehouseService warehouseService;
|
|
32
|
public static Subject getSubjct() {
|
|
33
34
35
|
return SecurityUtils.getSubject();
}
|
|
36
|
public static Session getSession() {
|
|
37
38
39
|
return SecurityUtils.getSubject().getSession();
}
|
|
40
|
public static void logout() {
|
|
41
42
43
|
getSubjct().logout();
}
|
|
44
|
public static User getUser() {
|
|
45
46
|
try {
Object obj = getSubjct().getPrincipal();
|
|
47
|
if (StringUtils.isNotNull(obj)) {
|
|
48
49
50
|
User user = new User();
BeanUtils.copyBeanProp(user, obj);
return user;
|
|
51
|
} else {
|
|
52
53
|
return null;
}
|
|
54
|
} catch (Exception e) {
|
|
55
56
57
58
|
return null;
}
}
|
|
59
|
public static void setUser(User user) {
|
|
60
61
62
63
64
65
66
67
|
Subject subject = getSubjct();
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
}
|
|
68
69
70
|
public static void clearCachedAuthorizationInfo() {
RealmSecurityManager rsm = (RealmSecurityManager)SecurityUtils.getSecurityManager();
UserRealm realm = (UserRealm)rsm.getRealms().iterator().next();
|
|
71
72
73
|
realm.clearCachedAuthorizationInfo();
}
|
|
74
|
public static Integer getUserId() {
|
|
75
76
77
|
return getUser().getId();
}
|
|
78
79
|
public static String getLoginName() {
if (getUser() == null) {
|
|
80
81
|
return null;
}
|
|
82
83
84
|
return getUser().getLoginName();
}
|
|
85
86
|
public static String getWarehouseCode() {
return getUser().getWarehouseCode();
|
|
87
88
|
}
|
|
89
90
|
public static void setUserByWcs(String loginname, String warehouseCode, String area) {
User user = new User();
|
|
91
92
|
user.setWarehouseCode(warehouseCode);
user.setLoginName(loginname);
|
|
93
|
user.setArea(area);
|
|
94
95
|
ShiroUtils.setUser(user);
}
|
|
96
97
98
|
public static void setUserByWcsZone(String loginname, String warehouseCode, String zoneCode) {
User user = new User();
|
|
99
100
101
|
user.setWarehouseCode(warehouseCode);
user.setLoginName(loginname);
user.setZoneCode(zoneCode);
|
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
user.setId(0);
Dept dept = new Dept();
dept.setDeptName("11");
user.setDept(dept);
// user.setr
ShiroUtils.setUser1(user);
}
public static void setUser1(User user) {
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("test", "123456", false, "1111");
subject.login(token);
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
|
|
119
|
}
|
|
120
|
|
|
121
122
123
|
public static String getZoneCode() {
return getUser().getZoneCode();
}
|
|
124
|
|
|
125
126
127
|
public static String getZoneName() {
return getUser().getZoneName();
}
|
|
128
|
|
|
129
130
131
132
|
public static void setZoneCode(String zoneCode) {
getUser().setZoneCode(zoneCode);
}
|
|
133
134
135
|
public static String getArea() {
String zoneCode = getUser().getZoneCode();
if (StringUtils.isNotEmpty(zoneCode)) {
|
|
136
137
138
139
140
|
return ZoneAreaUtils.getAreaByZoneCode(zoneCode).toString();
}
return getUser().getArea();
}
|
|
141
|
public static List<Integer> getCompanyIdList() {
|
|
142
143
144
|
return getUser().getCompanyIdList();
}
|
|
145
|
public static List<String> getCompanyCodeList() {
|
|
146
147
148
|
return getUser().getCompanyCodeList();
}
|
|
149
|
public static String getIp() {
|
|
150
151
152
|
return getSubjct().getSession().getHost();
}
|
|
153
|
public static String getSessionId() {
|
|
154
155
|
return String.valueOf(getSubjct().getSession().getId());
}
|
|
156
|
|
|
157
|
public static String getCompanyCode() {
|
|
158
159
160
|
return getUser().getCompanyCodeList().get(0);
}
|
|
161
|
}
|