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进行注解,表示该类为 ...
随机推荐
- JavaScript之获取标签
(1)html <p id="txt">这是P标签</p> <h1 id="txt" style="color: red ...
- Apache Commons FileUpload实现文件上传
一.Apache Commons-FileUpload简介 Apache Commons是一个专注于可重用Java组件的所有方面的 Apache 项目. Apache Commons项目由三个部分组成 ...
- MySQL Network--域名与VIP
VIP与域名1.域名能在多个IDC切换,而VIP通常在特定网段内切换.2.VIP切换可以立即生效,而域名切换存在一定时间延迟. DNS解析顺序:1.查询本地域名映射配置(/etc/hosts)2.查查 ...
- MySQL Binlog--基于ROW模式的binlog event大小限制
参数binlog-row-event-max-size:Specify the maximum size of a row-based binary log event, in bytes. Rows ...
- mac 下enable mysql的load data in file
1)使用root用户登录mysql 2)将 local_infile 变量设置为true SET GLOBAL local_infile = true; 3)重启数据库 在系统偏好设置中找到MySql ...
- ET·ci — 全自动软件测试调度(持续集成)平台
ET·ci 提供了编译-测试-发布解决方案,包括:自动提取配置库代码进行自动构建, 自动调度静态测试工具(如QAC)进行静态测试,自动调度单元测试工具(如Tessy)开展动态测试,自动 ...
- Luogu P1627 中位数
Luogu P1627 中位数 先记录目标数的位置,并且把数组映射为: $$a[i]=\begin{cases}-1,a[i]<b\0,a[i]=b\1,a[i]>b\end{cases} ...
- 使用 create-react-app 快速构建 React 开发环境
在终端执行以下命令创建项目: 1.指定创建的项目位置(这里以桌面为例) cd Desktop 2.创建 React 项目 npx create-react-app my-app 3.进入项目并启动 c ...
- 李航-统计学习方法-笔记-3:KNN
KNN算法 基本模型:给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的k个实例.这k个实例的多数属于某个类,就把输入实例分为这个类. KNN没有显式的学习过程. KNN使用的模型 ...
- 前端知识总结--2 js部分
1. JavaScript的『预解释』与『变量提升』 先看以下代码输出啥? var a= 1; function f() { console.log(a); var a = 2; } f(); 首先答 ...