whyczh
2021-09-08 8c51c3807befed873a7ab86c0e1e52dd189a8b3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * <b>文件名</b>:ShiroUtil.java
 *
 * 文件描述:
 *
 *
 * 2017-10-11  下午2:28:25
 */
 
package com.whyc.util;
 
import com.mysql.cj.core.util.StringUtils;
import com.whyc.constant.SuperConstant;
import com.whyc.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
 
 
/**
 * @Description shiro工具类
 */
public class ShiroUtil {
    
    /**
     * @Description 获得shiro的session
     * @param
     * @return
     */
    public static Session getShiroSession() {
        return SecurityUtils.getSubject().getSession();
    }
    
    /**
     * @Description 获得shiro的sessionId
     * @param
     * @return
     */
    public static String getShiroSessionId() {
        return getShiroSession().getId().toString();
    }
    
    /**
     * @Description 是否登陆
     * @param
     * @return
     */
    public static Boolean isAuthenticated(){
        Subject subject = SecurityUtils.getSubject();
        return subject.isAuthenticated();
    }
 
    public static User getUser() {
        if (!isNullOrEmpty(ThreadContext.getSubject()) && !isNullOrEmpty(SecurityUtils.getSubject().getPrincipal())) {
            return (User) SecurityUtils.getSubject().getPrincipal();
        }else {
            return new User(0,"none");
        }
    }
 
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null || "".equals(obj)) {
            return true;
        } else {
            return false;
        }
    }
}