返回社区精选
文章
jwt,过滤器,拦截器用法和介绍
<div class="article-viewer markdown-body result"><h2 data-id="heading-0" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: rgb(51, 51, 51); margin: 10px 0px; font-size: 14px; padding: 0px;">jwt,过滤器,拦截器介绍</h2><h3 data-id="heading-1" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: rgb(51, 51, 51); margin: 10px 0px; font-size: 14px; padding: 0px;">JWT令牌</h3><h4 data-id="heading-2" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: inherit; margin: 10px 0px; font-size: 14px; padding: 0px;">JWT介绍</h4><h2 data-id="heading-0"><div style="box-sizing: border-box; padding: 0px; margin: 0px; color: rgb(102, 102, 102); font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">JWT全称 JSON Web Token 。</p><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">jwt可以将原始的json数据格式进行安全的封装,这样就可以直接基于jwt在通信双方安全的进行信息传输了。</p></div></h2><h5 data-id="heading-3" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: inherit; margin: 10px 0px; font-size: 14px; padding: 0px;">JWT的组成</h5><h2 data-id="heading-0"><div style="box-sizing: border-box; padding: 0px; margin: 0px; color: rgb(102, 102, 102); font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><p style="box-sizing: border-box; margin: 10px 0px; padding: 0px; line-height: 1.5;">JWT令牌由三个部分组成,三个部分之间使用英文的点来分割</p><ul style="box-sizing: border-box; margin: 10px 0px 10px 20px; padding: 0px; list-style: disc;"><li style="box-sizing: border-box; padding: 0px; margin: 0px;">第一部分:Header(头), 记录令牌类型、签名算法等。 例如:{"alg":"HS256","type":"JWT"}</li><li style="box-sizing: border-box; padding: 0px; margin: 0px;">第二部分:Payload(有效载荷),携带一些自定义信息、默认信息等。 例如:{"id":"1","username":"Tom"}</li><li style="box-sizing: border-box; padding: 0px; margin: 0px;">第三部分:Signature(签名),防止Token被篡改、确保安全性。将header、payload,并加入指定秘钥,通过指定签名算法计算而来。</li></ul></div></h2><h5 data-id="heading-4" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: inherit; margin: 10px 0px; font-size: 14px; padding: 0px;">JWT将原始的JSON格式数据转变为字符串的方式:</h5><h2 data-id="heading-0"><div style="box-sizing: border-box; padding: 0px; margin: 0px; color: rgb(102, 102, 102); font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><ul style="box-sizing: border-box; margin: 10px 0px 10px 20px; padding: 0px; list-style: disc;"><li style="box-sizing: border-box; padding: 0px; margin: 0px;">其实在生成JWT令牌时,会对JSON格式的数据进行一次编码:进行base64编码</li><li style="box-sizing: border-box; padding: 0px; margin: 0px;">Base64:是一种基于64个可打印的字符来表示二进制数据的编码方式。既然能编码,那也就意味着也能解码。所使用的64个字符分别是A到Z、a到z、 0- 9,一个加号,一个斜杠,加起来就是64个字符。任何数据经过base64编码之后,最终就会通过这64个字符来表示。当然还有一个符号,那就是等号。等号它是一个补位的符号</li><li style="box-sizing: border-box; padding: 0px; margin: 0px;">需要注意的是Base64是编码方式,而不是加密方式。</li></ul></div></h2><h4 data-id="heading-5" style="box-sizing: border-box; font-family: "Helvetica Neue", STHeiti, 微软雅黑, "Microsoft YaHei", Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.5; color: inherit; margin: 10px 0px; font-size: 14px; padding: 0px;">生成和校验</h4><h2 data-id="heading-0"><div style="color: rgb(102, 102, 102);"><p>1.要想使用JWT令牌,需要先引入JWT的依赖:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="xml" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(142, 144, 140);"></span>
<span style="color: rgb(200, 40, 41);"><span style="color: rgb(66, 113, 174);"><</span><span style="color: rgb(66, 113, 174);">dependency</span><span style="color: rgb(66, 113, 174);">></span></span>
<span style="color: rgb(200, 40, 41);"><span style="color: rgb(66, 113, 174);"><</span><span style="color: rgb(66, 113, 174);">groupId</span><span style="color: rgb(66, 113, 174);">></span></span><span style="color: rgb(234, 183, 0);">io</span>.jsonwebtoken<span style="color: rgb(200, 40, 41);">groupId</span>>
<span style="color: rgb(200, 40, 41);"><span style="color: rgb(66, 113, 174);"><</span><span style="color: rgb(66, 113, 174);">artifactId</span><span style="color: rgb(66, 113, 174);">></span></span><span style="color: rgb(234, 183, 0);">jjwt</span><span style="color: rgb(200, 40, 41);"><span style="color: rgb(234, 183, 0);">artifactId</span></span>>
<span style="color: rgb(200, 40, 41);"><span style="color: rgb(66, 113, 174);"><</span><span style="color: rgb(66, 113, 174);">version</span><span style="color: rgb(66, 113, 174);">></span></span><span style="color: rgb(234, 183, 0);">0</span>.9.1<span style="color: rgb(200, 40, 41);">version</span>>
<span style="color: rgb(200, 40, 41);"><span style="color: rgb(234, 183, 0);">dependency</span></span>>
</code></pre><p>引入完JWT来赖后,就可以调用工具包中提供的API来完成JWT令牌的<span style="font-weight: 700;">生成和校验</span>。</p><p>2.<span style="font-weight: 700;">生成</span>JWT代码实现:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@Test</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(245, 135, 31);">void</span> <span style="color: rgb(66, 113, 174);">testGenJwt</span>(<span style="color: rgb(245, 135, 31);"></span>) {
<span style="color: rgb(66, 113, 174);">Map</span><<span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">String</span></span>, <span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Object</span></span>> claims = <span style="color: rgb(137, 89, 168);">new</span> <span style="color: rgb(66, 113, 174);">HashMap</span><>();
claims.<span style="color: rgb(66, 113, 174);">put</span>(<span style="color: rgb(113, 140, 0);">"id"</span>, <span style="color: rgb(245, 135, 31);">10</span>);
claims.<span style="color: rgb(66, 113, 174);">put</span>(<span style="color: rgb(113, 140, 0);">"username"</span>, <span style="color: rgb(113, 140, 0);">"itheima"</span>);
<span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">String</span></span> jwt = <span style="color: rgb(66, 113, 174);">Jwts</span>.<span style="color: rgb(66, 113, 174);">builder</span>().<span style="color: rgb(66, 113, 174);">signWith</span>(<span style="color: rgb(66, 113, 174);">SignatureAlgorithm</span>.HS256, <span style="color: rgb(113, 140, 0);">"aXRjYXN0"</span>)
.<span style="color: rgb(66, 113, 174);">addClaims</span>(claims)
.<span style="color: rgb(66, 113, 174);">setExpiration</span>(<span style="color: rgb(137, 89, 168);">new</span> <span style="color: rgb(66, 113, 174);"><span style="color: rgb(245, 135, 31);">Date</span></span>(<span style="color: rgb(66, 113, 174);">System</span>.<span style="color: rgb(66, 113, 174);">currentTimeMillis</span>() + <span style="color: rgb(245, 135, 31);">12</span> * <span style="color: rgb(245, 135, 31);">3600</span> * <span style="color: rgb(245, 135, 31);">1000</span>))
.<span style="color: rgb(66, 113, 174);">compact</span>();
<span style="color: rgb(66, 113, 174);">System</span>.out.<span style="color: rgb(66, 113, 174);">println</span>(jwt);
}
</code></pre><ol start="3"><li>实现了JWT令牌的生成,下面我们接着使用Java代码来<span style="font-weight: 700;">校验</span>JWT令牌(解析生成的令牌):</li></ol></div><pre style="color: rgb(77, 77, 76); background: white;"><code lang="csharp" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@Test</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">testParseJwt</span><span style="color: rgb(245, 135, 31);">()</span> {
Claims claims = Jwts.parser().setSigningKey(<span style="color: rgb(113, 140, 0);">"aXRjYXN0"</span>)
.parseClaimsJws(<span style="color: rgb(113, 140, 0);">"eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTAsInVzZXJuYW1lIjoiaXRoZWltYSIsImV4cCI6MTcwMTkwOTAxNX0.N-MD6DmoeIIY5lB5z73UFLN9u7veppx1K5_N_jS9Yko"</span>)
.getBody();
System.<span style="color: rgb(137, 89, 168);">out</span>.println(claims);
}
</code></pre><p style="color: rgb(102, 102, 102);"><span style="font-weight: 700;">篡改令牌中的任何一个字符,在对令牌进行解析时都会报错,所以JWT令牌是非常安全可靠的。</span></p><p style="color: rgb(102, 102, 102);"><span style="font-weight: 700;">JWT令牌过期后,令牌就失效了,解析的为非法令牌。</span></p></h2><h3 data-id="heading-6" style="font-size: 14px;">过滤器Filter</h3><h4 data-id="heading-7" style="color: rgb(102, 102, 102);">Filter介绍</h4><h2 data-id="heading-0"><ul style="color: rgb(102, 102, 102);"><li>Filter表示过滤器,是 JavaWeb三大组件(Servlet、Filter、Listener)之一。</li><li>过滤器可以把对资源的请求拦截下来,从而实现一些特殊的功能<p></p><ul><li>使用了过滤器之后,要想访问web服务器上的资源,必须先经过滤器,过滤器处理完毕之后,才可以访问对应的资源。</li></ul></li><li>过滤器一般完成一些通用的操作,比如:登录校验、统一编码处理、敏感字符处理等。</li></ul></h2><h6 data-id="heading-8" style="color: rgb(102, 102, 102); font-size: 14px;">定义过滤器</h6><h2 data-id="heading-0"><pre style="color: rgb(77, 77, 76); background: white;"><code lang="csharp" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">DemoFilter</span> <span style="color: rgb(66, 113, 174);"><span style="color: rgb(137, 89, 168);">implements</span></span> <span style="color: rgb(66, 113, 174);">Filter</span> {
<span style="color: rgb(142, 144, 140);">//初始化方法, web服务器启动, 创建Filter实例时调用, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">init</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">FilterConfig filterConfig</span><span style="color: rgb(245, 135, 31);">)</span> <span style="color: rgb(137, 89, 168);">throws</span> ServletException {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"init ..."</span>);
}
<span style="color: rgb(142, 144, 140);">//拦截到请求时,调用该方法,可以调用多次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">doFilter</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain</span><span style="color: rgb(245, 135, 31);">)</span> <span style="color: rgb(137, 89, 168);">throws</span> IOException, ServletException {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"拦截到了请求..."</span>);
}
<span style="color: rgb(142, 144, 140);">//销毁方法, web服务器关闭时调用, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">destroy</span><span style="color: rgb(245, 135, 31);">()</span> {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"destroy ... "</span>);
}
}
</code></pre></h2><h6 data-id="heading-9" style="color: rgb(102, 102, 102); font-size: 14px;">配置过滤器</h6><h2 data-id="heading-0"><p style="color: rgb(102, 102, 102);">在定义完Filter之后,Filter其实并不会生效,还需要完成Filter的配置,Filter的配置非常简单,只需要在Filter类上添加一个注解:<code style="font-size: 12.6px;">@WebFilter</code>,并指定属性<code style="font-size: 12.6px;">urlPatterns</code>,通过这个属性指定过滤器要拦截哪些请求</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="csharp" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@WebFilter</span>(urlPatterns = <span style="color: rgb(113, 140, 0);">"/*"</span>) <span style="color: rgb(142, 144, 140);">//配置过滤器要拦截的请求路径( /* 表示拦截浏览器的所有请求 )</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">DemoFilter</span> <span style="color: rgb(66, 113, 174);"><span style="color: rgb(137, 89, 168);">implements</span></span> <span style="color: rgb(66, 113, 174);">Filter</span> {
<span style="color: rgb(142, 144, 140);">//初始化方法, web服务器启动, 创建Filter实例时调用, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">init</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">FilterConfig filterConfig</span><span style="color: rgb(245, 135, 31);">)</span> <span style="color: rgb(137, 89, 168);">throws</span> ServletException {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"init ..."</span>);
}
<span style="color: rgb(142, 144, 140);">//拦截到请求时,调用该方法,可以调用多次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">doFilter</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain</span><span style="color: rgb(245, 135, 31);">)</span> <span style="color: rgb(137, 89, 168);">throws</span> IOException, ServletException {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"拦截到了请求..."</span>);
}
<span style="color: rgb(142, 144, 140);">//销毁方法, web服务器关闭时调用, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">destroy</span><span style="color: rgb(245, 135, 31);">()</span> {
System.<span style="color: rgb(137, 89, 168);">out</span>.println(<span style="color: rgb(113, 140, 0);">"destroy ... "</span>);
}
}
</code></pre><p style="color: rgb(102, 102, 102);">在Filter类上面加了@WebFilter注解之后,还需要在启动类上面加上一个注解<code style="font-size: 12.6px;">@ServletComponentScan</code>,通过这个<code style="font-size: 12.6px;">@ServletComponentScan</code>注解来开启SpringBoot项目对于Servlet组件的支持。</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="less" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(200, 40, 41);">@ServletComponentScan</span> <span style="color: rgb(142, 144, 140);">//开启对Servlet组件的支持</span>
<span style="color: rgb(200, 40, 41);">@SpringBootApplication</span>
public class TliasManagementApplication {
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">static</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(137, 89, 168);">main</span>(String[] args) {
<span style="color: rgb(137, 89, 168);">SpringApplication</span><span style="color: rgb(200, 40, 41);">.run</span>(TliasManagementApplication.class, args);
}
}
</code></pre><p style="color: rgb(102, 102, 102);"><span style="font-weight: 700;">在过滤器Filter中,如果不执行放行操作,将无法访问后面的资源。 放行操作:<code style="font-size: 12.6px;">chain.doFilter(request, response);</code></span></p></h2><h4 data-id="heading-10" style="color: rgb(102, 102, 102);">过滤器的执行流程</h4><h2 data-id="heading-0"><p style="color: rgb(102, 102, 102);">过滤器当中我们拦截到了请求之后,如果希望继续访问后面的web资源,就要执行放行操作,放行就是调用 FilterChain对象当中的doFilter()方法,在调用doFilter()这个方法之前所编写的代码属于放行之前的逻辑。</p><p style="color: rgb(102, 102, 102);">测试代码:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="java" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@WebFilter(urlPatterns = <span style="color: rgb(113, 140, 0);">"/*"</span>)</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">DemoFilter</span> <span style="color: rgb(137, 89, 168);">implements</span> <span style="color: rgb(66, 113, 174);">Filter</span> {
<span style="color: rgb(245, 135, 31);">@Override</span> <span style="color: rgb(142, 144, 140);">//初始化方法, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">init</span><span style="color: rgb(245, 135, 31);">(FilterConfig filterConfig)</span> <span style="color: rgb(137, 89, 168);">throws</span> ServletException {
System.out.println(<span style="color: rgb(113, 140, 0);">"init 初始化方法执行了"</span>);
}
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">doFilter</span><span style="color: rgb(245, 135, 31);">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span style="color: rgb(137, 89, 168);">throws</span> IOException, ServletException {
System.out.println(<span style="color: rgb(113, 140, 0);">"DemoFilter 放行前逻辑....."</span>);
<span style="color: rgb(142, 144, 140);">//放行请求</span>
filterChain.doFilter(servletRequest,servletResponse);
System.out.println(<span style="color: rgb(113, 140, 0);">"DemoFilter 放行后逻辑....."</span>);
}
<span style="color: rgb(245, 135, 31);">@Override</span> <span style="color: rgb(142, 144, 140);">//销毁方法, 只调用一次</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">destroy</span><span style="color: rgb(245, 135, 31);">()</span> {
System.out.println(<span style="color: rgb(113, 140, 0);">"destroy 销毁方法执行了"</span>);
}
}
</code></pre></h2><h4 data-id="heading-11" style="color: rgb(102, 102, 102);">过滤器的拦截路径配置</h4><h2 data-id="heading-0"><table style="background-color: rgb(255, 255, 255); color: rgb(102, 102, 102);"><thead><tr><th>拦截路径</th><th>urlPatterns值</th><th>含义</th></tr></thead><tbody><tr><td>拦截具体路径</td><td>/login</td><td>只有访问 /login 路径时,才会被拦截</td></tr><tr><td>目录拦截</td><td>/emps/*</td><td>访问/emps下的所有资源,都会被拦截</td></tr><tr><td>拦截所有</td><td>/*</td><td>访问所有资源,都会被拦截</td></tr></tbody></table><p style="color: rgb(102, 102, 102);">测试代码:</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="java" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@WebFilter(urlPatterns = <span style="color: rgb(113, 140, 0);">"/login"</span>)</span> <span style="color: rgb(142, 144, 140);">//拦截/login具体路径</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">DemoFilter</span> <span style="color: rgb(137, 89, 168);">implements</span> <span style="color: rgb(66, 113, 174);">Filter</span> {
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">doFilter</span><span style="color: rgb(245, 135, 31);">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span style="color: rgb(137, 89, 168);">throws</span> IOException, ServletException {
System.out.println(<span style="color: rgb(113, 140, 0);">"DemoFilter 放行前逻辑....."</span>);
<span style="color: rgb(142, 144, 140);">//放行请求</span>
filterChain.doFilter(servletRequest,servletResponse);
System.out.println(<span style="color: rgb(113, 140, 0);">"DemoFilter 放行后逻辑....."</span>);
}
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">init</span><span style="color: rgb(245, 135, 31);">(FilterConfig filterConfig)</span> <span style="color: rgb(137, 89, 168);">throws</span> ServletException {
Filter.<span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">super</span></span>.init(filterConfig);
}
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">destroy</span><span style="color: rgb(245, 135, 31);">()</span> {
Filter.<span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">super</span></span>.destroy();
}
}
</code></pre></h2><h4 data-id="heading-12" style="color: rgb(102, 102, 102);">过滤器链</h4><h2 data-id="heading-0"><p style="color: rgb(102, 102, 102);">过滤器链指的是在一个web应用程序当中,可以配置多个过滤器,多个过滤器就形成了一个过滤器链。</p><p style="color: rgb(102, 102, 102);">过滤器链上过滤器的执行顺序:注解配置的Filter,优先级是按照过滤器类名(字符串)的自然排序。 比如:</p><ul style="color: rgb(102, 102, 102);"><li>AbcFilter</li><li>DemoFilter</li></ul><p style="color: rgb(102, 102, 102);">这两个过滤器来说,AbcFilter 会先执行,DemoFilter会后执行。</p></h2><h3 data-id="heading-13" style="font-size: 14px;">拦截器Interceptor</h3><h2 data-id="heading-0"><ul style="color: rgb(102, 102, 102);"><li>拦截器是一种动态拦截方法调用的机制,类似于过滤器。</li><li>拦截器是Spring框架中提供的,用来动态拦截控制器方法的执行。</li><li>拦截器的作用:拦截请求,在指定方法调用前后,根据业务需要执行预先设定的代码。</li></ul><p style="color: rgb(102, 102, 102);"><span style="font-weight: 700;">自定义拦截器</span></p><p style="color: rgb(102, 102, 102);">实现HandlerInterceptor接口,并重写其所有方法</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="java" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(142, 144, 140);">//自定义拦截器</span>
<span style="color: rgb(245, 135, 31);">@Component</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">DemoInterceptor</span> <span style="color: rgb(137, 89, 168);">implements</span> <span style="color: rgb(66, 113, 174);">HandlerInterceptor</span> {
<span style="color: rgb(142, 144, 140);">//目标资源方法执行前执行。 返回true:放行 返回false:不放行</span>
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">boolean</span></span> <span style="color: rgb(66, 113, 174);">preHandle</span><span style="color: rgb(245, 135, 31);">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span style="color: rgb(137, 89, 168);">throws</span> Exception {
System.out.println(<span style="color: rgb(113, 140, 0);">"preHandle .... "</span>);
<span style="color: rgb(137, 89, 168);">return</span> <span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">true</span></span>; <span style="color: rgb(142, 144, 140);">//true表示放行</span>
}
<span style="color: rgb(142, 144, 140);">//目标资源方法执行后执行</span>
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">postHandle</span><span style="color: rgb(245, 135, 31);">(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)</span> <span style="color: rgb(137, 89, 168);">throws</span> Exception {
System.out.println(<span style="color: rgb(113, 140, 0);">"postHandle ... "</span>);
}
<span style="color: rgb(142, 144, 140);">//视图渲染完毕后执行,最后执行</span>
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">void</span> <span style="color: rgb(66, 113, 174);">afterCompletion</span><span style="color: rgb(245, 135, 31);">(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)</span> <span style="color: rgb(137, 89, 168);">throws</span> Exception {
System.out.println(<span style="color: rgb(113, 140, 0);">"afterCompletion .... "</span>);
}
}
</code></pre><ul style="color: rgb(102, 102, 102);"><li>preHandle方法:目标资源方法执行前执行。 返回true:放行 返回false:不放行</li><li>postHandle方法:目标资源方法执行后执行</li><li>afterCompletion方法:视图渲染完毕后执行,最后执行</li></ul><p style="color: rgb(102, 102, 102);"><span style="font-weight: 700;">注册配置拦截器</span></p><p style="color: rgb(102, 102, 102);">在 <code style="font-size: 12.6px;">com.itheima</code>下创建一个包,然后创建一个配置类 <code style="font-size: 12.6px;">WebConfig</code>, 实现 <code style="font-size: 12.6px;">WebMvcConfigurer</code> 接口,并重写 <code style="font-size: 12.6px;">addInterceptors</code> 方法</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@Configuration</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">WebConfig</span> <span style="color: rgb(137, 89, 168);">implements</span> <span style="color: rgb(66, 113, 174);">WebMvcConfigurer</span> {
<span style="color: rgb(142, 144, 140);">//自定义的拦截器对象</span>
<span style="color: rgb(245, 135, 31);">@Autowired</span>
<span style="color: rgb(137, 89, 168);">private</span> <span style="color: rgb(66, 113, 174);">DemoInterceptor</span> demoInterceptor;
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">void</span></span> <span style="color: rgb(66, 113, 174);">addInterceptors</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">InterceptorRegistry registry</span><span style="color: rgb(245, 135, 31);">)</span> {
<span style="color: rgb(142, 144, 140);">//注册自定义拦截器对象</span>
registry.<span style="color: rgb(66, 113, 174);">addInterceptor</span>(demoInterceptor).<span style="color: rgb(66, 113, 174);">addPathPatterns</span>(<span style="color: rgb(113, 140, 0);">"/**"</span>);<span style="color: rgb(142, 144, 140);">//设置拦截器拦截的请求路径( /** 表示拦截所有请求)</span>
}
}
</code></pre></h2><h4 data-id="heading-14" style="color: rgb(102, 102, 102);">拦截器的拦截路径配置</h4><h2 data-id="heading-0"><p style="color: rgb(102, 102, 102);">首先我们先来看拦截器的拦截路径的配置,在注册配置拦截器的时候,我们要指定拦截器的拦截路径,通过<code style="font-size: 12.6px;">addPathPatterns("要拦截路径")</code>方法,就可以指定要拦截哪些资源。</p><p style="color: rgb(102, 102, 102);">在入门程序中我们配置的是<code style="font-size: 12.6px;">/**</code>,表示拦截所有资源,而在配置拦截器时,不仅可以指定要拦截哪些资源,还可以指定不拦截哪些资源,只需要调用<code style="font-size: 12.6px;">excludePathPatterns("不拦截路径")</code>方法,指定哪些资源不需要拦截。</p><pre style="color: rgb(77, 77, 76); background: white;"><code lang="typescript" style="color: rgb(77, 77, 76); background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; overflow-x: auto;"><span style="color: rgb(245, 135, 31);">@Configuration</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(137, 89, 168);">class</span> <span style="color: rgb(66, 113, 174);">WebConfig</span> <span style="color: rgb(137, 89, 168);">implements</span> <span style="color: rgb(66, 113, 174);">WebMvcConfigurer</span> {
<span style="color: rgb(142, 144, 140);">//拦截器对象</span>
<span style="color: rgb(245, 135, 31);">@Autowired</span>
<span style="color: rgb(137, 89, 168);">private</span> <span style="color: rgb(66, 113, 174);">DemoInterceptor</span> demoInterceptor;
<span style="color: rgb(245, 135, 31);">@Override</span>
<span style="color: rgb(137, 89, 168);">public</span> <span style="color: rgb(245, 135, 31);"><span style="color: rgb(137, 89, 168);">void</span></span> <span style="color: rgb(66, 113, 174);">addInterceptors</span><span style="color: rgb(245, 135, 31);">(</span><span style="color: rgb(245, 135, 31);">InterceptorRegistry registry</span><span style="color: rgb(245, 135, 31);">)</span> {
<span style="color: rgb(142, 144, 140);">//注册自定义拦截器对象</span>
registry.<span style="color: rgb(66, 113, 174);">addInterceptor</span>(demoInterceptor)
.<span style="color: rgb(66, 113, 174);">addPathPatterns</span>(<span style="color: rgb(113, 140, 0);">"/**"</span>)<span style="color: rgb(142, 144, 140);">//设置拦截器拦截的请求路径( /** 表示拦截所有请求)</span>
.<span style="color: rgb(66, 113, 174);">excludePathPatterns</span>(<span style="color: rgb(113, 140, 0);">"/login"</span>);<span style="color: rgb(142, 144, 140);">//设置不拦截的请求路径</span>
}
}
</code></pre><p style="color: rgb(102, 102, 102);">在拦截器中除了可以设置<code style="font-size: 12.6px;">/**</code>拦截所有资源外,还有一些常见拦截路径设置:</p><table style="background-color: rgb(255, 255, 255); color: rgb(102, 102, 102);"><thead><tr><th>拦截路径</th><th>含义</th><th>举例</th></tr></thead><tbody><tr><td>/*</td><td>一级路径</td><td>能匹配/depts,/emps,/login,不能匹配 /depts/1</td></tr><tr><td>/**</td><td>任意级路径</td><td>能匹配/depts,/depts/1,/depts/1/2</td></tr><tr><td>/depts/*</td><td>/depts下的一级路径</td><td>能匹配/depts/1,不能匹配/depts/1/2,/depts</td></tr><tr><td>/depts/**</td><td>/depts下的任意级路径</td><td>能匹配/depts,/depts/1,/depts/1/2,不能匹配/emps/1</td></tr></tbody></table></h2><h4 data-id="heading-15" style="color: rgb(102, 102, 102);">拦截器的执行流程</h4><h2 data-id="heading-0"><ul style="color: rgb(102, 102, 102);"><li>当我们打开浏览器来访问部署在web服务器当中的web应用时,此时我们所定义的过滤器会拦截到这次请求。拦截到这次请求之后,它会先执行放行前的逻辑,然后再执行放行操作。而由于我们当前是基于springboot开发的,所以放行之后是进入到了spring的环境当中,也就是要来访问我们所定义的controller当中的接口方法。</li><li>Tomcat并不识别所编写的Controller程序,但是它识别Servlet程序,所以在Spring的Web环境中提供了一个非常核心的Servlet:DispatcherServlet(前端控制器),所有请求都会先进行到DispatcherServlet,再将请求转给Controller。</li><li>当我们定义了拦截器后,会在执行Controller的方法之前,请求被拦截器拦截住。执行<code style="font-size: 12.6px;">preHandle()</code>方法,这个方法执行完成后需要返回一个布尔类型的值,如果返回true,就表示放行本次操作,才会继续访问controller中的方法;如果返回false,则不会放行(controller中的方法也不会执行)。</li><li>在controller当中的方法执行完毕之后,再回过来执行<code style="font-size: 12.6px;">postHandle()</code>这个方法以及<code style="font-size: 12.6px;">afterCompletion()</code> 方法,然后再返回给DispatcherServlet,最终再来执行过滤器当中放行后的这一部分逻辑的逻辑。执行完毕之后,最终给浏览器响应数据。</li></ul></h2><h5 data-id="heading-16" style="color: rgb(102, 102, 102); font-size: 14px;">过滤器和拦截器之间的区别:</h5><h2 data-id="heading-0"><ul style="color: rgb(102, 102, 102);"><li><span style="font-weight: 700;">接口规范不同:过滤器需要实现Filter接口,而拦截器需要实现HandlerInterceptor接口。</span></li><li><span style="font-weight: 700;">拦截范围不同:过滤器Filter会拦截所有的资源,而Interceptor只会拦截Spring环境中的资源。</span></li></ul><br style="color: rgb(102, 102, 102);"><div style="color: rgb(102, 102, 102);">作者:丧心病狂汤姆猫<br>来源:juejin.cn/post/7527869985345339392</div></h2></div>
