Spring Reactive Security + общий сеанс + Redis - PullRequest
0 голосов
/ 23 октября 2019

Я пытаюсь использовать Spring Security Reactive (WebFlux) с Shared Session (Redis), но похоже, что реактивная версия ReactiveRedisOperationsSessionRepository не ведет себя как нереактивная RedisOperationsSessionRepository.

Использование webfluxПриложение Я вижу только один ключ после входа в систему

127.0.0.1:6380> keys '*'
1) "spring:session:sessions:f4751f43-43c2-4975-8bc6-77d97e5d4028"

Использование нереактивного есть список ключей.

127.0.0.1:6380> keys '*'
1) "spring:session:expirations:1571783280000"
2) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:admin@gmail.com"
3) "spring:session:sessions:expires:7616e01b-8def-467f-994b-8abe06e2f1e1"
4) "spring:session:sessions:7616e01b-8def-467f-994b-8abe06e2f1e1"

Мне было интересно, есть ли способ добавить эти ключи в реактивверсия.

Следующий фрагмент кода для лучшего понимания.

pom.xml

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

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

Классы конфигурации

@Configuration
@EnableRedisWebSession
public class RedisSessionConfiguration implements BeanClassLoaderAware {
    private ClassLoader loader;

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
    this.loader = classLoader;
    }

    @Bean
    RedisSerializer<Object> springSessionDefaultRedisSerializer() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
    return new GenericJackson2JsonRedisSerializer(mapper);
    }
}


@Configuration
@AllArgsConstructor
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SpringSecurityConfiguration {

    @Bean
    public SecurityWebFilterChain configure(ServerHttpSecurity http) {
    return http
        .authorizeExchange()
            .pathMatchers(WHITELIST).permitAll()
        .anyExchange().authenticated()
        .and()
        .formLogin()
            .loginPage("/api/authenticate")
            .authenticationFailureHandler((webFilterExchange, exception) -> handleResponseError.handle(webFilterExchange.getExchange(), exception, true))
            .authenticationSuccessHandler(getServerAuthenticationSuccessHandler())
            .securityContextRepository(webSessionServerSecurityContextRepository())
            .authenticationManager(authenticationManager())
            .and().logout().logoutUrl("/api/logout")
            .and().httpBasic().disable()
        .addFilterAt(authenticationWebFilter(), SecurityWebFiltersOrder.AUTHORIZATION)
        .exceptionHandling()
            .authenticationEntryPoint((exchange, e) -> handleResponseError.handle(exchange, e, true))
        .and()
        .requestCache()
            .requestCache(NoOpServerRequestCache.getInstance())
        .and()
        .csrf()
            .disable()
        .headers()
            .frameOptions().disable()
        .cache().disable()
        .and()
        .build();
    }

    @Bean
    public WebSessionServerSecurityContextRepository webSessionServerSecurityContextRepository() {
    return new WebSessionServerSecurityContextRepository();
    }

    ...

}
...