• 猿创征文|瑞吉外卖——管理端_过滤器


    个人名片:

    博主酒徒ᝰ.
    专栏瑞吉外卖
    个人简介沉醉在酒中,借着一股酒劲,去拼搏一个未来。

    本项目基于B站黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis plus开发核心技术的真java实战项目。

    视频链接【黑马程序员Java项目实战《瑞吉外卖》,轻松掌握springboot + mybatis
    plus开发核心技术的真java实战项目】 https://www.bilibili.com/video/BV13a411q753?
    点击观看

    过滤器代码较少,所有本篇大部分是笔记。

    一、过滤器设置

    前面为了方便,就一直没有使用过滤器,现在管理端已经完成。添加过滤器。

    package com.itheima.reggie.filter;
    
    import com.alibaba.fastjson.JSON;
    import com.itheima.reggie.common.R;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.util.AntPathMatcher;
    
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Slf4j
    @WebFilter(filterName = "CheckFilter", urlPatterns = "/*")
    public class CheckFilter implements Filter {
    
        //路径匹配器
        private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //1.转换为http形式
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
    
            //2.获取请求地址
            String requestURI = request.getRequestURI();
            //3.设置过滤地址
            String[] uris = new String[]{
                    "/employee/login",
                    "/employee/logout",
                    "/backend/**",
                    "/common/"
            };
    
            //4.判断是否包含以上地址
            for (String uri : uris) {
                boolean match = PATH_MATCHER.match(uri, requestURI);
                //包含,放行
                if (match){
                    filterChain.doFilter(request, response);
                    return;
                }
            }
    
            //5.判断是否登录,登录则放行
            if (request.getSession().getAttribute("employee") != null){
            	BaseContext.setCurrentId((Long)request.getSession().getAttribute("employee"));//一定要在下一步的放行之前
                filterChain.doFilter(request, response);
                return;
            }
    
            //6.未登录
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return;
        }
    }
    
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    二、笔记

    之后会写新的文章进行详细概述,这里就只写自己的理解了。

    1. @WebFilter

    直接使用过滤器不会生效@WebFilter(urlPatterns=“(需要过滤的地址)”, filterName=“(过滤器名称)”)
    需要在springboot启动类上加上@ServletComponentScan注解才会生效(推荐这种方式)

    /*
    *  请求被拦截的时候调用
    过滤器:拦截请求,对其进行相应操作后,放行
    */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
        //转换为HttpServletRequest形式,有利于之后的调用获取页面信息
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        //放行
        chain.doFilter(request, response);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    FilterChain:接口,该接口只有一个方法doFilter(ServletRequest request ,ServletResponse response)使用该方法可以调用过滤器链中的下一个 Filter 的 doFilter() 方法,若该 Filter 是链中最后一个过滤器,则调用目标资源的 service() 方法。

    2. NOTLOFIN详解

    返回登录页面
    response.getWriter().write(JSON.toJSONString(R.error(“NOTLOGIN”)));
    JSON.toJSONString:将HashMap类型数据返回成json字符串

    3.路径匹配器详解

    1. 概述

    Spring模型中的概念模型接口。
    Spring 为 PathMatcher 接口提供了一个默认实现 AntPathMatcher,支持 Ant 风格的路径匹配
    在这里插入图片描述

    2. 实现

    AntPathMatcher 方法
    private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    3. 方法:

    1. boolean isPattern(String path);
      a. 判断路径是否是pattent(模式),也就是以path/ * 结尾
    antPathMatcher.isPattern("/user/001");// 返回 false
    antPathMatcher.isPattern("/user/*"); // 返回 true
    
    • 1
    • 2
    1. boolean match(String pattern, String path);
      a. 检查path和pattern是否匹配,即path是pattern中的一部分。
    antPathMatcher.match("/user/001","/user/001");// 返回 true
    antPathMatcher.match("/user/*","/user/001");// 返回 true
    
    • 1
    • 2
    1. boolean matchStart(String pattern, String path);
      a. 检查path和pattern前缀名是否相同。
    antPathMatcher.matchStart("/user/*","/user/001"); // 返回 true
    antPathMatcher.matchStart("/user/*","/user"); // 返回 true
    antPathMatcher.matchStart("/user/*","/user001"); // 返回 false
    
    • 1
    • 2
    • 3
    1. String extractPathWithinPattern(String pattern, String path);
      a. 获取匹配的部分
    antPathMatcher.extractPathWithinPattern("uc/profile*","uc/profile.html"); // 返回 profile.html
    
    • 1
    1. String combine(String pattern1, String pattern2);
      a. 合并pattern1和pattern2
    antPathMatcher.combine("uc/*.html","uc/profile.html"); // uc/profile.html
    
    • 1
  • 相关阅读:
    unity操作_刚体 c#
    【MySQL】_JDBC
    MinIO Enterprise Cache:实现超性能的分布式 DRAM 缓存
    【Spring】——7、@Import注解给容器中快速导入一个组件
    Sublime合并生成,同时编辑三页代码和文本框
    IDEA上MySQL的jar包导入教程
    软件工程毕业设计课题(36)基于JAVA毕业设计JAVA新冠疫苗预约系统毕设作品项目
    优惠券的工厂与策略模式实现方案
    微信小程序在TS模板下引入TDesign组件
    Java基础系列(七)——多线程
  • 原文地址:https://blog.csdn.net/m0_65144570/article/details/126808405