我有一个Spring & Security应用程序正在运行,现在应该可以获得多租户支持。
我想让事情尽可能简单。尽管我想通过路径变量或请求参数来确定数据库。( Spring应用程序前面的将处理这个问题,即它将从子域映射到路径变量或请求参数)。
现在,我需要一种在Spring调用控制器之前获取第一个路径变量(或特定请求param)的方法,当然,该值必须存储在某个地方,因此当我需要选择正确的数据库时,我可以访问它。
所以要么(取决于什么是可能的/更容易的)
http://localhost:8080/customer1 (我更喜欢)或http://localhost:8080?_customer=customer1应该简单地用@RequestMapping("")调用@RequestMapping(""),并且值customer1应该存储在该请求的某个地方。
我知道2.可能更简单,因为它已经达到了正确的@Controller,但我还是更喜欢1.。
谢谢!
编辑:
我刚刚意识到,HandlerInterceptor并不像预期的那样工作,因为Security总是首先处理请求。虽然我需要一个拦截器来处理它,在Spring安全开始之前。
发布于 2015-05-27 11:24:53
您可以使用org.springframework.web.servlet.HandlerInterceptor。通过preHandle方法实现在请求中存储值的逻辑。
在spring配置文件中配置它如下所示
<mvc:interceptors>
<bean class="com.blah.interceptor.SomeInterceptor"/>
</mvc:interceptors> 如果要根据路径拦截请求,请使用下面的配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/customer1" />
<bean class="com.blah.interceptor.SomeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>https://stackoverflow.com/questions/30479928
复制相似问题