spring boot 之登录拦截
登录拦截,请求的session里面有username者判断为登录状态
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter {
@Bean
public SecurityInterceptor getSecurityInterceptor() {
return new SecurityInterceptor();
} public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor()); addInterceptor.excludePathPatterns("/error");
addInterceptor.excludePathPatterns("/plan/login**");
addInterceptor.addPathPatterns("/web/**");
} private class SecurityInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession();
if (session.getAttribute("username") != null)
return true;
String url = "/plan/logins";
response.sendRedirect(url);
return false;
}
}
} 使用redis 进行增强。
获取的session 去判断在redis里面是否存在,存在可登录成功
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter {
@Autowired
private RedisTemplate redisTemplate;
private RedisTemplate userredis(RedisTemplate redisTemplate) {
redisTemplate = RedisDbInit.initRedis(RedisConfig.userreidport, redisTemplate);
return redisTemplate;
}
@Bean
public SecurityInterceptor getSecurityInterceptor() {
return new SecurityInterceptor();
} public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor()); addInterceptor.excludePathPatterns("/error");
addInterceptor.excludePathPatterns("/plan/login**");
addInterceptor.addPathPatterns("/web/**");
} private class SecurityInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String url = "/plan/logins";
HttpSession session = request.getSession();
if (session.getAttribute("username") != null){
String name= (String) userredis(redisTemplate).opsForValue().get(session.getAttribute("username"));
if (name==null){
response.sendRedirect(url);
return false;
}else {
return true;
}
}
response.sendRedirect(url);
return false;
}
}
}
spring boot 之登录拦截的更多相关文章
- 玩转spring boot——简单登录认证
前言 在一个web项目中,某些页面是可以匿名访问的,但有些页面则不能.spring mvc提供了HandlerInterceptor接口来应对,只需要重写preHandle方法便可以实现此功能.那么使 ...
- spring boot 实现mybatis拦截器
spring boot 实现mybatis拦截器 项目是个报表系统,服务端是简单的Java web架构,直接在请求参数里面加了个query id参数,就是mybatis mapper的query id ...
- spring boot中注册拦截器
拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重 ...
- spring boot配置springMVC拦截器
spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...
- Spring Boot 如何使用拦截器、过滤器、监听器?
过滤器 过滤器的英文名称为 Filter, 是 Servlet 技术中最实用的技术. 如同它的名字一样,过滤器是处于客户端和服务器资源文件之间的一道过滤网,帮助我们过滤掉一些不符合要求的请求,通常用作 ...
- Spring Boot实战:拦截器与过滤器
一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...
- spring boot 过滤器、拦截器的区别与使用
原文:https://blog.csdn.net/heweimingming/article/details/79993591 拦截器与过滤器的区别: 1.过滤器和拦截器触发时机不一样,过滤器是在请求 ...
- spring boot 之登录笔记
在测试平台的开发中,会牵涉到登录内容,页面需要登录后才能访问,所以,对于登录的开发是很有必要的.本文记录我在系统登录的一些自己的做法. 首先对登录进行设计. 如下: 1.登录密码输入错误超过次数限制 ...
- Spring Boot下使用拦截器
Spring Boot对于原来在配置文件配置的内容,现在全部体现在一个类中,该类需要继承自WebMvcConfigurationSupport类,并使用@Configuration进行注解,表示该类为 ...
随机推荐
- 【转载】C#使用Math.Ceiling方法对计算结果向上取整操作
在C#的数值运算中,有时候需要对计算结果进行向上取整操作,支持设定结算结果的有效位数,Math.Ceiling方法是C#中专门用来对数值进行向上取整的方法,此方法和Math.Round方法.Math. ...
- npm 安装 react-devtools
由于不能科学的上网.网上看资料装上了这个插件,装的过程有点坑.记录一下,希望能帮到和我一样的新手. 1.第一步,克隆下远程仓库的东西. 桌面右键,git-bash.然后输入: git clone ht ...
- flashdevelop调用ios方法
来源:http://blog.csdn.net/zu12jing/article/details/7331397 flash开发工具用的是flashdevelop(由于flashdevelop还能直接 ...
- c# zip写comment注释
//生成的压缩文件为test.zip using (FileStream fsOut = File.Create("test.zip")) { //ZipOutputStream类 ...
- JDK的安装(mac)
1.第一步安装brew 教学网址 2.用brew安装jdk. brew update brew cask install java(未翻墙时长很长,大概猴年马月两个小时) 安装完成后就可以执行JAVA ...
- 【Python】单例模式
单例 class MusicPlayer(object): instance = None def __new__(cls, *args, **kwargs): if cls.instance is ...
- java线程的生命周期及五种基本状态
一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中的各知识点,Java中的多线程也就基本上掌 ...
- openwrt使用3G拔号的实践笔记
参照文档: https://soha.moe/post/make-4g-wifi-ap-with-openwrt.html 步骤: 1.安装必要的包: opkg update opkg install ...
- HTML&CSS基础-内边框
HTML&CSS基础-内边框 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> &l ...
- props、state、forms
{}用来内嵌任何JS表达式JSX属性JS核心分为三大块:Es6.DOM.WindowBABEL编译器:可以在线编译html语法生成对应的react语法 **自定义组件第一个字母大写:用于区别普通的对象 ...