欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 配置Filter过滤器的方法

程序员文章站 2024-03-02 20:19:46
filter 过滤器是web开发中很重要的一个组件,下面以一个session登陆的例子介绍下spring boot中如何使用filter 首先要准备一个实现了filt...

filter 过滤器是web开发中很重要的一个组件,下面以一个session登陆的例子介绍下spring boot中如何使用filter

首先要准备一个实现了filter的接口的类 sessionfilter:

import org.slf4j.loggerfactory;

import javax.servlet.*;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
import java.util.regex.matcher;
import java.util.regex.pattern;

/**
 * created by mazhenhua on 2016/12/27.
 *
 * 过滤器
 */
public class sessionfilter implements filter {
  private static final org.slf4j.logger logger = loggerfactory.getlogger(sessionfilter.class);


  /**
   * 封装,不需要过滤的list列表
   */
  protected static list<pattern> patterns = new arraylist<pattern>();

  @override
  public void init(filterconfig filterconfig) throws servletexception {

  }

  @override
  public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain chain) throws ioexception, servletexception {
    httpservletrequest httprequest = (httpservletrequest) servletrequest;
    httpservletresponse httpresponse = (httpservletresponse) servletresponse;
    logger.info("aaaaaaaaaa");
    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("") != null){
        // session存在
        chain.dofilter(httprequest, httpresponse);
        return;
      } else {
        // session不存在 准备跳转失败
        /* requestdispatcher dispatcher = request.getrequestdispatcher(path);
          dispatcher.forward(request, response);*/
        chain.dofilter(httprequest, httpresponse);
        return;
      }
    }


  }

  @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;
  }

}

实际开发中往往有很多请求要直接请求进来,不需要鉴权登陆的,所以代码中过滤掉这种请求的代码,装进list就好了。

  /**
   * 配置过滤器
   * @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();
  }

经过上面这俩步的配置,过滤器基本上就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。