gpt4 book ai didi

java - 从 HttpServletRequest 获取目标 Controller

转载 作者:行者123 更新时间:2023-11-30 06:50:40 25 4
gpt4 key购买 nike

我已经设置了 spring security 来验证和授权进入我的应用程序的请求。我已经这样设置了配置:

 public class OAuth2ServerConfiguration extends ResourceServerConfigurerAdapter {

@Override
public void configure(ResourceServerSecurityConfigurer resources) {

// ...set up token store here

resources.authenticationEntryPoint(new AuthenticationEntryPoint() {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

//QUESTION
// How do I get the destination controller that this request was going to go to?
// Really, I'd like to get some information about the annotations that were on the destination controller.

response.setStatus(401);
}
});
}

我想获取有关此请求将要到达的目标 Controller 的一些信息。在这种情况下, Controller 实际上不会受到攻击,因为 spring security 启动并在响应到达 Controller 之前将其丢弃。

有什么建议吗?谢谢!

最佳答案

假设 OAuth2ServerConfiguration 是一个 Spring 托管的 bean,这应该适合您。

...

@Autowired
private List<HandlerMapping> handlerMappings;

for (HandlerMapping handlerMapping : handlerMappings) {
HandlerExecutionChain handlerExecutionChain = handlerMapping.getHandler(request);
if (handlerExecutionChain != null) {
// handlerExecutionChain.getHandler() is your handler for this request
}
}

如果无法 Autowiring HandlerMapping 列表,请 Autowiring ApplicationContext 并进行如下调整。

for (HandlerMapping handlerMapping : applicationContext.getBeansOfType(HandlerMapping.class).values()) {
HandlerExecutionChain handlerExecutionChain = handlerMapping.getHandler(request);
if (handlerExecutionChain != null) {
// handlerExecutionChain.getHandler() is your handler for this request
}
}

关于java - 从 HttpServletRequest 获取目标 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171065/

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