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配置过 ...
随机推荐
- Nginx配置Awstats分析Nginx日志笔记
1.修改Nginx日志格式: log_format json '$remote_addr - $remote_user [$time_local] "$request" ' ...
- 【基础】centos 6.X 下修改图形界面为命令行界面(单用户救援模式)
1. Linux开机引导的时候,按键盘上的e 就可以进入进入GRUB菜单界面. 2.在出现GRUB引导画面时(CentOS(2.6.18-274**)),按字母e键,进入GRUB编辑状态: 3.把光标 ...
- 【Visual Studio】Visual C# 中XML注释的使用(含注释在开发时显示换行)
为函数方法注释说明要用到 xml 语句 <summary> 段落说明 </summary> .<para> 新段示例说明 </para>.<par ...
- C#文件夹权限操作工具类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...
- 【C语言】字符串常量与指针
- MySQL的timestamp类型自动更新问题
今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...
- python学习笔记——爬虫学习中的重要库urllib
1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...
- 'Agent XPs' component is turned off as part of the security configuration for this server
To turn on Agent XP's by running this script: sp_configure 'show advanced options', 1; Go RECONFIGUR ...
- java udp 广播
原文链接: http://blog.csdn.net/yudajun/article/details/8477149 udp 是一种网络通信协议,不需要客户端和服务器端建立连接即可进行通讯功能.相对于 ...
- Linux中iptables防火墙指定端口范围
我需要700至800之间的端口都能tcp访问 代码如下 复制代码 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 7 ...