other thing

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-11 12:27:49 +08:00
parent 1ab1a4ad8a
commit 8966f306df
2 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package cyou.chenx221.helper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
public class UsernameHelper {
public UsernameHelper() {
}
// 获取当前登录的用户名
public String getCurrentUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return userDetails.getUsername();
}
return null;
}
}