kongzy
2023-11-24 ebe94e19812a1b24257d60831ec932756855e94b
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
package com.gkhy.assess.system.utils;
 
import cn.hutool.core.util.ObjectUtil;
import com.gkhy.assess.system.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
 
public class ShiroUtils {
 
    public static Subject getSubject()
    {
        return SecurityUtils.getSubject();
    }
    public static String getIp()
    {
        String host=getSubject().getSession().getHost();
        if(StringUtils.isEmpty(host)){
            return "";
        }
        return host.substring(0,host.length()>=128?128:host.length());
    }
 
 
    public static Long getUserId()
    {
        return getSysUser().getId().longValue();
    }
 
    public static SysUser getSysUser()
    {
        SysUser user = null;
        Object obj = getSubject().getPrincipal();
        if (ObjectUtil.isNotNull(obj))
        {
            user = new SysUser();
            BeanUtils.copyProperties(obj, user);
        }
        return user;
    }
}