2023-02-16 14:17:36 +08:00
|
|
|
package com.zhgd.xmgl.security;
|
|
|
|
|
|
|
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
@UtilityClass
|
|
|
|
|
public class SecurityUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取Authentication
|
|
|
|
|
*/
|
|
|
|
|
public Authentication getAuthentication() {
|
|
|
|
|
return SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户
|
|
|
|
|
* @param authentication
|
|
|
|
|
* <p>
|
|
|
|
|
*/
|
2023-03-04 18:30:12 +08:00
|
|
|
private SecurityUser getUser(Authentication authentication) {
|
2023-02-16 14:17:36 +08:00
|
|
|
Object principal = authentication.getPrincipal();
|
|
|
|
|
if (principal instanceof SecurityUser) {
|
|
|
|
|
return (SecurityUser) principal;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户
|
|
|
|
|
*/
|
|
|
|
|
public SecurityUser getUser() {
|
|
|
|
|
Authentication authentication = getAuthentication();
|
2023-05-05 17:50:58 +08:00
|
|
|
return authentication == null ? null : getUser(authentication);
|
2023-02-16 14:17:36 +08:00
|
|
|
}
|
|
|
|
|
}
|