【发布时间】:2014-01-10 00:48:59
【问题描述】:
我知道这可能不是解决此类问题的最佳设计,但仅适用于特定要求。
当前应用程序需要ServletContext、HttpServletRequest、HttpServletResponse 进入服务层以获得customized authentication provider。
显然没有任何特定的配置或继承以下代码:
@Component("myAuthenticaionProvider")
public class MyAuthenticaionProvider implements AuthenticationUserDetailsService {
@Autowired private ServletContext context;
@Autowired private HttpServletRequest request;
@Autowired private HttpServletResponse response;
.......
}
必须抛出异常:
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [javax.servlet.http.HttpServletResponse] found for dependency:
我能想到的可能解决方案:
使用过滤器拦截
HttpServletRequest,但这需要 URL 模式,否则会拦截我认为可能存在性能问题的所有 URL?在 spring-security.xml 或 application-context.xml 中创建一个
request范围 bean,然后注入当前的身份验证提供程序类,使其能够获取HttpServletRequest。但是我觉得这里有问题,如何发起请求范围bean?
那么最好的做法是什么?
【问题讨论】:
-
你到底想达到什么目的?
-
这是个坏主意,但在适当的环境中它会起作用。
((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();。 适当的环境是指getRequestAttributes()返回的RequestAttributes是ServletWebRequest类型的环境。 -
如果您描述了您想要实现的目标,获得一个好的解决方案的机会会高得多。对我来说,您似乎需要一个过滤器(尤其是因为响应对象),但您不想要那个。所以我真的很想知道谁调用了这项服务以及何时(以及为什么)。
标签: java spring spring-mvc