能配置例外

先写配置文件类

FilterConfig.java

package com.ty.tyzxtj.config;

import javax.servlet.Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.ty.tyzxtj.fiter.SessionFilter; /**
* 过滤器配置
* @author wangjiping
*
*/
@Configuration
public class FilterConfig {
/**
* 配置过滤器
* @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();
}
}

过滤器类:

  对通过过滤器的url请求都查看对应session有没有值没有就跳转到登陆页面

package com.ty.tyzxtj.fiter;

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("login/index.html"));
patterns.add(Pattern.compile("login/login"));
patterns.add(Pattern.compile("login.do"));
patterns.add(Pattern.compile("main/autoFillty_rj_situation.*"));
patterns.add(Pattern.compile("main/post.*"));
patterns.add(Pattern.compile(".*[(\\.js)||(\\.css)||(\\.png)]"));
} @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() > ) {
url = url.substring();
}
if (isInclude(url)){
chain.doFilter(httpRequest, httpResponse);
return;
} else {
HttpSession session = httpRequest.getSession();
if (session.getAttribute("loginName") != null){
// session存在
chain.doFilter(httpRequest, httpResponse);
return;
} else {
// session不存在 准备跳转失败
httpResponse.sendRedirect("login.do?toLogin");
}
}
} @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;
} }

注意:应用了过滤器,前提是所有请求都从服务器上走一次

    例如:如果直接在浏览器中输入链接是服务器静态资源文件可能因为浏览器缓存的原因直接访问浏览器的缓存页面没有走过滤器从而想要实现的验证用户登陆没有成功

springboot 配置过滤器的更多相关文章

  1. springboot配置过滤器和拦截器

    import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Http ...

  2. springboot 增加过滤器方法

    在访问服务器时,我们需要控制用户是否允许权限,这个时候可以使用过滤器. 在springboot 配置过滤器的方法如下: 编写过滤器代码: package com.neo.filter; import ...

  3. SpringBoot图文教程6—SpringBoot中过滤器的使用

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文系列教程技术大纲 鹿老师的Java笔记 SpringBo ...

  4. springboot中配置过滤器以及可能出现的问题

    在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...

  5. springboot环境下配置过滤器和拦截器

    以前我们在配置过滤器和拦截器的时候,都是一个类继承一个接口,然后在xml中配置一下就ok 但是,但是,这是springboot的环境,没有xml的配置.所以我们还要继续学习啊啊啊啊啊~~~~~ 先简单 ...

  6. 使用SpringBoot的方式配置过滤器

    springboot 不存在web.xml 那么如何配置过滤器呢 springboot提供了一种方式 通过spring容器配置 @Bean public FilterRegistrationBean ...

  7. 跨站点脚本编制 - SpringBoot配置XSS过滤器(基于mica-xss)

    1. 简介   XSS,即跨站脚本编制,英文为Cross Site Scripting.为了和CSS区分,命名为XSS.   XSS是最普遍的Web应用安全漏洞.这类漏洞能够使得攻击者嵌入恶意脚本代码 ...

  8. CAS学习笔记三:SpringBoot自动配置与手动配置过滤器方式集成CAS客户端

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式集成CAS客户端. 需要提前搭建 CAS 服务端,参考 https://www.cnblogs.com/hell ...

  9. springboot 配置多数据源

    1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...

随机推荐

  1. OSPF-lsa-types

  2. The Intriguing Obsession

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. 巨蟒python全栈开发django5:组件&&CBV&FBV&&装饰器&&ORM增删改查

    内容回顾: 补充反向解析 Html:{% url ‘别名’ 参数 %} Views:reverse(‘别名’,args=(参数,)) 模板渲染 变量 {{ 变量名 }} 逻辑相关 {% %} 过滤器: ...

  4. Selenium IDE的使用

    Selenium IDE 的作用 Selenium IDE 是Firefox 浏览器的一个插件, 它会记录你对Firefox的操作,并且可以回放它的操作. 在实际自动化测试中,不会用Selenium ...

  5. 【24】response对象以及Python3中的一些变化

    request.COOKIES    用来获取cookie response.write()      写的方法是response对象的 转自:博客园python3的变化 print 由一个语句(st ...

  6. python基础知识回顾[1]

    1.声明变量 # 声明一个变量name用来存储一个字符串'apollo' name = 'apollo' # 声明一个变量age用来存储一个数字20 age = 20 # 在控制台打印变量name中存 ...

  7. Linux中vim命令出现E325错误解决方法

    出现该问题的原因是: vim在编辑文件的时候会创建一个swp file来保证文件的安全性,如果没有正常退出vim的,下次打开这个文件就会报E325的错误,提示swp文件已经存在. 解决方法也简单:把这 ...

  8. Python学习笔记3_数据类型

    Python数据类型:数字.字符串.列表.元祖.字典 一.数字类型:(整型.长整型.浮点型.复数型) 1.整型(int):表示范围-2,147,483,648到2,147,483,647 2.长整型( ...

  9. KGX滚动分页源码

    源码描述: 本工具采用Jquery框架,通过jquery调用ashx获取并输出数据,示例中采用测试数据,可以自行扩展为图片等等 当下流行的分页方式,鼠标滚动下拉条会自动展示下一页信息,类似瀑布流的效果 ...

  10. RHEVM 相关介绍

    基础概念: RHEV-H RHEVH(Redhat Enterprise Virtuallization Hypervisor),它是运行虚拟机所需的最低操作系统.RHEVH由作为RHEL(Redha ...