Get destination controller from a HttpServletRequest(从HttpServletRequest中获取目标控制器)
                            本文介绍了从HttpServletRequest中获取目标控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我已经设置了Spring安全性来对进入应用程序的请求进行身份验证和授权。我已按如下方式设置配置:
 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);
                }
            });
        }
我想获取有关此请求要发送到的目标控制器的一些信息。在这种情况下,控制器实际上不会受到攻击,因为Spring Security在响应到达控制器之前启动并抛出响应。
有什么建议吗? 谢谢!
推荐答案
假定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
  }
}
如果无法自动生成HandlerMap列表,请自动生成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
  }
}
                        这篇关于从HttpServletRequest中获取目标控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:从HttpServletRequest中获取目标控制器
				
        
 
            
        
             猜你喜欢
        
	     - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 
						
						
						
						
						
				
				
				
				