排除Spring Interceptor中的兄弟路径

时间:2015-03-31 18:13:43

标签: spring-mvc spring-boot interceptor

我有一个拦截器" SessionInterceptor"确保我的服务用户有会话。

我已将这个拦截器应用于一个类:

@Configuration
public class CustomConfiguration extends WebMvcConfigurerAdapter {

public void addInterceptors(InterceptorRegistry registry) {

/*
* SessionInterceptor ensures that all requests received have a valid and
* current session ID in their header.
* 
* This interceptor applies to all paths excluding the /login and /login sub directories.
*/
InterceptorRegistration sessIDReg = registry.addInterceptor(new SessionInterceptor());
sessIDReg.addPathPatterns("/**");
sessIDReg.excludePathPatterns("/login/**");

}

出于某种原因,每当我调用/ login,/ login / something等时,我总是从SessionInterceptor收到一条消息(" No session,...")。

我尝试过使用类级和方法级的RequestMapping注释,但无论如何都会得到相同的结果。

以www.website.com/dir1/subdir为例,这是我希望能够做到的:

除了登录及其子目录之外的所有dir1路径都需要会话才能继续。

website.com/user
website.com/account
website.com/questions

都会将SessionInterceptor应用于它们。

website.com/login
website.com/login/foo
website.com/login/bar

不会将SessionInterceptor应用于它们。

我已经浏览了各种Spring文档,但是没有找到任何可以解释为什么这个拦截器没有表现出我期望它的原因。

=======

编辑:2015-04-02

所以我已经做了一些挖掘,并在启动JAR文件时通过调用--debug = TRUE找到了如何启用调试。

当我尝试调用 / login 路由时,我在控制台中看到一条调试消息:

2015-04-02 15:43:33.827 DEBUG 34012 --- [qtp671981081-20] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /login
2015-04-02 15:43:33.828 DEBUG 34012 --- [qtp671981081-20] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/login]

我发现这个特别奇怪,因为我在调用 / login

时会收到输出

此时,我不认为拦截器是错误的,而是以某种方式我的路线没有被正确宣布。正如我在下面的一条评论中提到的,我使用的是Spring Boot 1.2.2。

这是我班级中充当控制者的片段:

@Controller
@RequestMapping("/login")
public class LoginController {


@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String login(){
    return "hello";
}

我有更多方法和@RequestMappings定义,但在调用其中任何一个时,我得到一个类似的调试错误,无法找到处理程序方法。

我希望这些额外的信息能够提供一些关于我做错的线索。

==========

编辑:2015-04-02#2

所以在花了一些时间调试之后,看起来正确定义了 / login (就像我上面提到的,我得到了所需的输出)并且拦截器工作正常。我意识到发生的事情是由于某种原因/错误是在/登录后调用的(我不知道为什么,然而)我的拦截器不排除/错误。所以当我打电话/登录时,一切都很顺利,但是对另一个/错误的调用,我收到了那个输出。

同样,不确定为什么会发生这种情况。我已经浪费了这么多时间,我考虑将Spring倾倒一些更重量轻的东西。我现在更愿意编写自己的Servlet,而不是试图找出幕后正在进行的黑魔法。

0 个答案:

没有答案