2016-12-15 3 views
2

私は春のセキュリティを設定して、アプリケーションに入ってくるリクエストを認証して認証します。私は次のように構成を設定しました。HttpServletRequestから宛先コントローラを取得

このリクエストが送信先コントローラにいくつかの情報を取得したいと思います。コントローラは実際にこのシナリオでヒットすることはありません。なぜなら、春のセキュリティがコントローラに到達する前にレスポンスを蹴り捨てたからです。

ヒント? ありがとう!

答えて

3

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、Autowire ApplicationContextのリストをAutowireできない場合は、次のように調整します。

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 
      } 
     }); 
    } 
} 
関連する問題