wuyouming666
2023-12-11 a487e1560cddf1d8b3e7372d9791b9589e9af96c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.springboot.security.util;
 
import com.example.springboot.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
 
public class SecurityUtil {
    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static User getCurrentUser() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated() && !subject.isRemembered()) {
            throw new RuntimeException("Log current user error: UnAuthenticated subject");
        }
        return (User) subject.getPrincipal();
    }
}