简体   繁体   English

从HttpServletRequest获取目标控制器

[英]Get destination controller from a HttpServletRequest

I have set up spring security to authenticate and authorize requests coming into my application. 我已经设置了spring安全性来认证和授权进入我的应用程序的请求。 I have set up the configuration as so: 我已经将配置设置为:

 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);
                }
            });
        }

I'd like to grab some information about the destination controller that this request was going to go to. 我想获取有关此请求将要到达的目标控制器的一些信息。 The controller isn't actually going to get hit in this scenario because spring security kicked in and threw out the response before it reached the controller. 在这种情况下,控制器实际上并不会受到攻击,因为Spring Security在到达控制器之前就加入了响应并抛出了响应。

Any tips? 有小费吗? Thanks! 谢谢!

Assuming that OAuth2ServerConfiguration is a Spring managed bean, this should work for you. 假设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
  }
}

If unable to Autowire a List of HandlerMapping, Autowire ApplicationContext and adjust as follows. 如果无法自动装配HandlerMapping列表,请自动装配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
  }
}

You could try this: 您可以尝试以下方法:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                return true;
            }

            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
                // handler is the controller
                MyAnnotation annotation = ((HandlerMethod) handler).getMethod().getAnnotation(MyAnnotation.class)
                // do stuff with the annotation
            }
        });
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM