基于SpringBoot实现用户身份验证工具
作者:玩具熊猫 发布时间:2022-05-08 18:37:29
session失效时间
在Tomcat上,session的默认有效时间是30分钟。也可以通过配置文件修改session的有效时间。
1)修改web.xml
<!-- 设置session失效,单位分 -->
<session-config>
<session-timeout>1</session-timeout>
</session-config>2).yml文件
server.session.cookie.http-only= #是否开启HttpOnly
server.session.timeout = #会话超时(秒)使用过滤器获取session进行身份验证(未全部测试,慎用)
1)新建Filter
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@ServletComponentScan//让@WebFilter起作用
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter{
@Autowired
private SessionKeyConfigProperties sessionKeyConfigProperties;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
System.out.println(sessionKeyConfigProperties.getUserTypeKey());
//通过session获取身份信息
AuthenticationUtil authenticationUtil = new AuthenticationUtil(sessionKeyConfigProperties);
UserTypeEnum userType = authenticationUtil.getUserAuthentication(httpServletRequest.getSession());
//进行认证
//认证失败
if(userType == null){
//...
}
//用户不是管理员
if(userType != UserTypeEnum.ADMIN){
//...
}
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
细心的读者会发现我用了AuthenticationUtil,这是为了将读写用户身份认证信息的功能分离而设计的工具类 2)AuthenticationUtil类
import org.apache.shiro.web.session.HttpServletSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class AuthenticationUtil {
private SessionKeyConfigProperties configProperties;
public AuthenticationUtil(SessionKeyConfigProperties configProperties) {
this.configProperties = configProperties;
}
/**
* 从session中获取用户的身份类型
* @param session
* @return 身份类型
*/
public UserTypeEnum getUserAuthentication(HttpSession session){
//获取session中的用户信息记录
Object userType = session.getAttribute(configProperties.getUserTypeKey());
//获取session中记录的用户类型
if(userType != null && userType instanceof UserTypeEnum) {
return (UserTypeEnum)userType;
}
return null;
}
/**
* 将用户的身份写入session中
* @param session
* @param userType
*/
public void setUserAuthentication(HttpSession session,UserTypeEnum userType){
session.setAttribute(configProperties.getUserTypeKey(),userType);
}
}
3)配置文件SessiionKeyConfig.properties
user_type_key = userTypeKey
4)配置读取文件SessionKeyConfigProperties.class
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@PropertySource("classpath:config/SessiionKeyConfig.properties")
@Component
public class SessionKeyConfigProperties {
@Value("${user_type_key}")
private String userTypeKey;
public String getUserTypeKey() {
return userTypeKey;
}
public void setUserTypeKey(String userTypeKey) {
this.userTypeKey = userTypeKey;
}
}
5)Enum类
public enum UserTypeEnum {
ADMIN,
USER
}
注:本文删除了一些package信息及部分import信息。Enum类和配置类的内容请根据项目需求及数据字典自行修改。
总结
以上所述是小编给大家介绍的基于SpringBoot实现用户身份验证工具网站的支持!
来源:https://blog.csdn.net/qq_34964570/article/details/79163562


猜你喜欢
- 应用场景:在Android开发过程中,有时需要调用手机自身设备的功能,上篇文章主要侧重摄像头拍照功能的调用。本篇文章将综合实现拍照与视频的操
- 本文实例讲述了Android开发实现判断通知栏是否打开及前往设置页面的方法。分享给大家供大家参考,具体如下:项目中用到日程提醒功能,如果应用
- 在servlet3.0标准之前,是每一个请求对应一个线程。如果此时一个线程出现了高延迟,就会产生阻塞问题,从而导致整个服务出现严重的性能情况
- 在构建RESTful数据服务过程中,我们定义了controller、repositories,并用一些注解修饰它们,但是到现在为止我们还没执
- 本文实例为大家分享了Android实现全局悬浮框的具体代码,供大家参考,具体内容如下效果图:代码实现:Androidmanifest.xml
- 一、常见的状态保存恢复方式①onSaveInstance + onRestoreInstance这种方式是最通用的实现状态保存与恢复,在An
- 在开发中我们经常需要把我们的应用设置为全屏,有两种方法,一中是在代码中设置,另一种方法是在配置文件里改!一、在代码中设置:package c
- 使用开源框架是,可以直接复制源代码到自己的项目(本人在Android Studio中操作报R程序包不存在),也可以使用jar包,下面记录一下
- 相信很多人都了解c#语言,但是对于c#语言编写应用程序的经验不够多,所以经常为没有实例练习而烦恼吧。今天小编给大家介绍下C#里的多线程技术。
- 实践过程效果代码public partial class Form1 : Form {
- 这是一个及其常见的问题,网上已经有关于这个问题的很多讨论。但是我觉得都是不求甚解,有一些还是在误导别人。下面我来说下我对这三者的理解,如有错
- 本文实例讲述了Android开发实现模仿微信小窗口功能。分享给大家供大家参考,具体如下:运用方法:将显示窗口的风格 设置为对话框风格即可具体
- 概述Handler是Android消息机制的上层接口。通过它可以轻松地将一个任务切换到Handler所在的线程中去执行。通常情况下,Hand
- 文章目录 简介增量构建自定义inputs和outputs运行时API隐式依赖输入校验自定义缓存方法输入归一化其他使用技巧简介在我们使用的各种
- 前言Hello!上一期我大致讲解了关于Collection单列集合以及它的子接口List集合的概述、特点和遍历等,今天我为大家讲解关于Col
- 本文实例讲述了C#非矩形窗体实现方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Colle
- 开篇Druid号称是Java语言中最好的数据库连接池,并且能够提供强大的监控和扩展功能。作为日常使用较多的数据库连接组件,纯粹个人兴趣研究下
- 这次是列表滑动删除的第三波,仿微信的列表滑动删除。先上个效果图: 前面的文章里面说过开源框架SwipeListView
- Android 自定义布局竖向的ViewPager的实现效果图:这个自定义控件涉及到的知识点:自定义ViewGroup中onMeasure和
- 静态变量初始化顺序1.简单规则首先先看一段最普遍的JAVA代码:public class Test{ public static Test1