Spring Boot Security 获取 Authentication 身份信息

Spring Security Spring Boot About 559 words

需求

已经认证的用户需要能从Controller中获取。

代码

ThreadLocal

SecurityContextHolder.getContext().getAuthentication().getPrincipal();

Controller

@PutMapping("/api/user")
public void updateUser(@RequestBody UserInfoRequest request, Authentication authentication) {
    userService.updateUserInfo(authentication.getPrincipal(), request);
}

自定义Authentication,如:TokenAuthentication

@PutMapping("/api/user")
public void updateUser(@RequestBody UserInfoRequest request, TokenAuthentication authentication) {
    userService.updateUserInfo(authentication.getPrincipal(), request);
}
Views: 504 · Posted: 2024-05-17

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh