Blame view

src/main/java/com/huaheng/common/utils/security/ShiroUtils.java 3.22 KB
tangying authored
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;
tangying authored
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;
tangying authored
19
20
/**
 * shiro 工具类
易文鹏 authored
21
 *
tangying authored
22
23
 * @author huaheng
 */
24
public class ShiroUtils {
tangying authored
25
26
27
28
    @Resource
    private WarehouseService warehouseService;
29
    public static Subject getSubjct() {
tangying authored
30
31
32
        return SecurityUtils.getSubject();
    }
33
    public static Session getSession() {
tangying authored
34
35
36
        return SecurityUtils.getSubject().getSession();
    }
37
    public static void logout() {
tangying authored
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) {
tangying authored
52
53
54
55
            return null;
        }
    }
56
    public static void setUser(User user) {
tangying authored
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();
tangying authored
68
69
70
        realm.clearCachedAuthorizationInfo();
    }
71
    public static Integer getUserId() {
tangying authored
72
73
74
        return getUser().getId();
    }
75
76
    public static String getLoginName() {
        if (getUser() == null) {
77
78
            return null;
        }
tangying authored
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();
tangying authored
94
95
    }
易文鹏 authored
96
97
98
99
    public static String getWarehouseCodeCS0001() {
        return "CS0001";
    }
100
    public static List<Integer> getCompanyIdList() {
tangying authored
101
102
103
        return getUser().getCompanyIdList();
    }
104
    public static List<String> getCompanyCodeList() {
tangying authored
105
106
107
        return getUser().getCompanyCodeList();
    }
108
    public static String getIp() {
tangying authored
109
110
111
        return getSubjct().getSession().getHost();
    }
112
    public static String getSessionId() {
tangying authored
113
114
115
        return String.valueOf(getSubjct().getSession().getId());
    }
}