redis 的 spring-boot 安全共享会话

     2023-02-27     111

关键词:

【中文标题】redis 的 spring-boot 安全共享会话【英文标题】:spring-boot security share session by redis 【发布时间】:2018-10-21 08:17:09 【问题描述】:

我有一个 spring-boot 安全演示,我想与 redis 共享会话。我在 pom.xml 中添加了一些代码

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

还有一个新的 redis 配置类

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig 
    @Bean
    public RedisTemplate<Object, Object> sessionRedisTemplate(
            RedisConnectionFactory connectionFactory) 
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(connectionFactory);
        return template;
    

将参数添加到“application.properties”文件中。

spring.session.store-type=redis
# redis database
spring.redis.database=8
# redis host
spring.redis.host=102.128.2.65
# redis password
#spring.redis.password=
#redis port
spring.redis.port=6379

程序可以像以前一样启动。我可以在redis中找到一些spring消息。 enter image description here

登录成功时出错。我发现spring security在登录成功时将SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper与会话密钥'SPRING_SECURITY_CONTEXT'放入会话。 为什么? HttpSessionWrapper类是spring的内部类,为什么spring放到redis中没有序列化? 我不知道如何解决这个问题。请帮助我,非常感谢。

错误日志:

20:25:17.629 [http-nio-8099-exec-6] ERROR o.s.boot.web.support.ErrorPageFilter - Forwarding to error page from request [/login] due to exception [Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper]
    org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper
        at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:93)
        at org.springframework.data.redis.core.AbstractOperations.rawHashValue(AbstractOperations.java:171)
        at org.springframework.data.redis.core.DefaultHashOperations.putAll(DefaultHashOperations.java:129)
        at org.springframework.data.redis.core.DefaultBoundHashOperations.putAll(DefaultBoundHashOperations.java:86)
        at org.springframework.session.data.redis.RedisOperationsSessionRepository$RedisSession.saveDelta(RedisOperationsSessionRepository.java:770)
        at org.springframework.session.data.redis.RedisOperationsSessionRepository$RedisSession.access$000(RedisOperationsSessionRepository.java:662)
        at org.springframework.session.data.redis.RedisOperationsSessionRepository.save(RedisOperationsSessionRepository.java:388)
        at org.springframework.session.data.redis.RedisOperationsSessionRepository.save(RedisOperationsSessionRepository.java:245)
        at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.commitSession(SessionRepositoryFilter.java:245)
        at org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper.access$100(SessionRepositoryFilter.java:217)
        at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:170)
        at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:117)
        at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
        at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:92)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:110)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

其实我是下载spring源代码,尝试通过编辑源代码修复它。但是当spring没有问题但出现另一个apache类时我放弃了。所以...我的配置文件错了吗?还是春季安全分享会议不喜欢我的方式?

【问题讨论】:

【参考方案1】:
@Bean
public ConfigureRedisAction configureRedisAction() 
    return ConfigureRedisAction.NO_OP;

【讨论】:

尝试在你的配置中添加这个 bean。我有这个基本示例,您可以查看github.com/Ravenloup/springboot-redis-session

redis异步客户端选型及落地实践

...的Java实现客户端,提供了比较全面的Redis命令的支持,在spring-boot1.x默认使用Jedis。但是Jedis使用阻塞的IO,且其方法调用都是同步的,程序流需要等到sockets处理完IO才能执行,不支持异步,在并发场景下,使用Jedis客户端会耗费... 查看详情

redis分布式锁篇

...:在分布式系统中,多个线程访问共享数据就会出现数据安全性的问题。而由于jdk中的锁要求多个线程在同一个jvm中,因此在分布式系统中无法使用jdk中的锁保证数据的安全性,那么此时就需要使用分布式锁。作用:可以保证在... 查看详情

如何在spring-boot中禁用静态内容句柄?

】如何在spring-boot中禁用静态内容句柄?【英文标题】:Howtodisablestaticcontenthandleinspring-boot?【发布时间】:2015-02-1406:29:04【问题描述】:我正在使用this链接来配置安全性和spring-boot作为其他的基础。但是spring-boot提供的静态资源... 查看详情

spring-boot redis:如何使用户的所有会话无效?

】spring-bootredis:如何使用户的所有会话无效?【英文标题】:spring-bootredis:Howtoinvalidateallsessionsofauser?【发布时间】:2017-08-2218:34:31【问题描述】:我是redis的新手。我已按照本教程将HttpSession与redis一起使用。https://docs.spring.io/spr... 查看详情

使用 Spring-boot 进行安全配置

】使用Spring-boot进行安全配置【英文标题】:SecurityconfigurationwithSpring-boot【发布时间】:2014-10-2707:32:21【问题描述】:我为Spring-Boot创建了一个SpringSecurity配置类。我的登录页面有资源css、js和ico文件。出于安全原因,资源被拒绝... 查看详情

spring-boot 弹簧安全 angularjs

】spring-boot弹簧安全angularjs【英文标题】:spring-bootspringsecurityangularjs【发布时间】:2018-04-2811:34:42【问题描述】:我是春天世界的新手。我需要创建一个带有一些CRUD操作的springboot-angularjs应用程序。客户端需要LDAP和本地JDBC身份... 查看详情

spring-boot使用jedis操作redis

背景:  1.Redis之前学了个皮毛还忘的差不多了,感觉公司项目中的Redis用的真的牛逼,so需要深造。  2.有个同事在搞Jedis,勾起了我对知识的向往,不会用,但是很渴望。过程:  1.改造原有项目集成Jedis,引入jar包<depe... 查看详情

共享异常实例是不是安全

】共享异常实例是不是安全【英文标题】:Isitsafetoshareexceptioninstance共享异常实例是否安全【发布时间】:2012-07-1315:12:06【问题描述】:我们正在制作一个类似Excel的系统。当我们打开一个文档并发现不受支持的功能时,我们会... 查看详情

spring-boot-2.0.3之redis缓存实现,不是你想的那样哦

前言  开心一刻    小白问小明:“你前面有一个5米深的坑,里面没有水,如果你跳进去后该怎样出来了?”小明:“躺着出来呗,还能怎么出来?”小白:“为什么躺着出来?”小明:“5米深的坑,还没有水,跳下去... 查看详情

带有spring-boot安全休息api的角度2

】带有spring-boot安全休息api的角度2【英文标题】:angular2withspring-bootsecurityrestapi【发布时间】:2017-08-1916:20:15【问题描述】:我正在开发一个web应用程序,它的前端是用Angular2(typescript)编写的,它是从angularcli和SpringBoot1.5.2RELEASE生... 查看详情

spring-boot之security安全管理-10

  一、SpringSecurity简介  1、SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了SpringIoC,DI(控制反转InversionofContr... 查看详情

如何在 Spring-Boot 2 中禁用安全性? [复制]

】如何在Spring-Boot2中禁用安全性?[复制]【英文标题】:HowtodisablesecurityinSpring-Boot2?[duplicate]【发布时间】:2019-01-0306:44:00【问题描述】:在spring-boot-1.x中,我有以下配置来禁用开发模式下的基本安全性:application.properties:security.b... 查看详情

Spring-boot 安全性不会尊重具有自定义 AuthenticationProvider 的角色 [重复]

】Spring-boot安全性不会尊重具有自定义AuthenticationProvider的角色[重复]【英文标题】:Spring-bootsecuritywon\'trespectroleswithcustomAuthenticationProvider[duplicate]【发布时间】:2018-03-0120:09:39【问题描述】:我正在创建自己的自定义身份验证提... 查看详情

Spring-boot REST 安全配置角色无法正常工作

】Spring-bootREST安全配置角色无法正常工作【英文标题】:Spring-bootRESTsecurityconfigrolesdon\'tworkright【发布时间】:2019-05-3110:55:50【问题描述】:只有具有管理员角色的用户应该能够在“users/all”发出请求,但基本用户也可以。这是... 查看详情

redis分布式锁详细分析

...作用,我想大家都理解,就是让不同的线程或者进程可以安全地操作共享资源,而不会产生冲突。比较熟悉的就是Synchronized和ReentrantLock等,这些可以保证同一个jvm程序中,不同线程安全操作共享资源。但是在分布式系统中,这... 查看详情

最佳实践——在应用之间共享 Spring-boot Service 和 Repo 层代码

】最佳实践——在应用之间共享Spring-bootService和Repo层代码【英文标题】:Bestpractice-shareSpring-bootServiceandRepolayercodebetweenapplications【发布时间】:2022-01-0814:50:20【问题描述】:需要一些关于基于层模块化Springboot应用程序的经典需... 查看详情

springboot整合redis

...为了lettuce?Jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedispool连接池!更像BIO模式Lettuce:采用netty,实例可以在多个线程中进行共享,不存在线程不安全的情况 查看详情

多线程安全问题

------------恢复内容开始------------一、线程中安全问题产生的原因。1、多个线程在操作共享数据。2、操作共享数据的线程代码有多条。当一个线程在执行操作共享数据的多条代码过程中,其他线程参与了运算,就会导致线程安全... 查看详情