Blame view

src/main/java/com/huaheng/common/utils/security/ShiroUtils.java 4.86 KB
tangying authored
1
2
package com.huaheng.common.utils.security;
周鸿 authored
3
4
5
6
import java.util.List;

import javax.annotation.Resource;
tangying authored
7
import org.apache.shiro.SecurityUtils;
周鸿 authored
8
import org.apache.shiro.authc.UsernamePasswordToken;
tangying authored
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;
周鸿 authored
14
15
16
17

import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.ZoneAreaUtils;
import com.huaheng.common.utils.bean.BeanUtils;
tangying authored
18
import com.huaheng.framework.shiro.realm.UserRealm;
周鸿 authored
19
20
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.system.dept.domain.Dept;
tangying authored
21
22
23
24
25
26
import com.huaheng.pc.system.user.domain.User;

/**
 * shiro 工具类
 * @author huaheng
 */
周鸿 authored
27
public class ShiroUtils {
tangying authored
28
29
30
31
    @Resource
    private WarehouseService warehouseService;
周鸿 authored
32
    public static Subject getSubjct() {
tangying authored
33
34
35
        return SecurityUtils.getSubject();
    }
周鸿 authored
36
    public static Session getSession() {
tangying authored
37
38
39
        return SecurityUtils.getSubject().getSession();
    }
周鸿 authored
40
    public static void logout() {
tangying authored
41
42
43
        getSubjct().logout();
    }
周鸿 authored
44
    public static User getUser() {
45
46
        try {
            Object obj = getSubjct().getPrincipal();
周鸿 authored
47
            if (StringUtils.isNotNull(obj)) {
48
49
50
                User user = new User();
                BeanUtils.copyBeanProp(user, obj);
                return user;
周鸿 authored
51
            } else {
52
53
                return null;
            }
周鸿 authored
54
        } catch (Exception e) {
tangying authored
55
56
57
58
            return null;
        }
    }
周鸿 authored
59
    public static void setUser(User user) {
tangying authored
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);
    }
周鸿 authored
68
69
70
    public static void clearCachedAuthorizationInfo() {
        RealmSecurityManager rsm = (RealmSecurityManager)SecurityUtils.getSecurityManager();
        UserRealm realm = (UserRealm)rsm.getRealms().iterator().next();
tangying authored
71
72
73
        realm.clearCachedAuthorizationInfo();
    }
周鸿 authored
74
    public static Integer getUserId() {
tangying authored
75
76
77
        return getUser().getId();
    }
周鸿 authored
78
79
    public static String getLoginName() {
        if (getUser() == null) {
80
81
            return null;
        }
tangying authored
82
83
84
        return getUser().getLoginName();
    }
周鸿 authored
85
86
    public static String getWarehouseCode() {
        return getUser().getWarehouseCode();
tangying authored
87
88
    }
周鸿 authored
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);
    }
周鸿 authored
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);
周鸿 authored
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
周鸿 authored
121
122
123
    public static String getZoneCode() {
        return getUser().getZoneCode();
    }
124
周鸿 authored
125
126
127
    public static String getZoneName() {
        return getUser().getZoneName();
    }
128
129
130
131
132
    public static void setZoneCode(String zoneCode) {
        getUser().setZoneCode(zoneCode);
    }
周鸿 authored
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();
    }
周鸿 authored
141
    public static List<Integer> getCompanyIdList() {
tangying authored
142
143
144
        return getUser().getCompanyIdList();
    }
周鸿 authored
145
    public static List<String> getCompanyCodeList() {
tangying authored
146
147
148
        return getUser().getCompanyCodeList();
    }
周鸿 authored
149
    public static String getIp() {
tangying authored
150
151
152
        return getSubjct().getSession().getHost();
    }
周鸿 authored
153
    public static String getSessionId() {
tangying authored
154
155
        return String.valueOf(getSubjct().getSession().getId());
    }
156
周鸿 authored
157
    public static String getCompanyCode() {
158
159
160

        return getUser().getCompanyCodeList().get(0);
    }
tangying authored
161
}