Spring Boot设置过滤器

最近一直在做老项目的移植,以前写的过滤器希望直接能够用到springboot项目上,争取用最少代码完成最多的事情。
首先spring boot上没有默认项目名,公司要求,我这里设置了一个项目名:

//springboot2.0之前
server.context-path=/demo
//springboot2.0以后
server.servlet.context-path=/demo

这里参考了这篇文章写的东西。
https://blog.csdn.net/wushengjun753/article/details/80612652

然后掏出了祖传的过滤器,详情看代码,逻辑不难理解,自己跑一遍基本能懂。



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhirong.smsshui.base.util.Constants;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;
/*
 * 
 * */

public class SessionFilter extends OncePerRequestFilter{
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        // 不过滤的uri
        String[] notFilter = new String[] {"","login","index.jsp"};

        // 请求的uri
        String uri = request.getRequestURI();
        String requestPath = uri.substring(uri.lastIndexOf("/")+1,uri.length());

        // uri中包含background时才进行过滤

        if (uri.indexOf("demo") != -1) { 				//此处demo为项目名
            // 是否过滤
            boolean doFilter = true;
            for (String s : notFilter) {
                if (requestPath.equals(s) || uri.substring(uri.lastIndexOf(".")+1).equals("css")
                        ||uri.substring(uri.lastIndexOf(".")+1).equals("js")
                        ||uri.substring(uri.lastIndexOf(".")+1).equals("gif")
                        ||uri.substring(uri.lastIndexOf(".")+1).equals("jpg")){
                    // 如果uri中包含不过滤的uri,则不进行过滤
                    doFilter = false;
                    break;
                }
            }
            if (doFilter) {
                // 执行过滤
                // 从session中获取登录者实体
                Object obj = request.getSession().getAttribute(Constants.USER_INFO_SESSION);
                if (null == obj) {
                    response.setCharacterEncoding("utf-8");
                    response.setContentType("text/html;charset=utf-8");
                    // 如果session中不存在登录者实体,则弹出框提示重新登录
                    // 设置request和response的字符集,防止乱码
                    PrintWriter out = response.getWriter();

                    out.print("<script>alert('会话超时,请重新登录...'); document.location.href='index.jsp';</script>");
                    out.flush();
                    out.close();
                     	/*request.getRequestDispatcher("index.jsp").forward(request,response);*/

                } else {
                    // 如果session中存在登录者实体,则继续
                    filterChain.doFilter(request, response);
                }
            } else {
                // 如果不执行过滤,则继续
                filterChain.doFilter(request, response);
            }
        } else {
            // 如果uri中不包含background,则继续
            filterChain.doFilter(request, response);
        }
    }

}




哈哈,其实这个过滤器是我自己写的,为了迎合业务的需要,我增加了许多请求是直接通过过滤的,比如js,css之类的。
回到正题,然后我们编写一个过滤器设置类,这里参考了这位老哥写的后半段,代码如下
https://www.cnblogs.com/xiaoping1993/p/7873975.html

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class FilterConfig {

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

这样一来,我们的过滤器就生效了,你可以debug看看有没有用,拜拜,咕噜拜。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值