简体   繁体   English

Spring 安全性:在方法中获取经过身份验证的用户

[英]Spring security: get Authenticated user in method

I am trying to get back the current authenticated user in a service class with:我正在尝试在服务 class 中取回当前经过身份验证的用户:

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

but it returns me a String username instead of a user object.但它返回给我一个字符串用户名而不是用户 object。

What should I do to have the user object?我应该怎么做才能拥有用户 object?

An example of how to get the authenticated user in Controller using @AuthenticationPrincipal :如何使用@AuthenticationPrincipal在 Controller 中获取经过身份验证的用户的示例:

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @GetMapping
    public void getUser(@AuthenticationPrincipal User activeUser) {
        System.out.println("user name is "+activeUser.getUsername());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于经过身份验证且未经过身份验证的用户,Spring Security会在休息服务中获取用户信息 - Spring Security get user info in rest service, for authenticated and not authenticated users Spring Security 3检查用户是否通过身份验证 - Spring Security 3 check if user is authenticated Spring-Security:正在优化获取当前经过身份验证的用户… - Spring-Security : Optimizing get currently authenticated user… 从安全上下文中获取当前经过身份验证的用户作为 Spring 缓存的密钥 - Get current authenticated user from security context as key for Spring Cache Spring Security预认证用户登录 - spring security pre authenticated user login Spring Security - 用户在会话销毁时保持身份验证 - Spring Security - User keeps authenticated on session destroy Spring安全匿名用户和具有弹簧安全性的经过身份验证的用户 - spring secuirty anonymous user and authenticated user with spring security 如何在spring security authenticated登录中获取用户输入的用户名和密码值 - How to get the user entered values of username and password in a spring security authenticated login Cometd,春季安全性:当前已通过身份验证的用户在侦听器中不可用 - Cometd, Spring-security : Currently authenticated user not available inside a Listener Spring Security + Thymeleaf-如果未通过身份验证,则向用户隐藏特定数据 - Spring Security + Thymeleaf - hide specific data from user if not authenticated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM