在使用 spring boot 开发 web 应用时,我们常常需要使用拦截器(interceptor)来对请求进行预处理。例如,验证用户是否登录。然而,很多开发者会遇到一个常见的问题:拦截器配置了却不生效。本文将讨论一种常见的原因及其解决方案——将配置类移入正确的包下。
问题描述
我们创建了一个 logincheckinterceptor
类,并在 webconfig
类中进行注册。但是,启动应用后发现拦截器并没有生效。
示例代码:
logincheckinterceptor
类:
package com.itheima.interceptor; import com.alibaba.fastjson.jsonobject; import com.itheima.pojo.result; import com.itheima.utils.jwtutils; import lombok.extern.slf4j.slf4j; import org.springframework.stereotype.component; import org.springframework.util.stringutils; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @slf4j @component public class logincheckinterceptor implements handlerinterceptor { @override public boolean prehandle(httpservletrequest req, httpservletresponse resp, object handler) throws exception { string url = req.getrequesturl().tostring(); log.info("请求的url: {}", url); if (url.contains("login")) { log.info("登录操作, 放行..."); return true; } string jwt = req.getheader("token"); if (!stringutils.haslength(jwt)) { log.info("请求头token为空,返回未登录的信息"); result error = result.error("not_login"); string notlogin = jsonobject.tojsonstring(error); resp.getwriter().write(notlogin); return false; } try { jwtutils.parsejwt(jwt); } catch (exception e) { e.printstacktrace(); log.info("解析令牌失败, 返回未登录错误信息"); result error = result.error("not_login"); string notlogin = jsonobject.tojsonstring(error); resp.getwriter().write(notlogin); return false; } log.info("令牌合法, 放行"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { system.out.println("posthandle ..."); } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { system.out.println("aftercompletion..."); } }
webconfig
类:
package com.config; import com.itheima.interceptor.logincheckinterceptor; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; @configuration public class webconfig implements webmvcconfigurer { @autowired private logincheckinterceptor logincheckinterceptor; @override public void addinterceptors(interceptorregistry registry) { registry.addinterceptor(logincheckinterceptor).addpathpatterns("/**"); } }
解决方案
将 webconfig
类移到 itheima
包下即可解决问题。原因在于 spring boot 的默认包扫描机制。
原因分析
spring boot 使用 @springbootapplication
注解的主应用类启动应用。该注解包含了 @componentscan
,默认扫描主应用类所在包及其子包中的所有组件。如果 webconfig
类不在主应用类所在包或其子包下,spring boot 将无法自动扫描到它,从而导致拦截器不生效。
解决方法
将 webconfig
类移到 com.itheima
包下,确保其在主应用类的扫描路径内。
调整后的目录结构:
src/main/java └── com └── itheima ├── myapplication.java ├── interceptor │ └── logincheckinterceptor.java └── config └── webconfig.java
代码调整
将 webconfig
类从 com.config
包移到 com.itheima.config
包下:
package com.itheima.config; import com.itheima.interceptor.logincheckinterceptor; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; @configuration public class webconfig implements webmvcconfigurer { @autowired private logincheckinterceptor logincheckinterceptor; @override public void addinterceptors(interceptorregistry registry) { registry.addinterceptor(logincheckinterceptor).addpathpatterns("/**"); } }
其他解决方案
如果不想移动配置类,还可以通过以下方法显式指定扫描路径:
1. 使用 @componentscan
注解指定扫描包:
package com.itheima; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.componentscan; @springbootapplication @componentscan(basepackages = {"com.itheima", "com.config"}) public class myapplication { public static void main(string[] args) { springapplication.run(myapplication.class, args); } }
2. 使用 @import
注解导入配置类:
package com.itheima; import com.itheima.config.webconfig; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.import; @springbootapplication @import(webconfig.class) public class myapplication { public static void main(string[] args) { springapplication.run(myapplication.class, args); } }
通过这些方式,可以确保 spring boot 正确扫描和加载拦截器配置类,使拦截器生效。
结论
在使用 spring boot 开发 web 应用时,正确配置包扫描路径非常重要。确保配置类在主应用类的扫描路径内,可以有效解决拦截器不生效的问题。希望这篇文章能够帮助大家更好地理解 spring boot 的包扫描机制,并顺利解决开发中遇到的问题。
到此这篇关于springboot拦截器不生效的问题解决的文章就介绍到这了,更多相关springboot拦截器不生效内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论