2016-12-15 5 views
2

Я установил весеннюю безопасность для аутентификации и авторизации запросов, поступающих в мое приложение. Я настроил конфигурацию, как так:Получить контроллер назначения из HttpServletRequest

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

Я хотел бы, чтобы захватить некоторую информацию о контроллере назначения, что этот запрос собирался пойти. Контроллер на самом деле не попадет в этот сценарий, потому что весенняя безопасность взорвалась и выбросила ответ до того, как он дошел до контроллера.

Любые советы? Спасибо!

ответ

3

Предполагая, что OAuth2ServerConfiguration является управляемым компонентом Spring, это должно работать на вас.

... 

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

В случае невозможности Autowire списка HandlerMapping, Autowire 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 
    } 
} 
+1

Это дает мне ошибку говоря – Jeff

+2

@Jeff, посмотреть мои ammendements –

+0

Это работал - хорошая работа! – Jeff

1

Вы могли бы попробовать это: "Не удалось autowire Нет бобов HandlerMapping или List найденные"

@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 
      } 
     }); 
    } 
} 
Смежные вопросы