将spring.session.store-type设置为'redis'是否不再提供FindByIndexNameSessionRepository bean?

enyaitl3  于 2023-03-28  发布在  Spring
关注(0)|答案(1)|浏览(243)

前段时间我写了一个spring security的小demo,我使用redis来存储用户会话。我一直在尝试将其升级到Sping Boot 3和Spring Security 6,但在提升项目版本后如下:
Java:11 -〉17
Spring Boot :2.7.5 -〉3.0.4
我无法再自动连接org.springframework.session.FindByIndexNameSessionRepository
这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-security-jpa-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-security-jpa-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>6.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.5.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.5.3.Final</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这是我在www.example.com上的Redis配置application.properties:

...

spring.session.store-type=redis
spring.session.redis.namespace=spring-security-jpa-demo
spring.data.redis.host=localhost
spring.data.redis.password=redis
spring.data.redis.port=6379

...

我在docker compose中有redis和redis commander服务:

version: '3.7'

volumes:
  spring-security-jpa-demo-redis-data:
    name: spring-security-jpa-demo-redis-data
    driver: local

networks:
  spring-security-jpa-demo:
    name: spring-security-jpa-demo
    driver: bridge

services:

  redis:
    image: redis:5-alpine
    volumes:
      - spring-security-jpa-demo-redis-data:/data
    command: redis-server --requirepass redis
    networks:
      - spring-security-jpa-demo
    restart: always
    ports:
      - "6379:6379"

  redis-commander:
    image: rediscommander/redis-commander:latest
    environment:
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: redis
    networks:
      - spring-security-jpa-demo
    ports:
      - "8081:8081"
    depends_on:
      - redis

...

这是我如何使用FindByIndexNameSessionRepository:

package com.example.springsecurityjpademo.session;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class RedisSessionHandler {

    private final FindByIndexNameSessionRepository<? extends Session> sessionRepository;

    public void removeAllUserSessions(String principalName) {
        log.debug("Removing all sessions for user: {}", principalName);
        sessionRepository.findByPrincipalName(principalName)
                .forEach((id, session) -> sessionRepository.deleteById(session.getId()));
    }

    public void removeUserSession(String principalName, String sessionId) {
        var sessionOptional = sessionRepository.findByPrincipalName(principalName)
                .values().stream().filter(session -> sessionId.equals(session.getId())).findFirst();

        sessionOptional.ifPresent(session -> sessionRepository.deleteById(session.getId()));
    }

}

由于版本升级,应用程序无法启动,并出现以下错误:

Description:

Parameter 0 of constructor in com.example.springsecurityjpademo.session.RedisSessionHandler required a bean of type 'org.springframework.session.FindByIndexNameSessionRepository' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.session.FindByIndexNameSessionRepository' in your configuration.

我尝试从www.example.com中删除store-type属性application.properties并使用@EnableRedisHttpSession,但没有成功。现在应该手动配置此bean还是我的配置中缺少了一些东西?
请注意,我没有在docker容器中运行应用程序,我试图首先在本地运行它,这就是为什么spring.data.redis.host被设置为localhost。
先谢了!

e7arh2l6

e7arh2l61#

我有同样的问题,但我解决了它。你使用@EnableRedisIndexedHttpSession而不是@EnableRedisHttpSession。
请参见以下内容。https://github.com/spring-projects/spring-session/issues/2235#issuecomment-1434657799

相关问题