如何在springboot中使用过滤器

前言

记录软件开发点滴,积累知识和经验(第六篇)

要做什么?

目标:在springboot配置过滤器

怎么去做?

  • 编写过滤器继承OncePerRequestFilter(JwtAuthenticationFilter)
package com.wyl.filters;

import com.wyl.utils.JwtUtil;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName JwtAuthenticationFilter
 * @Description 过滤器
 * @Author yilongwu
 * @DATE 2019-06-14 09:43
 * @Version 1.0.0
 **/
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    private static final PathMatcher pathMatcher = new AntPathMatcher();

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        HttpSession session = request.getSession(true);
        try {
            if(!isProtectedUrl(request)){
                String token = request.getHeader("X-ToKen");

                // 验证token是否合法,返回管理员名称
                String name = JwtUtil.validateToken(token);
                // 用session保存name
                session.setAttribute("name",name);
            }
        }catch (Exception e){
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,e.getMessage());
        }

        // 如果jwt令牌通过了检测, 那么就把request传递给后面的RESTful api
        filterChain.doFilter(request, response);
    }

    //匹配合法的url
    private boolean isProtectedUrl(HttpServletRequest request){

        return  pathMatcher.match("/login/**",request.getServletPath()) ||
                pathMatcher.matchStart("/swagger-ui.html",request.getServletPath()) ||
                pathMatcher.match("/webjars/**",request.getServletPath()) ||
                pathMatcher.match("/swagger-resources/**",request.getServletPath()) ||
                pathMatcher.match("/v2/**",request.getServletPath());
    }


}

解析关键词

OncePerRequestFilter: 每次请求只过滤一次
AntPathMatcher:路径匹配类
JwtUtil: 这是我做JWT验证用的,不用理会
ps:如果想知道成功与否,就在doFilterInterna的方法里打印一句话即可

  • 配置过滤器,将过滤器注入到Spring容器中
package com.wyl.config;

import com.wyl.filters.JwtAuthenticationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName FilterConfigs
 * @Description 过滤器的配置
 * @Author yilongwu
 * @DATE 2019-06-14 10:19
 * @Version 1.0.0
 **/
@Configuration
public class FilterConfigs {


    /**
     * 把过滤器注入spring容器
     * @return
     */
    @Bean
    public FilterRegistrationBean jwtFilter() {
        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        JwtAuthenticationFilter filter = new JwtAuthenticationFilter();
        registrationBean.setFilter(filter);
        return registrationBean;
    }
}

结果怎么样?

启动springboot的项目,自己测试吧!!!!!
有问题请留言-------


如何在springboot中使用过滤器_第1张图片

你可能感兴趣的:(java)