java - 从 HttpServletRequest 获取目标 Controller

标签 java spring spring-security spring-security-oauth2

我已经设置了 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/

相关文章:

java - 基于 Maven 配置文件加载属性?

java - 找不到 Spring Security : WebSecurityConfigurerAdapter. 类

java - 如何添加有关用户未通过身份验证的原因的详细信息?

css - 更改 Web 应用程序的 URL 后不再识别 Layout.css

Java Path Finder for Bytecode Verification in an existing project,字节码验证

java - 自定义异常类应该继承Exception还是Throwable?

java - 注释指定的 bean 名称与现有的、不兼容的 bean 定义冲突

java - 奇怪的 "18 chars"LOGBack 行为

java - 删除动态链表的中间节点(如果存在)

Java + Spring 启动 : I am trying to add CacheControl header to ResponseEntity