You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
659 B
25 lines
659 B
|
3 months ago
|
package com.project.ding.utils;
|
||
|
|
|
||
|
|
import org.springframework.security.core.Authentication;
|
||
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||
|
|
|
||
|
|
import java.util.Optional;
|
||
|
|
|
||
|
|
public class SecurityUtils {
|
||
|
|
/**
|
||
|
|
* 获取当前登录用户的 ID
|
||
|
|
*/
|
||
|
|
public static String getUserId() {
|
||
|
|
return getAuthentication()
|
||
|
|
.map(auth -> (String) auth.getPrincipal())
|
||
|
|
.orElse(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取当前认证信息
|
||
|
|
*/
|
||
|
|
private static Optional<Authentication> getAuthentication() {
|
||
|
|
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication());
|
||
|
|
}
|
||
|
|
}
|