【问题标题】:Get destination controller from a HttpServletRequest从 HttpServletRequest 获取目标控制器
【发布时间】:2017-05-01 11:07:19
【问题描述】:

我已经设置了 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);
                }
            });
        }

我想获取有关此请求将要到达的目标控制器的一些信息。在这种情况下,控制器实际上不会受到影响,因为弹簧安全性在响应到达控制器之前启动并抛出了响应。

有什么建议吗? 谢谢!

【问题讨论】:

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


    【解决方案1】:

    假设 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
      }
    }
    

    如果无法自动装配一个 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
      }
    }
    

    【讨论】:

    • 这给了我一个错误提示“无法自动装配。找不到 HandlerMapping 或 List 的bean”
    • @Jeff,看看我的修正
    【解决方案2】:

    你可以试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2020-01-13
      • 2012-12-06
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多