Java获取当前用户信息

	@UtilityClass
public class SecurityUtils {


    /**
     * 获取用户
     */
    public SysUser getSysUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null) {
            throw new UnauthorizedException("无法获取用户信息");
        }

        if (!(authentication instanceof OAuth2Authentication)) {
            throw new UnauthorizedException("无法获取用户信息");
        }
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)authentication;

        YamiSysUser details = (YamiSysUser) oAuth2Authentication.getUserAuthentication().getDetails();
        if (details == null) {
            throw new UnauthorizedException("无法获取用户信息");
        }
        return details;
    }
}

版权声明:本文为T_yuqing原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。