【问题标题】:Connecting Spring boot to redis: NoClassDefFoundError: org/springframework/session/hazelcast/HazelcastFlushMode将 Spring boot 连接到 redis:NoClassDefFoundError: org/springframework/session/hazelcast/HazelcastFlushMode
【发布时间】:2018-09-24 16:48:50
【问题描述】:

所以我有一个 Spring Boot 应用程序,我正在尝试连接到托管在 AWS 上的 redis 集群以用于会话 完整的错误是:

  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.session-org.springframework.boot.autoconfigure.session.SessionProperties': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.session.SessionProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/session/hazelcast/HazelcastFlushMode

在我的 build.gradle 中

buildscript {
ext {
    springBootVersion = '1.5.9.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
   }

  apply plugin: 'java'
  apply plugin: 'eclipse'
  apply plugin: 'org.springframework.boot'

   group = 'haughton.daniel'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = 1.8

  repositories {
mavenCentral()
  }  


   ext {
springCloudVersion = 'Edgware.SR1'
     }
  processResources {
from ('.ebextensions/') {
    into '.ebextensions'
}
    }


   dependencies {

compile('org.springframework.boot:spring-boot-starter-data-jpa')

compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.0.RELEASE'

compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')

compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '2.1.2.RELEASE'

compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

compile group: 'org.springframework.session', name: 'spring-session-data-redis', version: '2.0.2.RELEASE'


compile('org.springframework.boot:spring-boot-starter-web')
compile ('org.apache.tomcat:tomcat-dbcp:8.0.30')

runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
 }

 dependencyManagement {
imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
  }

在我的 app.properties 中

 spring.session.store-type=redis
 spring.redis.host=my aws redis end point
 spring.redis.password=my password
 spring.redis.port=6379

我正在按照这里的指南https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot-redis.html#boot-how

我的安全配置

@EnableWebSecurity
@Configuration

public class SecurityConfiguration {
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http
                .antMatcher("/api/**")
                .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .and()
                .httpBasic();
    }
    @Bean
    public EvaluationContextExtension securityExtension() {
        return new EvaluationContextExtensionSupport() {
            @Override
            public String getExtensionId() {
                return "security";
            }

            @Override
            public Object getRootObject() {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                return new SecurityExpressionRoot(authentication) {};
            }
        };
    }
}

@Configuration
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }



    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/web/login",
                "/web/forgotPassword",
                "/web/forgotPassword/useToken","/web/forgotPassword/**",
                "/web/forgotPassword/useToken/**").permitAll().antMatchers("/web/**").

                authenticated().
                and()
                .formLogin()
                .loginPage("/web/login")
                .permitAll()
                .successHandler(loginSuccessHandler())
                .failureUrl("/web/login-error")
                .and()
                .logout()
                .logoutUrl("/web/logout")
                .logoutSuccessUrl("/web/login")
                .and().
                csrf().disable();;

    }

    public AuthenticationSuccessHandler loginSuccessHandler() {
        return (request, response, authentication) -> response.sendRedirect("/web");
    }


    @Bean
    public EvaluationContextExtension securityExtension() {
        return new EvaluationContextExtensionSupport() {
            @Override
            public String getExtensionId() {
                return "security";
            }

            @Override
            public Object getRootObject() {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                return new SecurityExpressionRoot(authentication) {};
            }
        };
    }

}
}

【问题讨论】:

  • 您使用的是哪个版本的 Spring Session?从您的代码 sn-ps 中看不出来。
  • 我只是在使用 spring-data-redis',版本:'2.0.6.RELEASE',就像教程所说的那样
  • 您链接的教程根本没有引用spring-data-redis,而是引用了Spring Session的spring-session-data-redis
  • 我的错误,我在摆弄东西并放入 spring-data-redis,我最初有 spring-session-data-redis,我刚刚确认我的代码是最新的 spring-session -data-redis 并编辑了我的帖子以使我的 build.gradle 准确,我得到的错误是帖子中最初的错误

标签: amazon-web-services spring-boot redis spring-session


【解决方案1】:

您正在使用不兼容的 Spring Boot 和 Spring Session 版本。

Spring Boot 1.5 与 Spring Session 1.3 保持一致,如 Spring Boot 参考手册的dependency versions appendix 所示。 OTOH,Spring Boot 2.0 与 Spring Session 2.0 保持一致,可以看到 here

您应该避免在构建脚本中使用显式的依赖版本声明,并依赖 Spring Boot 提供的依赖管理,除非有充分的理由不这样做。

【讨论】:

  • ahhh 所以如果我这样做 // mvnrepository.com/artifact/org.springframework.session/… compile group: 'org.springframework.session', name: 'spring-session-data-redis' 它会自动选择正确的?
  • 是的,您可以像处理其他一些依赖项一样简单地使用compile 'org.springframework.session:spring-session-data-redis'
猜你喜欢
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2021-06-27
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2015-10-14
相关资源
最近更新 更多