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配置过 ...
随机推荐
- 蛋疼的经历--wireshark不能启动的问题
事情是这样子的,最近新入职,安装了wireshark,,,在急需要其观察数据包结构,,,,写代码时,,,,卡了,,,我的天!!! 刚开始是提示说,找不到动态链接库api-ms-win-crt-runt ...
- 我收藏的技术知识图(每张都是大图)关于XX背后的知识、技术图,例如:Linux、Nginx架构、PHP知识卡、机会、HTML5移动、Android系统架构、YII架构的典型流程、Css知识表
我收藏的技术知识图(每张都是大图) HTML5Linux/Unix系统设计思想读书笔记 LinuxMVCJava线程MVCSpring MVCCSS3Nginx架构VimCliCommandsPHP知 ...
- [企业化NET]Window Server 2008 R2[2]-SVN 服务端 和 客户端 安装
1. 服务器基本安装即问题解决记录 √ 2. SVN环境搭建和客户端使用 2.1 服务端 和 客户端 安装 √ 2.2 项目建立与基本使用 √ 2.3 基本冲突解决, ...
- WSAAsyncSelect模型触发不了FD_Close消息?
原帖链接: http://bbs.csdn.net/topics/330164174 楼主: Server用WSAAsyncSelect模型,Client关闭程序时(不进行shutdown与close ...
- 两张图看清SharePoint 2013 Farm 逻辑体系结构
前篇文章分析了SharePoint 2013 的物理拓扑结构.物理拓扑分为3层(2层),详情参见<SharePoint 2013 Farm (多层服务器)安装指南——Least Privileg ...
- CentOS 7安装Redis4.0.10
cd /usr/local/src && wget http://download.redis.io/releases/redis-4.0.10.tar.gz && t ...
- nodejs 遍历文件夹下所有的图片改名为中文
安装依赖 $ npm init -y && npm i fs-extra globby request -S main.js const fs = require('node-fs-e ...
- CSS中易忽略的点
一.使用斜杠/分割的关键字 1.font H2{ font:12px/100% sans-serif; } 分割的分别是 字体大小 与 行高 ,其他关键是用空白符分割. 2.background di ...
- 码字工作者的发文姿势—— 用MWeb+Markdown Here+七牛 轻松实现多平台发布
码字工作者的发文姿势—— 用MWeb+Markdown Here+七牛 轻松实现多平台发布 1.对于写作你最头疼什么 对于大多数码字工作者来说,随时随地记录灵感,构思文章,集中书写,其实是一件令人 ...
- centos 6.3 64位下cpuminer +mining_proxy 挖掘莱特币(LTC)教程
1.下载软件: cpuminer: http://sourceforge.net/projects/cpuminer/files/ 找到对应的版本,我的服务器是centos64的,找了个当前最高版本: ...