问题描述
今天在给springboot项目配置拦截器的时候发现怎么都进不到拦截器的方法里面,在搜索引擎上看了无数篇关于配置拦截器的文章都没有找到解决方案。
就在我准备放弃的时候,在 csdn 上发现了一篇文章,说的是springboot 用了@importresource 配置的拦截器就不起作用了。于是我就赶紧到application
启动类看了一眼,果然项目中使用了@importresource
注解用于配置系统的参数。
代码如下:
启动类配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.xx.xxx; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.builder.springapplicationbuilder; import org.springframework.boot.web.servlet.servletcomponentscan; import org.springframework.boot.web.support.springbootservletinitializer; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.importresource; @enablediscoveryclient @enableautoconfiguration (exclude = {datasourceautoconfiguration. class , thymeleafautoconfiguration. class }) @springbootapplication // 注意这里 !!!! @importresource (locations={ "classpath:config/application-*.xml" }) @enablehystrix public class application extends springbootservletinitializer { public static void main(string[] args) { springapplication.run(application. class , args); } } |
拦截器配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.xx.xxx.config; import com.example.springbootdemo.interceptor.logininterceptor; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.cache.annotation.enablecaching; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.importresource; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; @configuration public class webmvcconfig extends webmvcconfigureradapter { /** * 拦截器(用户登录验证) * @param registry */ @override public void addinterceptors(interceptorregistry registry) { // addpathpatterns 用于添加拦截规则 // excludepathpatterns 用户排除拦截 registry.addinterceptor( new logininterceptor()).addpathpatterns( "/**" ).excludepathpatterns( "/user" , "/login" ); super .addinterceptors(registry); } } |
拦截器实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.xx.xxx.interceptor; import org.springframework.stereotype.component; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; public class logininterceptor implements handlerinterceptor { private final static logger logger = loggerfactory.getlogger(logininterceptor. class ); @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { logger.info( "******进来了******" ); return true ; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { } } |
具体为什么使用@importresource注解会影响拦截器的配置,如果有机会研究一下源码或许能够找到答案。
ps : springboot 版本 1.5.2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/yuansc/p/9077509.html