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. 自动化运维之Cobbler自动化部署安装操作系统

    Cobbler概述: Cobbler可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会. 在生产环境中,经常批 ...

  2. 运维人员20道必会iptables面试题

    1.详述iptales工作流程以及规则过滤顺序? iptables过滤的规则顺序是由上至下,若出现相同的匹配规则则遵循由上至下的顺序 2.iptables有几个表以及每个表有几个链? Iptables ...

  3. C链表的简单案例

    此案例只是简单的使用链表 链表的特点: 1.不需要提前知道要存入数据的长度 2.最后结点为NULL 3.头结点指向下一个结点的结构体指针 #include <stdio.h> #inclu ...

  4. java.lang.IllegalStateException: No typehandler found for mapping XXX

    前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...

  5. UI--仿IOS控件之ActionSheet样式 and more..

    ## <代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/d ...

  6. Linux 4.10中两个新特性与我的一段故事

    今早5点半起来没有開始写文章,而是去西湾红树林连跑带走折腾了将近20公里.回来后就8点多了...洗了个澡之后坐稳当.開始写一段关于我的故事.        在2014年到2015年期间,我在负责研发一 ...

  7. SpringApplication初始化

    SpringApplication: private void initialize(Object[] sources) { if (sources != null && source ...

  8. windows系统如何通过Xshell 客户端连接 linux系统(主要介绍ubuntu系统)

    一. 1.查看ubuntu系统的ip地址:ifconfig 在window系统运行窗口下:ping ubuntu系统的IP地址:例如:ping 192.168.163.129 出现下述命令就是ping ...

  9. php AES cbc模式 pkcs7 128位加密解密(微信小程序)

    PHP AES CBC模式PKCS7 128位加密 加密: $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...

  10. received shard failed for shard id

    1.问题描述 早上醒来发现手机有很多ES状态为red的告警,集群就前几天加了几个每天有十多亿记录的业务,当时估算过磁盘容量,应该是没有问题的,但是现在集群状态突然变成red了,这就有点懵逼了. 2.查 ...