您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)SpringBoot2.x如何設(shè)置Session失效時間及失效跳轉(zhuǎn)的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
#Session超時時間設(shè)置,單位是秒,默認(rèn)是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 這個類是什么,我不推薦你接著看下去了,因為沒有什么幫助。
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; @Configuration public 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默認(rèn)的提交地址 .successHandler(coreqiAuthenticationSuccessHandler) //登錄成功以后要用我們自定義的登錄成功處理器,不用Spring默認(rèn)的。 .failureHandler(coreqiAuthenticationFailureHandler) //自己體會把 .and() .sessionManagement() .invalidSessionUrl("session/invalid") //session過期后跳轉(zhuǎn)的URL .and() .authorizeRequests() //對授權(quán)請求進行配置 .antMatchers("/coreqi-signIn.html","/code/image","/session/invalid").permitAll() //指定登錄頁面不需要身份認(rèn)證 .anyRequest().authenticated() //任何請求都需要身份認(rèn)證 .and().csrf().disable() //禁用CSRF .apply(smsCodeAuthenticationSecurityConfig); //FilterSecurityInterceptor 整個SpringSecurity過濾器鏈的最后一環(huán) } }
@GetMapping("/session/invalid") @ResponseStatus(code = HttpStatus.UNAUTHORIZED) public SimpleResponse sessionInvalid(){ String message = "session失效"; return new SimpleResponse(message); }
這里可以在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; @SpringBootApplication public class DemoApplication {undefined public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } //設(shè)置session過期時間 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { public void customize(ConfigurableEmbeddedServletContainer container) { container.setSessionTimeout(7200);// 單位為S } }; } }
application.yml
server: port: 8081 servlet: session: timeout: 60s
@RestController public 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; } }
感謝各位的閱讀!關(guān)于“SpringBoot2.x如何設(shè)置Session失效時間及失效跳轉(zhuǎn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。