基于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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分别在Groovy和Java中创建并初始化映射的不同分析
Android按钮单击事件的四种常用写法总结
Springboot2.x+ShardingSphere实现分库分表的示例代码
![](https://img.aspxhome.com/file/2023/1/59591_0s.jpg)
详解Java的TCP/IP编程学习--基于定界符的成帧
简单了解4种分布式session解决方案
![](https://img.aspxhome.com/file/2023/7/57717_0s.png)
java使用静态关键字实现单例模式
![](https://img.aspxhome.com/file/2023/3/60763_0s.png)
不规范使用ThreadLocal导致bug分析解决
![](https://img.aspxhome.com/file/2023/2/59182_0s.png)
Java中IO流文件读取、写入和复制的实例
IDEA部署JavaWeb项目到Tomcat服务器的方法
Java面试题冲刺第五天--基础篇2
![](https://img.aspxhome.com/file/2023/7/58617_0s.png)
详解java WebSocket的实现以及Spring WebSocket
![](https://img.aspxhome.com/file/2023/5/59885_0s.png)
ArrayList详解和使用示例_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/0/57840_0s.png)
Java实现批量向mysql写入数据的方法
浅谈java中Math.random()与java.util.random()的区别
Java Thread之Sleep()使用方法及总结
![](https://img.aspxhome.com/file/2023/4/59014_0s.png)
IDEA不编译除了.java之外的文件的解决办法(推荐)
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
关于@ApiImplicitParams、ApiImplicitParam的使用说明
![](https://img.aspxhome.com/file/2023/0/59220_0s.jpg)
sms4j 2.0 全新来袭功能的调整及maven变化详解
Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发环境
![](https://img.aspxhome.com/file/2023/4/60314_0s.png)