【发布时间】:2021-10-05 15:10:23
【问题描述】:
我在 Spring Boot @RestController 中有一些方法,当我将它们添加为参数时,它们神奇地似乎可以访问 Spring Security Authentication 或 Principal 对象。我想知道,我的方法如何处理这些参数?他们来自哪里?
这是一个例子:
@GetMapping("/someEndpoint")
public ObjectNode someEndpoint(Authentication authentication) {
...
CustomAuthentication customAuthentication = (CustomAuthentication) authentication;
logger.debug("Name: {}", customAuthentication.getName());
...
}
或
@GetMapping("/anotherEndpoint)
public ObjectNode anotherEndpoint(Principal principal) {
logger.debug(principal.getName());
...
}
不仅仅是Authentication 和Principal 对象。我有时在这些控制器端点中也看到了HttpServletRequest 和其他参数。它们来自哪里,为什么它们是可选的?我可以在我的方法中获取这些对象的列表吗?
【问题讨论】:
-
这个答案回答了这个问题:stackoverflow.com/a/29410196/1191849
标签: spring-boot spring-mvc spring-security