SpringBoot2.x如何设置Session失效时间及失效跳转
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章给大家分享的是有关SpringBoot2.x如何设置Session失效时间及失效跳转的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。设置Session失效时间及失效跳
千家信息网最后更新 2025年02月02日SpringBoot2.x如何设置Session失效时间及失效跳转
这篇文章给大家分享的是有关SpringBoot2.x如何设置Session失效时间及失效跳转的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
设置Session失效时间及失效跳转
#Session超时时间设置,单位是秒,默认是30分钟 server.servlet.session.timeout=10
然而并没有什么用,因为SpringBoot在TomcatServletWebServerFactory代码中写了这个
private long getSessionTimeoutInMinutes() { Duration sessionTimeout = this.getSession().getTimeout(); return this.isZeroOrLess(sessionTimeout) ? 0L : Math.max(sessionTimeout.toMinutes(), 1L); }
如果说某些人看不懂 Duration 这个类是什么,我不推荐你接着看下去了,因为没有什么帮助。
Session失效后如何跳转到Session失效地址
package cn.coreqi.security.config; import cn.coreqi.security.Filter.SmsCodeFilter;import cn.coreqi.security.Filter.ValidateCodeFilter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.password.NoOpPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.security.web.authentication.AuthenticationSuccessHandler;import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;@Configurationpublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private AuthenticationSuccessHandler coreqiAuthenticationSuccessHandler; @Autowired private AuthenticationFailureHandler coreqiAuthenticationFailureHandler; @Autowired private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig; @Bean public PasswordEncoder passwordEncoder(){ return NoOpPasswordEncoder.getInstance(); } @Override protected void configure(HttpSecurity http) throws Exception { ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter(); validateCodeFilter.setAuthenticationFailureHandler(coreqiAuthenticationFailureHandler); SmsCodeFilter smsCodeFilter = new SmsCodeFilter(); //http.httpBasic() //httpBasic登录 BasicAuthenticationFilter http.addFilterBefore(smsCodeFilter, UsernamePasswordAuthenticationFilter.class) //加载用户名密码过滤器的前面 .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) //加载用户名密码过滤器的前面 .formLogin() //表单登录 UsernamePasswordAuthenticationFilter .loginPage("/coreqi-signIn.html") //指定登录页面 //.loginPage("/authentication/require") .loginProcessingUrl("/authentication/form") //指定表单提交的地址用于替换UsernamePasswordAuthenticationFilter默认的提交地址 .successHandler(coreqiAuthenticationSuccessHandler) //登录成功以后要用我们自定义的登录成功处理器,不用Spring默认的。 .failureHandler(coreqiAuthenticationFailureHandler) //自己体会把 .and() .sessionManagement() .invalidSessionUrl("session/invalid") //session过期后跳转的URL .and() .authorizeRequests() //对授权请求进行配置 .antMatchers("/coreqi-signIn.html","/code/image","/session/invalid").permitAll() //指定登录页面不需要身份认证 .anyRequest().authenticated() //任何请求都需要身份认证 .and().csrf().disable() //禁用CSRF .apply(smsCodeAuthenticationSecurityConfig); //FilterSecurityInterceptor 整个SpringSecurity过滤器链的最后一环 }}
@GetMapping("/session/invalid") @ResponseStatus(code = HttpStatus.UNAUTHORIZED) public SimpleResponse sessionInvalid(){ String message = "session失效"; return new SimpleResponse(message); }
设置Session失效的几种方式
如果是1.5.6版本
这里可以在application中加上bean文件
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class DemoApplication {undefinedpublic static void main(String[] args) { SpringApplication.run(DemoApplication.class, args);}//设置session过期时间@Beanpublic EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { public void customize(ConfigurableEmbeddedServletContainer container) { container.setSessionTimeout(7200);// 单位为S } };}}
还可以设置
application.yml
server:port: 8081servlet:session:timeout: 60s
@RestControllerpublic class HelloController {undefined@PostMapping("test")public Integer getTest(@RequestParam("nyy")String nn, HttpServletRequest httpServletRequest ){ HttpSession session = httpServletRequest.getSession(); session.setMaxInactiveInterval(60); int maxInactiveInterval = session.getMaxInactiveInterval(); long lastAccessedTime = session.getLastAccessedTime(); return maxInactiveInterval;}}
感谢各位的阅读!关于"SpringBoot2.x如何设置Session失效时间及失效跳转"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
登录
时间
地址
过滤器
成功
内容
单位
密码
更多
用户
用户名
篇文章
表单
身份
身份认证
页面
帮助
认证
不错
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京it 软件开发品牌企业
天容信好的网络安全公司
PHP软件开发任务书
耐特安网络技术有限公司
中国移动公司软件开发待遇
网络安全文明上网承诺书
mysql数据库索引是什么
锐捷路由器服务器名称
软件开发方案设计软件
华为视频会议网络安全
淮安云主机服务器租用
大华视频服务器的作用有哪些
潮州软件开发工程师
北京美洽网络技术有限公司
幼儿园网络安全简单画
嘉定区软件开发技术服务案例
服务器如何让外网访问
网上正面宣传维护网络安全
服务器怎么虚拟机多开
暨南大学网络安全知乎
数据库文件删除了有影响吗
网络安全泄露处罚
通过页面访问数据库
医保医用耗材代码数据库动态维护
腾讯云推出首款自研服务器星星海
叶子解说我的世界服务器生存
煎饼网络技术有限公司
大工 计算机网络技术
济南蓝翼网络技术有限公司
c语言软件开发大概要多少钱