项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821

定义一个拦截器,判断用户是通过记住我登录时,查询数据库后台自动登录,同时把用户放入session中。

配置拦截器也很简单,Spring 为此提供了基础类WebMvcConfigurerAdapter ,我们只需要重写addInterceptors 方法添加注册拦截器。

实现自定义拦截器只需要3步:

1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口。

2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。

3、实例化我们自定义的拦截器,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。

package com.sun.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties; /**
* Created by sun on 2017-3-21.
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { /**
* 此方法把该拦截器实例化成一个bean,否则在拦截器里无法注入其它bean
* @return
*/
@Bean
SessionInterceptor sessionInterceptor() {
return new SessionInterceptor();
}
/**
* 配置拦截器
* @param registry
*/
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sessionInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/login","/permission/userInsert",
"/error","/tUser/insert","/gif/getGifCode");
} }
package com.sun.configuration;

import com.sun.permission.model.User;
import com.sun.permission.service.PermissionService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Created by sun on 2017-4-9.
*/
public class SessionInterceptor implements HandlerInterceptor{
private final Logger logger = Logger.getLogger(SessionInterceptor.class);
@Resource
private PermissionService permissionService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
logger.info("---preHandle---");
System.out.println(request.getContextPath());
Subject currentUser = SecurityUtils.getSubject();
//判断用户是通过记住我功能自动登录,此时session失效
if(!currentUser.isAuthenticated() && currentUser.isRemembered()){
try {
User user = permissionService.findByUserEmail(currentUser.getPrincipals().toString());
//对密码进行加密后验证
UsernamePasswordToken token = new UsernamePasswordToken(user.getEmail(), user.getPswd(),currentUser.isRemembered());
//把当前用户放入session
currentUser.login(token);
Session session = currentUser.getSession();
session.setAttribute("currentUser",user);
//设置会话的过期时间--ms,默认是30分钟,设置负数表示永不过期
session.setTimeout(-1000l);
}catch (Exception e){
//自动登录失败,跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login");
return false;
}
if(!currentUser.isAuthenticated()){
//自动登录失败,跳转到登录页面
response.sendRedirect(request.getContextPath()+"/login");
return false;
}
}
return true;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
logger.info("---postHandle---");
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
logger.info("---afterCompletion---");
}
}

  

SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)的更多相关文章

  1. [转][原]openstack-kilo--issue(六)kilo版openstack的dashboard在session超时后重新登录报错解决办法

    http://blog.csdn.net/wylfengyujiancheng/article/details/50523373?locationNum=1&fps=1 1.现象描述: kil ...

  2. Jenkins权限配置失误后导致登录失败的解决办法

    为了便于管理,Jenkins一般需要设置用户,而且这些用户是需要配置相应的权限的,如果一不小心配置的时候出了问题,那么,你就斯巴达了. 这里,用我的切身经历,为大家说一下Jenkins因为权限配置失误 ...

  3. swiper手滑动轮播图后自动轮播失效解决办法

    设置autoplay:true之后,再设置 autoplay:{disableOnInteraction: false} --------------------------------------- ...

  4. 基于localStorge开发登录模块的记住密码与自动登录

    前沿||我是乐于分享,善于交流的鸟窝 先做写一篇关于登录模块中记住密码与自动登录的模块.鸟窝微信:jkxx123321 关于这个模块功能模块的由来,这是鸟大大的处女秀,为什么这么说呢?一天在群里,一个 ...

  5. spring security实现记住我下次自动登录功能

    目录 spring security实现记住我下次自动登录功能 一.原理分析 二.实现方式 2.1 简单实现方式 2.2 数据库实现方式 三.区分是密码登录还是rememberme登录 spring ...

  6. /etc/profile 路径出错后相关的命令失效解决方式

    关于 Linux 的配置文件 /etc/profile 路径出错后相关的命令失效解决方式(如:ls,vi不能用) 今天学习LINUX 下配置jdk 和安装tomcat 通过VI编辑/etc/profi ...

  7. php中实现记住密码下次自动登录的例子

    这篇文章主要介绍了php中实现记住密码下次自动登录的例子,本文使用cookie实现记住密码和自动登录功能,需要的朋友可以参考下 做网站的时候经常会碰到要实现记住密码,下次自动登录,一周内免登陆,一个月 ...

  8. 数据加密实战之记住密码、自动登录和加密保存数据运用DES和MD5混合使用

    MD5的简介:MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有 ...

  9. WPF发布程序后未授予信任的解决办法

    WPF发布程序后未授予信任的解决办法 基于浏览器的WPF应用程序由于需要比较高的操作权限,所以在项目的安全性属性中选择了“这是完全可信的应用程序”选项.可是,在发布部署后,在其他电脑上打开xbap文件 ...

随机推荐

  1. interaction-oriented architecture - MVC

    MVC(Model-View-Controller),它是专门针 对交互系统提出的,所以如果我们要构建一个交互系统,那么我们就可以直接应用MVC模式,然后 在该模式所搭建的场景的启发下去发现Model ...

  2. 在CentOS实现mysql数据库的自动备份

    数据是一个比较重要的数据,经常需要备份,每次都手动比较麻烦.本脚本主要现实在CentOS中实现对数据库的备份和保留最近十五天的备份文件.避免太多无用陈旧的备份占用空间. #!/bin/bashid=& ...

  3. halcon 分水领域法详解(转载)

    寻思自己写了的,但是这个大佬写的太好了,感谢Y忍冬草_ http://blog.csdn.net/y363703390 https://blog.csdn.net/y363703390/article ...

  4. 【AngularJS学习笔记】开发时候的建议目录结构,基本开发步骤

    项目目录结构划分 Debug Node.JS的生成目录 Node_modules Node.Js的依赖项 Src 项目源文件 ----|framework  项目框架 --------|app --- ...

  5. Vue教程:过滤器filters(五)

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaScrip ...

  6. 架构风格:万金油CS与分层

    计算机科学家David Wheele曾说过这么一句话: All problems in computer science can be solved by another level of indir ...

  7. 系统优化怎么做-Tomcat优化

    大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...

  8. anyconnect connection attempt has failed

    anyconnect connection attempt has failed 在控制面板-网络与Internet-网络连接,右键AnyConnect secure连接适配器,点击属性 在连接项目中 ...

  9. vue的$emit 与$on父子组件与兄弟组件的之间通信

    本文主要对vue 用$emit 与 $on 来进行组件之间的数据传输. 主要的传输方式有三种: 1.父组件到子组件通信 2.子组件到父组件的通信 3.兄弟组件之间的通信 一.父组件传值给子组件 父组件 ...

  10. mysql 8.0.12安装步骤

    首先从官网下载压缩包: 解压压缩包到指定目录,在目录下新建my.ini,配置内容如下; [mysqld]  # 设置3306端口  port=3306  # 设置mysql的安装目录  basedir ...