web xml

  1. <filter>
  2.    <filter-name>DelegatingFilterProxy</filter-name>
  3.    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  4.    <init-param>
  5.      <param-name>targetFilterLifecycle</param-name>
  6.      <param-value>true</param-value>
  7.    </init-param>
  8.    <init-param>
  9.      <param-name>targetBeanName</param-name>
  10.      <param-value>autoLoginfilter</param-value>
  11.    </init-param>
  12.  </filter>

配置自动扫描注解

  1. <context:component-scan base-package="cn.com.do1.important.filter" />

filter

  1. package cn.com.do1.important.filter;
  2. import cn.com.do1.component.common.util.ValidateUtil;
  3. import cn.com.do1.dqdp.core.DqdpAppContext;
  4. import cn.com.do1.important.service.IAuthService;
  5. import cn.com.do1.wechat.aes.AesException;
  6. import cn.com.do1.wechat.aes.WXBizMsgCrypt;
  7. import cn.com.do1.wechat.basic.WeChatReqsUTLCfg;
  8. import cn.com.do1.wechat.common.ConfKit;
  9. import cn.com.do1.wechat.common.RequestUtil;
  10. import cn.com.do1.wechat.common.Tools;
  11. import cn.com.do1.wechat.core.WeChatCore;
  12. import org.apache.struts2.ServletActionContext;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.context.WebApplicationContext;
  17. import org.springframework.web.context.support.WebApplicationContextUtils;
  18. import org.springframework.web.filter.OncePerRequestFilter;
  19. import javax.annotation.Resource;
  20. import javax.servlet.*;
  21. import javax.servlet.http.Cookie;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.servlet.http.HttpSession;
  25. import java.io.IOException;
  26. import java.net.CookieManager;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * 请求拦截
  31. *
  32. * @author ao.ouyang
  33. */
  34. @Component("autoLoginfilter")
  35. public class AutoLoginFilter extends OncePerRequestFilter {
  36.    private final Logger log = LoggerFactory.getLogger(AutoLoginFilter.class);
  37.    @Resource
  38.    private IAuthService authService;
  39.    @Override
  40.    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  41.        try {
  42.            HttpSession session = request.getSession(true);
  43.            String username = null;
  44.            String password = null;   // 此sessionid是上次用户登录时保存于用户端的识别码,用于用户后续访问的自动登录。不是本次访问的session id。
  45.            Cookie[] cookies;
  46.            // 如果session中没有user对象,则创建一个。
  47.            Map user = (Map) session.getAttribute("_user");
  48.            if (user == null) {
  49.                user = new HashMap(); // 此时user中的username属性为"",表示用户未登录。
  50.            }
  51.            // 如果user对象的username为"",表示用户未登录。则执行自动登录过程。
  52.            // 否则不自动登录。
  53.            if (ValidateUtil.isNullOrBlank(user.get("USER_NAME"))) {
  54.                cookies = request.getCookies();
  55.                for (Cookie cookie : cookies){
  56.                    if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_USERNAME")){
  57.                        username = cookie.getValue();
  58.                    }
  59.                    if(cookie.getName().equalsIgnoreCase("SESSION_LOGIN_PASSWORD")){
  60.                        password = cookie.getValue();
  61.                    }
  62.                }
  63.                if(!ValidateUtil.isNullOrBlank(username) && !ValidateUtil.isNullOrBlank(password)){
  64.                    if(authService.validUser(username,password).equalsIgnoreCase("successful")){
  65.                        user = authService.viewPersonByUserName(username);
  66.                        authService.updateUserLog(user);
  67.                        ServletActionContext.getRequest().getSession().setAttribute("_user", user);
  68.                    }
  69.                }
  70.            }
  71.        }catch (Exception e){
  72.            log.error(e.getMessage(),e);
  73.        }
  74.    }
  75. }

spring filter 配置的更多相关文章

  1. Spring Boot 2 使用Servlet、Listener和Filter配置

    开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 新建一个名称为demo的Spring Boot项目. 一.使用Servlet配置 1.修改启动类 Demo ...

  2. spring filter过滤器

    1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...

  3. springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

    替代struts 1  web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...

  4. spring filter lister servlet

    https://blog.csdn.net/nacey5201/article/details/8547772 https://blog.csdn.net/xwl617756974/article/d ...

  5. spring boot 之 spring security 配置

    Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...

  6. 最小可用 Spring MVC 配置

    [最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...

  7. spring下配置shiro

    1.web.xml中加入shiro的过滤器: <!-- Spring --> <!-- 配置Spring配置文件路径 --> <context-param> < ...

  8. Tomcat配置和Spring MVC配置

    Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...

  9. Spring Boot配置过滤器的两种方式

    过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...

随机推荐

  1. Nginx配置Awstats分析Nginx日志笔记

    1.修改Nginx日志格式: log_format json '$remote_addr - $remote_user [$time_local] "$request" '     ...

  2. 【基础】centos 6.X 下修改图形界面为命令行界面(单用户救援模式)

    1. Linux开机引导的时候,按键盘上的e 就可以进入进入GRUB菜单界面. 2.在出现GRUB引导画面时(CentOS(2.6.18-274**)),按字母e键,进入GRUB编辑状态: 3.把光标 ...

  3. 【Visual Studio】Visual C# 中XML注释的使用(含注释在开发时显示换行)

    为函数方法注释说明要用到 xml 语句 <summary> 段落说明 </summary> .<para> 新段示例说明 </para>.<par ...

  4. C#文件夹权限操作工具类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...

  5. 【C语言】字符串常量与指针

  6. MySQL的timestamp类型自动更新问题

    今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...

  7. python学习笔记——爬虫学习中的重要库urllib

    1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...

  8. '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 ...

  9. java udp 广播

    原文链接: http://blog.csdn.net/yudajun/article/details/8477149 udp 是一种网络通信协议,不需要客户端和服务器端建立连接即可进行通讯功能.相对于 ...

  10. Linux中iptables防火墙指定端口范围

    我需要700至800之间的端口都能tcp访问 代码如下 复制代码 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 7 ...