问题描述
今天在给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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/yuansc/p/9077509.html
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 88
-
winkawaks1.65怎么添加游戏(winkawaks中文版)
2025-05-25 97 -
2025-05-24 49
-
2025-05-29 32
-
2025-06-04 61
热门评论