Session management

本文深入探讨了HTTP Session的管理,包括SessionID的使用、通过Cookie和URL重写传递SessionID,以及Session的结束和生命周期。重点阐述了setMaxInactiveInterval()和invalidate()方法,以及与之相关的监听器和事件处理。
摘要由CSDN通过智能技术生成

一个HttpSession对应同一个客户端发送的多个Request。

一、SessionID

服务器通过SessionID来识别request是否来自同一个客户端。当第一次收到客户端发来的Requst时,Container会生成一个SessionID并随response发回客户端,当客户端发送随后的请求时将SessionID一同发送到服务器,Container将收到的SessionID与现存Session的SessionID进行匹配。

1、Cookies 

Cookies是一种简单的发送SessionID的方法。当Container通过Cookies发送ID时,response header中会有Set-Cookie:JSESSIONID=1231231项,客户发回的request header中也会有Cookie:JSESSIONID=1231231项。

使用HttpRequest.getSession()会自动在response中加入Cookie:JSESSIONID,使用HttpSession.isNew()可以判断得到的Session是否是新的,当该Session中无匹配ID时视为新的,返回true。当想要获得旧的Session时,可以用HttpRequest.getSession(false),如果没有旧的Session则返回null。

ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests(); permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll()); httpSecurity // CSRF禁用,因为不使用session .csrf().disable() // 禁用HTTP响应标头 .headers().cacheControl().disable().and() // 认证失败处理类 .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() // 基于token,所以不需要session .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() // 过滤请求 .authorizeRequests() // 对于登录login 注册register 验证码captchaImage 允许匿名访问 .antMatchers("/login", "/register", "/captchaImage","/system/workbenchinfo/**").permitAll() // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**","/system/workbenchinfo/**").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() .and() .headers().frameOptions().disable(); // 添加Logout filter httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); // 添加JWT filter httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); // 添加CORS filter httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class); httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);代码解析
06-06
这段代码是一个 Java Spring Security 配置类的代码,主要作用是配置 Spring Security 的一些安全认证策略。以下是代码的解析: 1. `ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();` 这行代码定义了一个 `HttpSecurity` 实例的 `authorizeRequests()` 方法返回的对象,并将其赋值给 `registry` 变量。 2. `permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());` 这行代码遍历了一个 `permitAllUrl` 对象中的所有 URL,并使用 `antMatchers(url)` 方法将这些 URL 添加到 `registry` 对象中,并使用 `permitAll()` 方法允许这些 URL 的匿名访问。 3. `.csrf().disable()` 这行代码禁用了 CSRF 保护,因为这个应用程序不使用 session。 4. `.headers().cacheControl().disable().and()` 这行代码禁用了响应头中的 cacheControl。 5. `.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()` 这行代码定义了一个异常处理类 `authenticationEntryPoint`,在认证失败时将会被调用。 6. `.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()` 这行代码定义了一个 `SessionCreationPolicy.STATELESS` 的 `sessionManagement()`,因为这个应用程序是基于 token 的,所以不需要使用 session。 7. `.authorizeRequests()` 这行代码开始定义了所有请求的鉴权认证策略。 8. `.antMatchers("/login", "/register", "/captchaImage","/system/workbenchinfo/**").permitAll()` 这行代码定义了几个 URL 的匿名访问策略。 9. `.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()` 这行代码定义了一些静态资源的匿名访问策略,包括 HTML、CSS、JavaScript 文件以及 `/profile/` 目录下的所有资源。 10. `.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**","/system/workbenchinfo/**").permitAll()` 这行代码定义了一些 Swagger 相关的 URL 的匿名访问策略。 11. `.anyRequest().authenticated()` 这行代码定义了除了上面所列出的 URL 之外的所有请求都需要进行鉴权认证。 12. `.headers().frameOptions().disable()` 这行代码禁用了 X-Frame-Options。 13. `httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);` 这行代码添加了一个 Logout filter,并定义了 `/logout` URL 的登出成功处理器。 14. `httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);` 这行代码添加了一个 JWT filter,该 filter 会在 `UsernamePasswordAuthenticationFilter` 前执行。 15. `httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);` 这行代码添加了一个 CORS filter,该 filter 会在 `JwtAuthenticationTokenFilter` 前执行。 16. `httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);` 这行代码添加了一个 CORS filter,该 filter 会在 `LogoutFilter` 前执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值