【问题标题】:Spring: Global Path Variable or Request Parameter without ControllerSpring:没有控制器的全局路径变量或请求参数
【发布时间】:2015-08-09 09:20:41
【问题描述】:

我正在运行一个 Spring Boot 和 Spring Security 应用程序,它现在应该获得多租户支持。

我想让事情尽可能简单。虽然我想通过路径变量或请求参数来确定数据库。 (Spring Boot 应用程序前面的 Apache Web 服务器将处理这个问题,即它从子域映射到路径变量或请求参数)。

现在我需要一种在 Spring 调用控制器之前获取第一个路径变量(或特定请求参数)的方法,当然该值必须存储在某个地方,以便在需要选择正确的数据库时访问它.

所以要么(取决于可能/更容易)

  1. http://localhost:8080/customer1(我更喜欢)或
  2. http://localhost:8080?_customer=customer1

应该简单地用@RequestMapping("") 调用@Controller 并且值customer1 应该存储在某个地方以用于该请求。

我知道2. 可能更简单,因为它已经正确地命中了@Controller,但我更喜欢1.。

谢谢!

编辑:

我刚刚意识到,HandlerInterceptor 没有按预期工作,因为 Spring Security 总是首先处理请求。虽然我需要一个拦截器在 Spring Security 启动之前处理它。

【问题讨论】:

标签: spring spring-mvc spring-security spring-boot spring-4


【解决方案1】:

您可以使用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>

【讨论】:

  • 感谢您的回答,我刚试了一下,发现需要其他类型的拦截器,因为我使用的是Spring Security。我需要拦截器在任何事情之前启动。这意味着:甚至在 Spring Security 之前。你也知道解决方案吗?
  • @BenjaminM Okies。让我再说一遍。但是我很好奇为什么即使问题仍然存在,这个问题也被删除了?
  • 因为我认识到,这个问题与我的问题无关。虽然我无法验证它是否对我必须解决的问题有用。这些拦截器在两者之​​间应用,但我需要在一开始就拦截请求,这导致使用:stackoverflow.com/questions/30487553/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2012-04-11
  • 1970-01-01
  • 2021-09-16
  • 2012-07-30
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多