springboot配置过滤器和拦截器

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SessionFilter implements Filter {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SessionFilter.class);

    @Value("$(serverurl)")
    private String serverurl;
/*
     * 封装,不需要过滤的list列表
     */

    protected static List<Pattern> patterns = new ArrayList<Pattern>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        patterns.add(Pattern.compile("loginq.do"));
        patterns.add(Pattern.compile("loginh.do"));
        patterns.add(Pattern.compile("share.do"));
        patterns.add(Pattern.compile("zhuce.do"));
        patterns.add(Pattern.compile("huiyuan.do"));
        patterns.add(Pattern.compile("login.do"));
        patterns.add(Pattern.compile("tiaozhuan.do"));
        patterns.add(Pattern.compile("register.do"));
        patterns.add(Pattern.compile("loginy.do"));
        patterns.add(Pattern.compile("jiali.do"));
        patterns.add(Pattern.compile("main/autoFillty_rj_situation.*"));
        patterns.add(Pattern.compile("main/post.*"));
        patterns.add(Pattern.compile("main/get.*"));
        patterns.add(Pattern.compile(".*[(\\.js)||(\\.css)||(\\.png)||(\\.tff)]"));
}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
        if (url.startsWith("/") && url.length() > 1) {
            url = url.substring(1);
        }
        if (isInclude(url)){
            chain.doFilter(httpRequest, httpResponse);
            return;
        } else {
            HttpSession session = httpRequest.getSession();
            if (session.getAttribute("currentUser") != null){
                // session存在
                chain.doFilter(httpRequest, httpResponse);
                return;
            } else {
                // session不存在 准备跳转失败
                httpResponse.sendRedirect("/loginy.do");
            }
        }
    }

    @Override
    public void destroy() {

    }

/*
     * 是否需要过滤
     * @param url
     * @return
     */

    private boolean isInclude(String url) {
        for (Pattern pattern : patterns) {
            Matcher matcher = pattern.matcher(url);
            if (matcher.matches()) {
                return true;
            }
        }
        return false;
    }

}

filter

在启动类里面添加

/**
     * 配置过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(sessionFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("sessionFilter");
        return registration;
    }

    /*
     * 创建一个bean
     * @return
     */
    @Bean(name = "sessionFilter")
    public Filter sessionFilter() {
        return new SessionFilter();
    }

registration.addUrlPatterns("/**");过滤所有请求
在这里插入图片描述
拦截器:
在启动类里面添加

@Bean//拦截器
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
   ServletRegistrationBean reg = new ServletRegistrationBean(dispatcherServlet);
   reg.getUrlMappings().clear();
   reg.addUrlMappings("*.html");
   reg.addUrlMappings("*.do");
   reg.addUrlMappings("*.js");
   reg.addUrlMappings("*.css");
   reg.addUrlMappings("*.txt");
   reg.addUrlMappings("*.woff");
   reg.addUrlMappings("*.ttf");
   reg.addUrlMappings("*.png");
   reg.addUrlMappings("*.jpg");
   return reg;
}

源码地址:https://www.cnblogs.com/NCL--/p/9104191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值