gpt4 book ai didi

java - 如何在 Spring MVC REST channel 中获取登录的用户名/主体?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:04:52 26 4
gpt4 key购买 nike

我有 Spring MVC REST channel :

@Controller
@RequestMapping("/rest")
public class REST {

我有我的方法:

@RequestMapping(value = "/doSomething")
public @ResponseBody DoSomethingResultDTO doSomething(
@RequestBody DoSomethingRequestDTO)

现在我需要登录的用户名。通常我可以通过方法来完成

HttpServletRequest.getUserPrincipal()

但是这里怎么得到呢?我有 header (@RequestHeader) 甚至 cookie (@CookieValue) 的注释。但是如何在我的方法中获取 Principal

最佳答案

您可以将 Principal 对象注入(inject)到您的 Controller 处理程序方法中

@RequestMapping(value = "/doSomething")
public @ResponseBody DoSomethingResultDTO doSomething(
@RequestBody DoSomethingRequestDTO, Principal principal)

参见 the spring reference manual for more info

关于java - 如何在 Spring MVC REST channel 中获取登录的用户名/主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17720488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com