目录
问题内容
解决方法
首页 Java spring-security过滤器是否从redis中获取会话信息?

spring-security过滤器是否从redis中获取会话信息?

Feb 09, 2024 pm 10:30 PM
spring security

php小编子墨在这里解答关于Spring Security过滤器是否从Redis中获取会话信息的问题。Spring Security是一个强大的安全框架,它提供了一套完整的身份验证和授权机制。在默认情况下,Spring Security使用HttpSession来管理用户的会话信息。然而,通过配置,我们可以将会话信息存储在Redis等外部存储中。这样做的好处是可以实现分布式会话管理,提高系统的可伸缩性。因此,Spring Security过滤器是可以从Redis中获取会话信息的。

问题内容

我正在尝试使用 spring-boot、spring-security 和 spring-session 实现登录系统,并使用 redis 作为会话的存储。

我的配置:

@enablewebsecurity
@enablemethodsecurity
@configuration
@requiredargsconstructor
public class securityconfig {


    private final userdetailsservice detailsservice;

    @bean
    public passwordencoder passwordencoder() {
        return new bcryptpasswordencoder();
    }

    @bean
    public authenticationprovider authenticationprovider(passwordencoder passwordencoder) {
        daoauthenticationprovider provider = new daoauthenticationprovider();
        provider.setpasswordencoder(passwordencoder);
        provider.setuserdetailsservice(this.detailsservice);
        return provider;
    }

    @bean
    public authenticationmanager authenticationmanager(authenticationprovider authenticationprovider) {
        return new providermanager(authenticationprovider);
    }

    @bean
    public securityfilterchain filterchain(httpsecurity http) throws exception {
        return http
                .csrf().disable()
                .cors(customizer.withdefaults())
                .authorizehttprequests(auth -> {
                    auth.requestmatchers("/api/v1/auth/register/**", "/api/v1/auth/login").permitall();
                    auth.anyrequest().authenticated();
                })
                .sessionmanagement(sessionmanagement -> sessionmanagement
                        .sessioncreationpolicy(if_required) //
                        .sessionfixation(sessionmanagementconfigurer.sessionfixationconfigurer::newsession) //
                        .maximumsessions(100000) //
                        //.sessionregistry(sessionregistry())
                )
                //.exceptionhandling((ex) -> ex.authenticationentrypoint(this.authentrypoint))
                .logout(out -> out
                        .logouturl("/api/v1/auth/logout")
                        .invalidatehttpsession(true) // invalidate all sessions after logout
                        .deletecookies("jsessionid")
                        .logoutsuccesshandler((request, response, authentication) ->
                                securitycontextholder.clearcontext()
                        )
                )
                .build();
    }

    @bean
    public securitycontextrepository securitycontextrepository() {
        return new httpsessionsecuritycontextrepository();
    }
}
登录后复制

我的登录控制器:

@postmapping("/login")
public void login(@requestbody loginform form, httpservletrequest request, httpservletresponse response) {
    string ip = httprequestutil.getip(request);
    string device = httprequestutil.getdevice(request);

    loginformwrapper loginformwrapper = new loginformwrapper(form.email(), form.password(), ip, device);

    authenticationservice.login(loginformwrapper, request, response);
}
登录后复制

和身份验证服务:

@override
public void login(loginformwrapper form, httpservletrequest request, httpservletresponse response) {
    authentication authentication = authenticationmanager.authenticate(usernamepasswordauthenticationtoken.unauthenticated(
            form.email().trim(), form.password()));

    // create a new context
    securitycontext context = securitycontextholder.createemptycontext();
    context.setauthentication(authentication);

    // update securitycontextholder and strategy
    this.securitycontextholderstrategy.setcontext(context);
    this.securitycontextrepository.savecontext(context, request, response);
}
登录后复制

如果我理解正确的话

this.securitycontextholderstrategy.setcontext(context); 应该将身份验证保存在应用程序的内存中,例如在 threadlocal 上下文中

`this.securitycontextrepository.savecontext(context, request, response);`
登录后复制

应该将会话信息保存到redis中。

现在,当我登录时,我看到数据已保存到 redis 中:

但是,当检查我的登录请求返回的内容时,我看到:

完全不同的会话 id。

我的第一个问题是:为什么这些 id 不匹配? spring 如何知道要寻找哪个键?

另一个问题是:什么过滤器从redis中获取数据?我尝试调试过滤器链中的所有过滤器:

[org.springframework.security.web.session.DisableEncodeUrlFilter@2fedae96,
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@4945cd1f,
org.springframework.security.web.context.SecurityContextHolderFilter@72499396,
org.springframework.security.web.header.HeaderWriterFilter@7048d039,
org.springframework.web.filter.CorsFilter@2dbfcbe4,
org.springframework.security.web.authentication.logout.LogoutFilter@5d5a77de,
org.springframework.security.web.session.ConcurrentSessionFilter@1f8e1096,
org.springframework.security.web.savedrequest.RequestCacheAwareFilter@651bec9a,
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@76d4e1af,
org.springframework.security.web.authentication.AnonymousAuthenticationFilter@6f13ed1,
org.springframework.security.web.session.SessionManagementFilter@642693c2,
org.springframework.security.web.access.ExceptionTranslationFilter@2199e1a4,
org.springframework.security.web.access.intercept.AuthorizationFilter@48c584c]
登录后复制

但它似乎以某种方式从 httpservletrequest 读取会话信息 - 但是,如果我从 redis 中删除密钥,则需要它的端点的身份验证失败。

我错过了什么吗?是否在我的 fitler 开始之前检索来自 redis 的会话信息并将其存储在 httpservlerrequest 中?或者说它是如何读取redis数据的?

感谢您的帮助。

解决方法

会话cookie中的值是base64编码的:

echo '3c048eae-9f73-4df5-a009-bdf802ae37ca' | openssl base64
m2mwndhlywutowy3my00zgy1lwewmdktymrmodayywuzn2nhcg==
登录后复制
echo 'M2MwNDhlYWUtOWY3My00ZGY1LWEwMDktYmRmODAyYWUzN2NhCg==' | openssl base64 -d
3c048eae-9f73-4df5-a009-bdf802ae37ca
登录后复制

因此,当进行 base64 解码时,cookie 的会话 id 与存储在 redis 中的会话 id 相匹配。

如果您还没有阅读过它,我会推荐此文档:https://www.php.cn/link/e27c71957d1e6c223e0d48a165da2ee1

特别是“了解会话管理的组件”部分:https://www.php.cn/link/e27c71957d1e6c223e0d48a165da2ee1#understanding-session-management-components

您没有提到您正在使用哪个版本的 spring security,但我猜测您正在使用 spring security 6。在该部分中,有这样一句话与 sessionauthentication 相关:

以上是spring-security过滤器是否从redis中获取会话信息?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Spring Security 6:cors() 已弃用并标记为删除 Spring Security 6:cors() 已弃用并标记为删除 Feb 10, 2024 pm 11:45 PM

我有下面的代码:publicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{returnhttp.httpBasic().disable().cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("

如何使用Java开发一个基于Spring Security SAML的单点登录系统 如何使用Java开发一个基于Spring Security SAML的单点登录系统 Sep 22, 2023 am 08:49 AM

如何使用Java开发一个基于SpringSecuritySAML的单点登录系统引言:随着互联网的快速发展,越来越多的应用程序被开发出来。在这些应用程序中,用户登录是最常见的功能之一。然而,对于企业级应用程序,用户需要在多个系统中进行登录,这将导致用户的登录体验非常糟糕。为了解决这个问题,单点登录系统(SingleSign-On,简称SSO)应运而生。简

GO 验证访问令牌(keycloak) GO 验证访问令牌(keycloak) Feb 09, 2024 am 09:30 AM

我正在尝试使用GO实现访问令牌验证。但我在网上看到的例子似乎只是用TOKEN_SECRET来验证它。但是我习惯了在Javaspring中编程,并且不需要使用TOKEN_SECRET。我只是提供jwk-set-uri,它会检查有效性(自动-安全过滤器等),我知道它与oauth服务器通信并进行此验证。Go中是否没有库可以通过向oauth服务器发出请求来检查令牌是否有效?我知道我知道我可以通过向oauth服务器的用户信息端点发出请求来手动进行此操作:http://localh

Spring Security权限控制框架使用指南 Spring Security权限控制框架使用指南 Feb 18, 2024 pm 05:00 PM

在后台管理系统中,通常需要访问权限控制,以限制不同用户对接口的访问能力。如果用户缺乏特定权限,则无法访问某些接口。本文将用waynboot-mall项目举例,给大家介绍常见后管系统如何引入权限控制框架SpringSecurity。大纲如下:waynboot-mall项目地址:https://github.com/wayn111/waynboot-mall一、什么是SpringSecuritySpringSecurity是一个基于Spring框架的开源项目,旨在为Java应用程序提供强大和灵活的安

如何使用Java开发一个基于Spring Security OAuth2的单点登录系统 如何使用Java开发一个基于Spring Security OAuth2的单点登录系统 Sep 20, 2023 pm 01:06 PM

如何使用Java开发一个基于SpringSecurityOAuth2的单点登录系统引言:随着互联网的高速发展,越来越多的网站和应用程序需要用户进行登录,而用户却不希望为每个网站或应用程序都记住一个账号和密码。单点登录系统(SingleSign-On,简称SSO)能够解决这个问题,允许用户在一次登录后,无需重复认证就可以访问多个网站和应用程序。本文将介绍

Spring Security 在其余服务中获取经过身份验证和未经过身份验证的用户的用户信息 Spring Security 在其余服务中获取经过身份验证和未经过身份验证的用户的用户信息 Feb 08, 2024 pm 11:00 PM

我有一个springrest服务,我想将它用于经过身份验证和未经身份验证的用户。如果用户经过身份验证,我想从securitycontextholder.getcontext().getauthentication()获取用户信息。如果我使用.antmatchers("/app/rest/question/useroperation/list/**").permitall()在ouath2配置中,如下所示,然后我可以获取用户信息经过身份验证的用户,但未经过身份验证的用户会出现40

Java RESTful API 烹饪书:为每个应用程序打造完美的服务 Java RESTful API 烹饪书:为每个应用程序打造完美的服务 Mar 27, 2024 pm 12:11 PM

引言在当今互联互通的世界中,RESTfulapi已成为应用程序之间进行通信的关键机制。借助Java这一强大的编程语言,您可以构建高效、可扩展且维护良好的RESTfulAPI。第1章:RESTfulAPI基础RESTful架构的原则和最佳实践Http方法、状态代码和响应标头JSON和XML等数据格式第2章:设计和建模RESTfulAPIRESTfulAPI设计原则资源建模和URI设计版本控制和HATEOAS第3章:使用SpringBoot构建RESTfulAPISpringBoot简介和入门构建和

vue框架和springboot框架前后端怎么交互 vue框架和springboot框架前后端怎么交互 Apr 06, 2024 am 01:51 AM

Vue.js 和 Spring Boot 通过以下方式交互:RESTful API:Vue.js 使用 Axios 发送异步 HTTP 请求,Spring Boot 提供 RESTful API 实现。数据传递:数据通过请求和响应传递,如请求正文或查询参数。请求方法:GET、POST、PUT 和 DELETE 等 HTTP 请求方法用于指定操作。路由:Spring Boot @RequestMapping 注解定义控制器路由,Vue.js 使用 Vue Router 定义界面路由。状态管理:Vu