spring filter 配置
web xml
<filter><filter-name>DelegatingFilterProxy</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param><init-param><param-name>targetBeanName</param-name><param-value>autoLoginfilter</param-value></init-param></filter>
配置自动扫描注解
<context:component-scan base-package="cn.com.do1.important.filter" />
filter
package cn.com.do1.important.filter;import cn.com.do1.component.common.util.ValidateUtil;import cn.com.do1.dqdp.core.DqdpAppContext;import cn.com.do1.important.service.IAuthService;import cn.com.do1.wechat.aes.AesException;import cn.com.do1.wechat.aes.WXBizMsgCrypt;import cn.com.do1.wechat.basic.WeChatReqsUTLCfg;import cn.com.do1.wechat.common.ConfKit;import cn.com.do1.wechat.common.RequestUtil;import cn.com.do1.wechat.common.Tools;import cn.com.do1.wechat.core.WeChatCore;import org.apache.struts2.ServletActionContext;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import org.springframework.web.filter.OncePerRequestFilter;import javax.annotation.Resource;import javax.servlet.*;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.net.CookieManager;import java.util.HashMap;import java.util.Map;/*** 请求拦截** @author ao.ouyang*/@Component("autoLoginfilter")public class AutoLoginFilter extends OncePerRequestFilter {private final Logger log = LoggerFactory.getLogger(AutoLoginFilter.class);@Resourceprivate IAuthService authService;@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {try {HttpSession session = request.getSession(true);String username = null;String password = null; // 此sessionid是上次用户登录时保存于用户端的识别码,用于用户后续访问的自动登录。不是本次访问的session id。Cookie[] cookies;// 如果session中没有user对象,则创建一个。Map user = (Map) session.getAttribute("_user");if (user == null) {user = new HashMap(); // 此时user中的username属性为"",表示用户未登录。}// 如果user对象的username为"",表示用户未登录。则执行自动登录过程。// 否则不自动登录。if (ValidateUtil.isNullOrBlank(user.get("USER_NAME"))) {cookies = request.getCookies();for (Cookie cookie : cookies){if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_USERNAME")){username = cookie.getValue();}if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_PASSWORD")){password = cookie.getValue();}}if(!ValidateUtil.isNullOrBlank(username) && !ValidateUtil.isNullOrBlank(password)){if(authService.validUser(username,password).equalsIgnoreCase("successful")){user = authService.viewPersonByUserName(username);authService.updateUserLog(user);ServletActionContext.getRequest().getSession().setAttribute("_user", user);}}}}catch (Exception e){log.error(e.getMessage(),e);}}}
spring filter 配置的更多相关文章
- Spring Boot 2 使用Servlet、Listener和Filter配置
开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 新建一个名称为demo的Spring Boot项目. 一.使用Servlet配置 1.修改启动类 Demo ...
- spring filter过滤器
1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...
- springMVC用法 以及一个简单的基于springMVC hibernate spring的配置
替代struts 1 web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...
- spring filter lister servlet
https://blog.csdn.net/nacey5201/article/details/8547772 https://blog.csdn.net/xwl617756974/article/d ...
- spring boot 之 spring security 配置
Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...
- 最小可用 Spring MVC 配置
[最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...
- spring下配置shiro
1.web.xml中加入shiro的过滤器: <!-- Spring --> <!-- 配置Spring配置文件路径 --> <context-param> < ...
- Tomcat配置和Spring MVC配置
Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
随机推荐
- 自动化运维之Cobbler自动化部署安装操作系统
Cobbler概述: Cobbler可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会. 在生产环境中,经常批 ...
- 运维人员20道必会iptables面试题
1.详述iptales工作流程以及规则过滤顺序? iptables过滤的规则顺序是由上至下,若出现相同的匹配规则则遵循由上至下的顺序 2.iptables有几个表以及每个表有几个链? Iptables ...
- C链表的简单案例
此案例只是简单的使用链表 链表的特点: 1.不需要提前知道要存入数据的长度 2.最后结点为NULL 3.头结点指向下一个结点的结构体指针 #include <stdio.h> #inclu ...
- java.lang.IllegalStateException: No typehandler found for mapping XXX
前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...
- UI--仿IOS控件之ActionSheet样式 and more..
## <代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/d ...
- Linux 4.10中两个新特性与我的一段故事
今早5点半起来没有開始写文章,而是去西湾红树林连跑带走折腾了将近20公里.回来后就8点多了...洗了个澡之后坐稳当.開始写一段关于我的故事. 在2014年到2015年期间,我在负责研发一 ...
- SpringApplication初始化
SpringApplication: private void initialize(Object[] sources) { if (sources != null && source ...
- windows系统如何通过Xshell 客户端连接 linux系统(主要介绍ubuntu系统)
一. 1.查看ubuntu系统的ip地址:ifconfig 在window系统运行窗口下:ping ubuntu系统的IP地址:例如:ping 192.168.163.129 出现下述命令就是ping ...
- php AES cbc模式 pkcs7 128位加密解密(微信小程序)
PHP AES CBC模式PKCS7 128位加密 加密: $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...
- received shard failed for shard id
1.问题描述 早上醒来发现手机有很多ES状态为red的告警,集群就前几天加了几个每天有十多亿记录的业务,当时估算过磁盘容量,应该是没有问题的,但是现在集群状态突然变成red了,这就有点懵逼了. 2.查 ...