{"id":12658,"date":"2023-09-08T16:25:16","date_gmt":"2023-09-08T08:25:16","guid":{"rendered":"https:\/\/92it.top\/?p=12658"},"modified":"2023-09-08T16:45:24","modified_gmt":"2023-09-08T08:45:24","slug":"spring-%e8%bf%87%e6%bb%a4%e5%99%a8%e5%92%8c%e6%8b%a6%e6%88%aa%e5%99%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=12658","title":{"rendered":"Spring \u8fc7\u6ee4\u5668\u548c\u62e6\u622a\u5668"},"content":{"rendered":"\n<p><strong>1\u3001\u8ba4\u8bc6\u8fc7\u6ee4\u5668\uff08Filter\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.1\u3001\u8fc7\u6ee4\u5668\u7684\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>\u8fc7\u6ee4\u5668\u662fJavaWeb\u7684\u4e09\u5927\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u662f\u5b9e\u73b0Filter\u63a5\u53e3\u7684Java\u7c7b\u3002<\/p>\n\n\n\n<p>\u8fc7\u6ee4\u5668\u662f\u5b9e\u73b0\u5bf9\u8bf7\u6c42\u8d44\u6e90\uff08jsp\u3001servlet\u3001html\uff09\u7684\u8fc7\u6ee4\u529f\u80fd\uff0c\u662f\u4e00\u4e2a\u8fd0\u884c\u5728\u670d\u52a1\u5668\u7684\u7a0b\u5e8f\uff0c\u4f18\u5148\u4e8e\u8bf7\u6c42\u8d44\u6e90\uff08jsp\u3001servlet\u3001html\uff09\u4e4b\u524d\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u5f53\u6d4f\u89c8\u5668\u53d1\u9001\u8bf7\u6c42\u7ed9\u670d\u52a1\u5668\u7684\u65f6\u5019\uff0c\u5148\u6267\u2f8f\u8fc7\u6ee4\u5668\uff0c\u7136\u540e\u624d\u8bbf\u95eeWeb\u7684\u8d44\u6e90\u3002\u670d\u52a1\u5668\u54cd\u5e94Response\uff0c\u4eceWeb\u8d44\u6e90\u62b5\u8fbe\u6d4f\u89c8\u5668\u4e4b\u524d\uff0c\u4e5f\u4f1a\u9014\u5f84\u8fc7\u6ee4\u5668\u3002<\/p>\n\n\n\n<p>\u5728\u5f88\u591aWeb\u5f00\u53d1\u4e2d\uff0c\u90fd\u4f1a\u7528\u5230\u8fc7\u6ee4\u5668\uff08Filter\uff09\uff0c\u5982\u53c2\u6570\u8fc7\u6ee4\u3001\u9632\u6b62SQL\u6ce8\u5165\u3001\u9632\u6b62\u9875\u9762\u653b\u51fb\u3001\u8fc7\u6ee4\u654f\u611f\u5b57\u7b26\u3001\u89e3\u51b3\u7f51\u7ad9\u4e71\u7801\u3001\u7a7a\u53c2\u6570\u77eb\u6b63\u3001Token\u9a8c\u8bc1\u3001Session\u9a8c\u8bc1\u3001\u70b9\u51fb\u7387\u7edf\u8ba1\u7b49\u3002<\/p>\n\n\n\n<p><strong>1.2\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u8fc7\u6ee4\u5668<\/strong><\/p>\n\n\n\n<p>\u5728Web\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u6709\u8fd9\u6837\u7684\u9700\u6c42\uff1a\u5728\u6240\u6709\u63a5\u53e3\u4e2d\u53bb\u9664\u7528\u6237\u8f93\u5165\u7684\u975e\u6cd5\u5b57\u7b26\uff0c\u4ee5\u9632\u6b62\u5f15\u8d77\u4e1a\u52a1\u5f02\u5e38\u3002\u8981\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u5f88\u591a\u65b9\u6cd5\uff0c\u5982\uff1a<\/p>\n\n\n\n<ul><li>\u5728\u524d\u7aef\u53c2\u6570\u4f20\u5165\u65f6\u8fdb\u884c\u6821\u9a8c\uff0c\u5148\u8fc7\u6ee4\u975e\u6cd5\u5b57\u7b26\uff0c\u7136\u540e\u8fd4\u56de\u7528\u6237\u754c\u9762\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/li><li>\u540e\u7aef\u63a5\u6536\u524d\u7aef\u6ca1\u6709\u8fc7\u6ee4\u7684\u6570\u636e\uff0c\u7136\u540e\u8fc7\u6ee4\u975e\u6cd5\u5b57\u7b26\u3002<\/li><li>\u5229\u7528filter\u5904\u7406\u9879\u76ee\u4e2d\u6240\u6709\u975e\u6cd5\u5b57\u7b26\u3002<\/li><\/ul>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u524d\u4e24\u79cd\u5b9e\u73b0\u65b9\u6cd5\u4f1a\u5b58\u5728\u91cd\u590d\u4ee3\u7801\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u524d\u7aef\u9875\u9762\u6216\u540e\u7aef\u90fd\u9700\u8981\u5904\u7406\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u4ee3\u7801\u5f88\u96be\u7ef4\u62a4\u3002\u5982\u679c\u7528\u8fc7\u6ee4\u5668\u6765\u5b9e\u73b0\uff0c\u5219\u53ea\u9700\u8981\u7528\u8fc7\u6ee4\u5668\u5bf9\u6240\u6709\u63a5\u53e3\u8fdb\u884c\u8fc7\u6ee4\u5904\u7406\u3002\u8fd9\u6837\u975e\u5e38\u65b9\u4fbf\uff0c\u540c\u65f6\u4e0d\u4f1a\u51fa\u73b0\u5197\u4f59\u4ee3\u7801\u3002<\/p>\n\n\n\n<p><strong>1.3\u3001\u4f7f\u7528Filter\u7684\u6b65\u9aa4\uff08\u4ee5SpringBoot\u9879\u76ee\u4e3a\u4f8b\uff09<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u65b0\u5efa\u7c7b\uff0c\u5b9e\u73b0Filter\u62bd\u7c7b\u7c7b\u3002<\/p>\n\n\n\n<p>\uff082\uff09\u91cd\u5199init\u3001doFilter\u3001destroy\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\uff083\uff09\u5728Spring Boot\u5165\u53e3\u7c7b\u4e2d\u6dfb\u52a0\u6ce8\u89e3@ServletComponentScan\uff0c\u4ee5\u6ce8\u518cFilter\u3002<\/p>\n\n\n\n<p>init()\uff1a\u8be5\u65b9\u6cd5\u5728\u5bb9\u5668\u542f\u52a8\u521d\u59cb\u5316\u8fc7\u6ee4\u5668\u65f6\u88ab\u8c03\u7528\uff0c\u5b83\u5728Filter\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u53ea\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5fc5\u987b\u6267\u884c\u6210\u529f\uff0c\u5426\u5219\u8fc7\u6ee4\u5668\u4f1a\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>doFilter()\uff1a\u5bb9\u5668\u4e2d\u7684\u6bcf\u4e00\u6b21\u8bf7\u6c42\u90fd\u4f1a\u8c03\u7528\u8be5\u65b9\u6cd5\uff0cFilterChain\u7528\u6765\u8c03\u7528\u4e0b\u4e00\u4e2a\u8fc7\u6ee4\u5668Filter\u3002<\/p>\n\n\n\n<p>destroy()\uff1a\u5bb9\u5668\u9500\u6bc1\u65f6\u88ab\u8c03\u7528\u3002\u4e00\u822c\u5728\u65b9\u6cd5\u4e2d\u9500\u6bc1\u6216\u5173\u95ed\u8d44\u6e90\uff0c\u4e5f\u53ea\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\u7684\u662fdoFilter()\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\npublic void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) throws IOException, ServletException{\n  \/\/\u8bf7\u6c42\uff08request\uff09\u5904\u7406\u903b\u8f91\n  \/\/\u8bf7\u6c42\uff08request\uff09\u5c01\u88c5\u903b\u8f91\n  \/\/chain\u91cd\u65b0\u5199\u56derequest\u548cresponse\n}<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684FilterChain\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u91cc\u9762\u53c8\u5b9a\u4e49\u4e86doFilter()\u65b9\u6cd5\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728Java\u4e2d\u4f7f\u2f64\u4e86\u94fe\u5f0f\u7ed3\u6784\u3002\u628a\u6240\u6709\u7684\u8fc7\u6ee4\u5668\u90fd\u653e\u5728FilterChain\u2fa5\u8fb9\uff0c\u5982\u679c\u7b26\u5408\u6761\u4ef6\uff0c\u5c31\u6267\u2f8f\u4e0b\u2f00\u4e2a\u8fc7\u6ee4\u5668\uff08\u5982\u679c\u6ca1\u6709\u8fc7\u6ee4\u5668\u4e86\uff0c\u5c31\u6267\u2f8f\u2f6c\u6807\u8d44\u6e90\uff09\u3002<\/p>\n\n\n\n<p><strong>1.4\u3001SpringBoot\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8fc7\u6ee4\u5668<\/strong><\/p>\n\n\n\n<p>\uff081\uff09\u9996\u5148\u968f\u4fbf\u5199\u4e00\u4e2a\u63a7\u5236\u5668Controller<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\n@Slf4j\n@RequestMapping(\"\/api\/filter\")\npublic class FilterUserController {\n    @GetMapping(\"\/getUserList\")\n    public List&lt;String> getUser() {\n        log.info(\"\u5f00\u59cb\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u3002\");\n        List&lt;String> list = new ArrayList&lt;>();\n        list.add(\"\u5f20\u4e09\");\n        list.add(\"\u674e\u56db\");\n        list.add(\"\u738b\u4e94\");\n        log.info(\"\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u7ed3\u675f\u3002\");\n        return list;\n    }\n}<\/pre>\n\n\n\n<p><strong>\uff082\uff09\u5728\u542f\u52a8\u7c7b\u6dfb\u52a0\u4e00\u4e2a\u6ce8\u89e3\uff0c\u627e\u5230\u5b9a\u4e49\u7684\u62e6\u622a\u5668\u3002<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@ServletComponentScan(basePackages = \"com.binlog.study.filter\")<\/pre>\n\n\n\n<p><strong>\uff083\uff09\u5199\u4e00\u4e2a\u8fc7\u6ee4\u5668\uff0c\u5b9e\u73b0Filter<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Slf4j\n@Order(1)  \/\/\u5982\u679c\u6709\u591a\u4e2aFilter\uff0c\u5219\u5e8f\u53f7\u8d8a\u5c0f\uff0c\u8d8a\u65e9\u88ab\u6267\u884c\n\/\/@Component\/\/\u65e0\u9700\u6dfb\u52a0\u6b64\u6ce8\u89e3\uff0c\u5728\u542f\u52a8\u7c7b\u6dfb\u52a0@ServletComponentScan\u6ce8\u89e3\u540e\uff0c\u4f1a\u81ea\u52a8\u5c06\u5e26\u6709@WebFilter\u7684\u6ce8\u89e3\u8fdb\u884c\u6ce8\u5165\uff01\n\/\/\u8fd9\u91cc\u7684urlPatterns\u4e3a\u63a5\u53e3\u91cc\u7684\u8def\u5f84\u8fc7\u6ee4\u6761\u4ef6\n@WebFilter(filterName = \"timeFilter\", urlPatterns = \"\/api\/filter\/*\")\npublic class TimeFilter implements Filter {\n    @Override\n    public void init(FilterConfig filterConfig) throws ServletException {\n        log.info(\"\u521d\u59cb\u5316\u8fc7\u6ee4\u5668\uff1a{}\", filterConfig.getFilterName());\n    }\n    @Override\n    public void destroy() {\n        log.info(\"\u9500\u6bc1\u8fc7\u6ee4\u5668\");\n    }\n    @Override\n    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {\n        log.info(\"\u5f00\u59cb\u6267\u884c\");\n        long startTime = System.currentTimeMillis();\n        filterChain.doFilter(servletRequest, servletResponse);\n        long endTime = System.currentTimeMillis();\n        log.info(\"\u8bf7\u6c42\uff1a{}\uff0c\u8017\u65f6\uff1a{}ms\", getUrlFrom(servletRequest), (endTime - startTime));\n        log.info(\"\u7ed3\u675f\u6267\u884c\");\n    }\n    private String getUrlFrom(ServletRequest servletRequest) {\n        if (servletRequest instanceof HttpServletRequest) {\n            return ((HttpServletRequest) servletRequest).getRequestURL().toString();\n        }\n        return \"\";\n    }\n}<\/pre>\n\n\n\n<p><strong>\uff084\uff09\u9879\u76ee\u542f\u52a8\u540e\uff0c\u63a7\u5236\u53f0\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/strong><\/p>\n\n\n\n<p>\u9879\u76ee\u542f\u52a8\u540e\uff0c\u5c31\u5df2\u7ecf\u521d\u59cb\u5316\u8fc7\u6ee4\u5668\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-1024x207.png\" alt=\"\" class=\"wp-image-12659\" width=\"509\" height=\"102\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-1024x207.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-300x61.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-768x155.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-830x168.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-230x46.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-350x71.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45-480x97.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-45.png 1396w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/figure><\/div>\n\n\n\n<p>\u6d4f\u89c8\u5668\u9875\u9762\u8c03\u7528\u4e00\u4e0b\u63a5\u53e3\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=http%3A\/\/localhost%3A8060\/api\/filter\/getUserList\" target=\"_blank\">http:\/\/localhost:8060\/api\/filter\/getUserList<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-1024x185.png\" alt=\"\" class=\"wp-image-12660\" width=\"530\" height=\"95\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-1024x185.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-300x54.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-768x138.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-830x150.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-230x41.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-350x63.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46-480x87.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-46.png 1420w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/figure><\/div>\n\n\n\n<p>\u5173\u95ed\u9879\u76ee\u540e\uff0c\u8fc7\u6ee4\u5668\u4e5f\u9500\u6bc1\u4e86\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-1024x184.png\" alt=\"\" class=\"wp-image-12661\" width=\"521\" height=\"93\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-1024x184.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-300x54.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-768x138.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-830x149.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-230x41.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-350x63.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47-480x86.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-47.png 1366w\" sizes=\"(max-width: 521px) 100vw, 521px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2\u3001\u8ba4\u8bc6\u62e6\u622a\u5668\uff08Interceptor\uff09<\/strong><\/p>\n\n\n\n<p><strong>2.1\u3001\u62e6\u622a\u5668\u7684\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>SpringMVC\u7684\u5904\u7406\u5668\u62e6\u622a\u5668\u7c7b\u4f3c\u4e8eServlet\u5f00\u53d1\u4e2d\u7684\u8fc7\u6ee4\u5668Filter,\u7528\u4e8e\u5bf9\u5904\u7406\u5668\u8fdb\u884c\u9884\u5904\u7406\u548c\u540e\u5904\u7406\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e9b\u62e6\u622a\u5668\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u62e6\u622a\u5668\u5b83\u662f\u94fe\u5f0f\u8c03\u7528\uff0c\u4e00\u4e2a\u5e94\u7528\u4e2d\u53ef\u4ee5\u540c\u65f6\u5b58\u5728\u591a\u4e2a\u62e6\u622a\u5668Interceptor\uff0c\u4e00\u4e2a\u8bf7\u6c42\u4e5f\u53ef\u4ee5\u89e6\u53d1\u591a\u4e2a\u62e6\u622a\u5668\uff0c\u800c\u6bcf\u4e2a\u62e6\u622a\u5668\u7684\u8c03\u7528\u4f1a\u4f9d\u636e\u5b83\u7684\u58f0\u660e\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u3002<\/p>\n\n\n\n<p><strong>2.2\u3001\u62e6\u622a\u5668\u7684\u6838\u5fc3API<\/strong><\/p>\n\n\n\n<p>SpringMVC\u62e6\u622a\u5668\u63d0\u4f9b\u4e09\u4e2a\u65b9\u6cd5\u5206\u522b\u662fpreHandle\u3001postHandle\u3001afterCompletion\uff0c\u6211\u4eec\u5c31\u662f\u901a\u8fc7\u91cd\u5199\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u6765\u5bf9\u7528\u6237\u7684\u8bf7\u6c42\u8fdb\u884c\u62e6\u622a\u5904\u7406\u7684\u3002<\/p>\n\n\n\n<ul><li><strong>preHandle()<\/strong> \uff1a\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u5728\u8bf7\u6c42\u5904\u7406\u4e4b\u524d\u8fdb\u884c\u8c03\u7528\u3002<strong>\u300c\u6ce8\u610f\u300d\uff1a\u5982\u679c\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4e3afalse \uff0c\u5c06\u89c6\u4e3a\u5f53\u524d\u8bf7\u6c42\u7ed3\u675f\uff0c\u4e0d\u4ec5\u81ea\u8eab\u7684\u62e6\u622a\u5668\u4f1a\u5931\u6548\uff0c\u8fd8\u4f1a\u5bfc\u81f4\u5176\u4ed6\u7684\u62e6\u622a\u5668\u4e5f\u4e0d\u518d\u6267\u884c\u3002<\/strong><\/li><li><strong>postHandle()<\/strong>\uff1a\u53ea\u6709\u5728 preHandle() \u65b9\u6cd5\u8fd4\u56de\u503c\u4e3atrue \u65f6\u624d\u4f1a\u6267\u884c\u3002\u4f1a\u5728Controller \u4e2d\u7684\u65b9\u6cd5\u8c03\u7528\u4e4b\u540e\uff0cDispatcherServlet \u8fd4\u56de\u6e32\u67d3\u89c6\u56fe\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u300c\u6709\u610f\u601d\u7684\u662f\u300d\uff1apostHandle() \u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u987a\u5e8f\u8ddf preHandle() \u662f\u76f8\u53cd\u7684\uff0c\u5148\u58f0\u660e\u7684\u62e6\u622a\u5668 preHandle() \u65b9\u6cd5\u5148\u6267\u884c\uff0c\u800cpostHandle()\u65b9\u6cd5\u53cd\u800c\u4f1a\u540e\u6267\u884c\u3002<\/li><li><strong>afterCompletion()<\/strong>\uff1a\u53ea\u6709\u5728 preHandle() \u65b9\u6cd5\u8fd4\u56de\u503c\u4e3atrue \u65f6\u624d\u4f1a\u6267\u884c\uff0c\u5728\u6574\u4e2a\u8bf7\u6c42\u7ed3\u675f\u4e4b\u540e\uff0c DispatcherServlet \u6e32\u67d3\u4e86\u5bf9\u5e94\u7684\u89c6\u56fe\u4e4b\u540e\u6267\u884c\u3002<\/li><\/ul>\n\n\n\n<p><strong>2.3\u3001SpringBoot\u5b9e\u73b0\u4e00\u4e2a\u767b\u5f55\u62e6\u622a\u5668<\/strong><\/p>\n\n\n\n<p>\u9884\u60f3\uff1a\u7528\u6237\u5728\u8bbf\u95ee\u9996\u9875\u63a5\u53e3\uff0c\u5148\u5224\u65ad\u4e00\u4e0bsession\uff0c\u5982\u679csession\u4e2d\u6709user\u7684\u4fe1\u606f\uff0c\u8bf4\u660e\u7528\u6237\u5df2\u7ecf\u767b\u5f55\u8fc7\u4e86\uff0c\u80fd\u6b63\u5e38\u8bbf\u95ee\u9996\u9875\u63a5\u53e3\uff0c\u5426\u5219\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\uff0c\u8ba9\u7528\u6237\u8fdb\u884c\u767b\u5f55\u3002<\/p>\n\n\n\n<p><strong>\uff081\uff09\u9996\u5148\u968f\u4fbf\u5b9a\u4e49\u4e00\u4e2a\u5b9e\u4f53\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Data\npublic class InterceptorUserEntity {\n    private Integer id;\n    private String name;\n}<\/pre>\n\n\n\n<p><strong>\uff082\uff09\u5728controller\u91cc\u51993\u4e2a\u63a5\u53e3<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\n@Slf4j\n@RequestMapping(\"\/api\/interceptor\")\npublic class InterceptorUserController {\n    @GetMapping(\"\/setSession\")\n    @ResponseBody\n    public Object setSession(HttpServletRequest request) {\n        \/\/\u5c06\u7528\u6237\u4fe1\u606f\u5b58\u653e\u5230session\u4e2d\n        InterceptorUserEntity user = new InterceptorUserEntity();\n        user.setId(001);\n        user.setName(\"\u5f20\u4e09\");\n        request.getSession().setAttribute(\"user\", user);\n        return \"\u5df2\u8fdb\u884c\u767b\u5f55\uff01\";\n    }\n    \/**\n     * \u7528\u6237\u767b\u5f55\u540e\u8df3\u8f6c\u5230\u9996\u9875\n     *\n     * @return\n     *\/\n    @GetMapping(\"\/index\")\n    public Object index() {\n        return \"\u8fd9\u91cc\u662f\u9996\u9875\uff01\";\n    }\n    \/**\n     * \u767b\u5f55\u9875\u9762\n     *\n     * @return\n     *\/\n    @GetMapping(\"\/login\")\n    public Object login() {\n        return \"\u8bf7\u8fdb\u884c\u767b\u5f55\uff01\";\n    }\n}<\/pre>\n\n\n\n<p><strong>\uff083\uff09\u7f16\u5199\u62e6\u622a\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u8981\u5b9a\u4e49\u7684Interceptor\u7c7b\u5b9e\u73b0handlerInterceptor\u63a5\u53e3\u3002<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\n@Slf4j\npublic class UserInterceptor implements HandlerInterceptor {\n    @Override\n    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {\n        \/\/\u4e1a\u52a1\u62e6\u622a\u76f8\u5173\u89c4\u5219\n        \/\/\u4ecesession\u4e2d\u83b7\u53d6\u7528\u6237\u7684\u4fe1\u606f\n        InterceptorUserEntity user = (InterceptorUserEntity) request.getSession().getAttribute(\"user\");\n        \/\/\u5224\u65ad\u7528\u6237\u662f\u5426\u767b\u5f55\n        if (null == user) {\n            response.sendRedirect(request.getContextPath() + \"\/api\/interceptor\/login\");\n            return false;\n        }\n        \/\/\u9700\u8981\u8fd4\u56detrue\uff0c\u5426\u5219\u8bf7\u6c42\u4e0d\u4f1a\u88ab\u63a7\u5236\u5668\u5904\u7406\n        return true;\n    }\n    @Override\n    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {\n        log.info(\"\u8bf7\u6c42\u5904\u7406\u4e4b\u540e\u8fdb\u884c\u8c03\u7528\uff0c\u4f46\u662f\u5728\u89c6\u56fe\u88ab\u6e32\u67d3\u4e4b\u524d\uff08Controller\u65b9\u6cd5\u8c03\u7528\u4e4b\u540e\uff09\uff0c\u5982\u679c\u5f02\u5e38\u53d1\u751f\uff0c\u5219\u8be5\u65b9\u6cd5\u4e0d\u4f1a\u88ab\u8c03\u7528\");\n    }\n    @Override\n    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {\n        log.info(\"\u5728\u6574\u4e2a\u8bf7\u6c42\u7ed3\u675f\u4e4b\u540e\u88ab\u8c03\u7528\uff0c\u4e5f\u5c31\u662f\u5728DispatcherServlet\u6e32\u67d3\u4e86\u5bf9\u5e94\u7684\u89c6\u56fe\u4e4b\u540e\u6267\u884c\uff08\u4e3b\u8981\u662f\u7528\u4e8e\u8fdb\u884c\u8d44\u6e90\u6e05\u7406\u5de5\u4f5c\uff09\");\n    }\n}<\/pre>\n\n\n\n<p><strong>\uff084\uff09\u4f7f\u7528@Configuration\u6ce8\u89e3\u5199\u4e00\u4e2a\u62e6\u622a\u5668\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class InterceptorConfig implements WebMvcConfigurer {\n    @Autowired\n    private UserInterceptor userInterceptor;\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(userInterceptor).addPathPatterns(\"\/api\/interceptor\/**\").excludePathPatterns(\"\/**\/login\", \"\/**\/setSession\");\n    }\n}<\/pre>\n\n\n\n<p>.addPathPatterns\u8868\u793a\u4f5c\u7528\u8303\u56f4\u3002\uff08\u53ea\u5728\u8fd9\u4e2ainterceptor\u4e0b\u7684\u6240\u6709\u63a5\u53e3\u8fdb\u884c\u62e6\u622a\uff09<\/p>\n\n\n\n<p>.excludePathPatterns\u8868\u793a\u653e\u884c\u3002\u8fd9\u91cc\u628a\u767b\u5f55\u9875\u9762\u548c\u5df2\u767b\u5f55\u5b8c\u6210\uff08setSession\uff09\u653e\u884c\u3002<\/p>\n\n\n\n<p>\uff08\u5176\u5b83\u63a5\u53e3\u90fd\u4f1a\u88ab\u62e6\u622a\uff0c\u7136\u540e\u8df3\u8f6c\u5230login\u9875\u9762\uff09<\/p>\n\n\n\n<p>\u5728\u7528\u6237\u6ca1\u6709\u8bf7\u6c42\u8fc7 \/interceptor\/setsession\u7684\u65f6\u5019\uff0c\u5982\u679c\u7528\u6237\u8bf7\u6c42\u4e86 \/interceptor\/\u62e6\u622a\u5668\u5c31\u4f1a\u53d1\u6325\u4f5c\u7528\uff0c \u628a\u5b83\u8df3\u8f6c\u5230\/user\/login\u7684\u63a5\u53e3\u4e0a\u53bb\uff0c\u5982\u679c\u7528\u6237\u8bf7\u6c42\u8fc7\/interceptor\/setsession\u7684\u8bdd\uff0c \u518d\u53bb\u8bf7\u6c42\/user\/index\u62e6\u622a\u5668\u5c31\u4f1a\u653e\u884c\uff0c\u8bf7\u6c42\u5230\u76f8\u5e94\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p><strong>3\u3001\u8fc7\u6ee4\u5668\u4e0e\u62e6\u622a\u5668\u7684\u533a\u522b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u76f8\u540c\u70b9\uff1a<\/strong><\/p>\n\n\n\n<p>\u8fc7\u6ee4\u5668\u4e0e\u62e6\u622a\u5668\u90fd\u4f53\u73b0\u4e86AOP\u7684\u7f16\u7a0b\u601d\u60f3\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0\u4f8b\u5982\u65e5\u5fd7\u3001\u767b\u5f55\u9274\u6743\u7b49\u529f\u80fd\u3002<\/p>\n\n\n\n<p><strong>\u4e0d\u540c\u70b9\uff1a<\/strong><\/p>\n\n\n\n<p>\u2460\uff1a\u62e6\u622a\u5668\u662f\u57fa\u4e8ejava\u7684\u53cd\u5c04\u673a\u5236\uff08\u52a8\u6001\u4ee3\u7406\uff09\u7684\u5b9e\u73b0\uff0c\u800c\u8fc7\u6ee4\u5668\u662f\u57fa\u4e8e\u51fd\u6570\u7684\u56de\u8c03\u3002<\/p>\n\n\n\n<p>\u2461\uff1a\u62e6\u622a\u5668\u4e0d\u4f9d\u8d56\u4e8eservlet\u5bb9\u5668\uff0c\u800c\u8fc7\u6ee4\u5668\u4f9d\u8d56\u4e8eservlet\u5bb9\u5668\u3002<\/p>\n\n\n\n<p>\u2462\uff1a\u62e6\u622a\u5668\u53ea\u5bf9Controller\u8bf7\u6c42\u8d77\u4f5c\u7528\uff0c\u800c\u8fc7\u6ee4\u5668\u5219\u53ef\u4ee5\u5bf9\u51e0\u4e4e\u6240\u6709\u7684\u8bf7\u6c42\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u2463\uff1a\u62e6\u622a\u5668\u53ef\u4ee5\u8bbf\u95eeController\u4e0a\u4e0b\u6587\u3001\u503c\u3001\u6808\u91cc\u9762\u7684\u5bf9\u8c61\uff0c\u800c\u8fc7\u6ee4\u5668\u4e0d\u53ef\u4ee5\u3002<\/p>\n\n\n\n<p>\u2464\uff1a\u5728spring\u5bb9\u5668\u7684\u751f\u547d\u5468\u671f\u4e2d\uff0c\u62e6\u622a\u5668\u53ef\u4ee5\u591a\u6b21\u88ab\u8c03\u7528\uff0c\u800c\u8fc7\u6ee4\u5668\u53ea\u80fd\u5728\u5bb9\u5668\u521d\u59cb\u5316\u65f6\u88ab\u8c03\u7528\u4e00\u6b21\u3002<\/p>\n\n\n\n<p>\u2465\uff1a\u62e6\u622a\u5668\u53ef\u4ee5\u83b7\u53d6IOC\u5bb9\u5668\u4e2d\u7684\u5404\u4e2abean\uff0c\u800c\u8fc7\u6ee4\u5668\u4e0d\u884c\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u5728\u62e6\u622a\u5668\u91cc\u6ce8\u5165\u4e00\u4e2aservice\uff0c\u53ef\u4ee5\u8c03\u7528\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n\n\n\n<p><strong>\u89e6\u53d1\u673a\u5236\u4e0d\u540c<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-1024x785.png\" alt=\"\" class=\"wp-image-12662\" width=\"385\" height=\"294\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-1024x785.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-300x230.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-768x589.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-830x636.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-230x176.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-350x268.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48-480x368.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-48.png 1088w\" sizes=\"(max-width: 385px) 100vw, 385px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fc7\u6ee4\u5668Filter\u662f\u5728\u8bf7\u6c42\u8fdb\u5165\u5bb9\u5668\u540e\uff0c\u4f46\u5728\u8fdb\u5165servlet\u4e4b\u524d\u8fdb\u884c\u9884\u5904\u7406\uff0c\u8bf7\u6c42\u7ed3\u675f\u662f\u5728servlet\u5904\u7406\u5b8c\u4ee5\u540e\u3002<\/p>\n\n\n\n<p>\u62e6\u622a\u5668 Interceptor \u662f\u5728\u8bf7\u6c42\u8fdb\u5165servlet\u540e\uff0c\u5728\u8fdb\u5165Controller\u4e4b\u524d\u8fdb\u884c\u9884\u5904\u7406\u7684\uff0cController \u4e2d\u6e32\u67d3\u4e86\u5bf9\u5e94\u7684\u89c6\u56fe\u4e4b\u540e\u8bf7\u6c42\u7ed3\u675f\u3002<\/p>\n\n\n\n<p>\u8fc7\u6ee4\u5668\u51e0\u4e4e\u53ef\u4ee5\u5bf9\u6240\u6709\u8fdb\u5165\u5bb9\u5668\u7684\u8bf7\u6c42\u8d77\u4f5c\u7528\uff0c\u800c\u62e6\u622a\u5668\u53ea\u4f1a\u5bf9Controller\u4e2d\u8bf7\u6c42\u6216\u8bbf\u95eestatic\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90\u8bf7\u6c42\u8d77\u4f5c\u7528\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-1024x562.png\" alt=\"\" class=\"wp-image-12663\" width=\"428\" height=\"235\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-1024x562.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-300x165.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-768x422.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-830x456.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-230x126.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-350x192.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49-480x263.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-49.png 1126w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u9644\uff1aChatGpt\u7684\u56de\u7b54<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-1024x877.png\" alt=\"\" class=\"wp-image-12665\" width=\"470\" height=\"402\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-1024x877.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-300x257.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-768x658.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-830x711.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-230x197.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-350x300.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50-480x411.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-50.png 1364w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4.\u4e3a\u4ec0\u4e48\u8bf4Filter\u662f\u57fa\u4e8e\u51fd\u6570\u56de\u8c03\u7684?<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u76f8\u4fe1\u7ecf\u5e38\u67e5\u9605\u535a\u5ba2\u7684\u670b\u53cb\u4eec\u90fd\u77e5\u9053\uff0c\u6211\u4eec\u53bb\u67e5\u9605<strong>Filter<\/strong>\u548c<strong>Interceptor<\/strong>\u7684\u533a\u522b\u7684\u6587\u7ae0\u65f6\uff0c\u7ecf\u5e38\u6709\u8fd9\u4e48\u4e00\u53e5\u8bdd\uff1a<\/p>\n\n\n\n<ul><li>\u8fc7\u6ee4\u5668\uff0c\u4f9d\u8d56\u4e8eservlet\u5bb9\u5668\uff0c\u5728\u5b9e\u73b0\u4e0a\u57fa\u4e8e\u51fd\u6570\u56de\u8c03\u3002<\/li><li>\u62e6\u622a\u5668\uff0c\u4f9d\u8d56\u4e8e\u4f9d\u8d56\u4e8eweb\u6846\u67b6\uff0c\u5728\u5b9e\u73b0\u4e0a\u57fa\u4e8eJava\u7684\u53cd\u5c04\u673a\u5236\uff0c\u5c5e\u4e8e\u9762\u5411\u5207\u9762\u7f16<\/li><li>\u7a0b\uff08AOP\uff09\u7684\u4e00\u79cd\u8fd0\u7528\u3002<\/li><\/ul>\n\n\n\n<p>\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8bf4Filter\u662f\u57fa\u4e8e\u51fd\u6570\u56de\u8c03\u7684\u5462\uff1f\u51fd\u6570\u56de\u8c03\u53c8\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u51fd\u6570\u56de\u8c03<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A callback is a function that is passed as an argument to another function and is executed after its parent function has completed\u3002<\/pre>\n\n\n\n<p>\u7ffb\u8bd1\uff1a\u56de\u8c03\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\uff0c\u5728\u7236\u51fd\u6570\u5b8c\u6210\u540e\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u7b80\u5355\u70b9\u8bf4\u5c31\u662f\u67092\u4e2a\u7c7b\uff0cA\u548cB\uff0cA\u6301\u6709B\u7684\u5f15\u7528\uff0c\u5f53A\u4e2d\u8c03\u7528B\u7684\u65b9\u6cd5\u65f6\u5c31\u662f\u56de\u8c03 \u5373\uff1aA\uff08B\uff09<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u770b\u770bFilter\u7684\u5de5\u4f5c\u56fe\uff1a<\/p>\n\n\n\n<p>FilterChain\u662fFilter\u7684\u94fe\u8def\uff0c\u6bcf\u6b21url\u8bf7\u6c42\u8fc7\u6765\u7684\u65f6\u5019\u5c31\u4f1a\u8fdb\u5165\u94fe\u8def\u7136\u540e\u6839\u636e\u4e8b\u5148\u8bbe\u5b9a\u597d\u7684Filter\u987a\u5e8f\u6309\u7167\u987a\u5e8f\u8fc7\u6ee4\u4e0b\u53bb\uff0c\u6bcf\u6b21Filter\u5b8c\u6210\u4e86\u81ea\u5df1\u7684\u64cd\u4f5c\u540e\u5c31\u4f1a\u8c03\u7528filterChain.doFilter(servletRequest, servletResponse);\u5c06\u8bf7\u6c42\u53d1\u5230FilterChain\u4e2d\u7684\u4e0b\u4e00\u4e2aFilter\u5904\u7406\uff0c\u5982\u679c\u6ca1\u6709\u4e0b\u4e00\u4e2a\uff0c\u5c31\u76f4\u63a5\u653e\u884c\uff0c\u7136\u540e\u8fdb\u5165servlet\u83b7\u53d6\u8d44\u6e90<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-1024x669.png\" alt=\"\" class=\"wp-image-12668\" width=\"464\" height=\"302\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-1024x669.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-300x196.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-768x502.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-830x542.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-230x150.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51-350x229.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-51.png 1264w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/figure><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52.png\" alt=\"\" class=\"wp-image-12669\" width=\"430\" height=\"202\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52.png 802w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52-300x141.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52-768x360.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52-230x108.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52-350x164.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/09\/image-52-480x225.png 480w\" sizes=\"(max-width: 430px) 100vw, 430px\" \/><\/figure><\/div>\n\n\n\n<p>Filter\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.hxkj.rbac.filter;\n\nimport javax.servlet.*;\nimport java.io.IOException;\n\n\/** \u5b57\u7b26\u7f16\u7801\u8fc7\u6ee4\u5668\n * Create by wangbin\n * 2019-03-28-14:52\n *\/\npublic class EncodingFilter implements Filter {\n\n    private String encode;\n\n    @Override\n    public void init(FilterConfig filterConfig) throws ServletException {\n        \/\/Filter\u521d\u59cb\u5316\n        \/\/ \u8bfb\u53d6web.xml\u4e2dFilter\u914d\u7f6e\u7684\u521d\u59cb\u5316\u53c2\u6570\n        encode = filterConfig.getInitParameter(\"encode\");\n    }\n\n    @Override\n    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {\n        \/\/ \u8bbe\u7f6e\u521d\u59cb\u5316\u7684\u53c2\u6570encode\n        servletRequest.setCharacterEncoding(encode);\n        servletResponse.setCharacterEncoding(encode);\n        servletResponse.setContentType(\"text\/html; charset=UTF-8\");\n        filterChain.doFilter(servletRequest, servletResponse); \/\/ \u653e\u884c\uff0c\u8f6c\u5230\u4e0b\u4e00\u4e2a\u8fc7\u6ee4\u5668\n\n    }\n\n    @Override\n    public void destroy() {\n        \/\/\u5728Filter\u9500\u6bc1\u524d\uff0c\u5b8c\u6bd5\u67d0\u4e9b\u8d44\u6e90\u7684\u56de\u6536\n    }\n}\n<\/pre>\n\n\n\n<p>\u4eceFilter\u7684 doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) \u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cFilterChain\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u4e86\u8fdb\u6765\uff0c<br>\u5f53\u524d\u7684\u5b57\u7b26\u8fc7\u6ee4Filter\u5c06\u5b57\u7b26\u7f16\u7801\u8bbe\u7f6e\u597d\u540e\uff0c\u8c03\u7528\u4e86FilterChain\u7684 filterChain.doFilter(servletRequest, servletResponse); \u8ba9FilterChain\u53bb\u627e\u4e0b\u4e00\u4e2aFilter\u8fdb\u884c\u65b0\u7684\u5904\u7406\uff0c\u5982\u679c\u6ca1\u6709\u4e0b\u4e00\u4e2aFilter\u4e86\uff0c\u5c31\u76f4\u63a5\u653e\u884c\uff0c\u8bbf\u95eeservlet\u83b7\u53d6\u8d44\u6e90\uff0c\u8fd9\u5c31\u662f\u5178\u578b\u7684\u4e00\u4e2a\u51fd\u6570\u56de\u8c03\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Filter\u5c31\u76f8\u5f53\u4e8e\u7c7bA\nFilterChain\u5c31\u76f8\u5f53\u4e8e\u7c7bB\nA\uff08B\uff09<\/pre>\n\n\n\n<p>FilterChain\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u4e86Filter\u7684doFilter()\u65b9\u6cd5\uff0c\u7136\u540e\u5728\u8be5\u65b9\u6cd5\u4e2d\u53c8\u8c03\u7528\u4e86FilterChain\u7684doFilter()\u65b9\u6cd5\uff0c\u5373\u4e3a\uff1a\u51fd\u6570\u56de\u8c03\uff0c\u8fd9\u4e5f\u5c31\u662f\u4e3a\u4ec0\u4e48\u8bf4Filter\u662f\u57fa\u4e8e\u51fd\u6570\u56de\u8c03\u7684\u539f\u56e0\u4e86<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u8ba4\u8bc6\u8fc7\u6ee4\u5668\uff08Filter\uff09 1.1\u3001\u8fc7\u6ee4\u5668\u7684\u5b9a\u4e49 \u8fc7\u6ee4\u5668\u662fJavaWeb\u7684\u4e09\u5927\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u662f\u5b9e\u73b0Filter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12658"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12658"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12658\/revisions"}],"predecessor-version":[{"id":12670,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/12658\/revisions\/12670"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}