首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Spring连接到redis: NoClassDefFoundError: NoClassDefFoundError

将Spring连接到redis: NoClassDefFoundError: NoClassDefFoundError
EN

Stack Overflow用户
提问于 2018-04-14 13:35:17
回答 1查看 2.4K关注 0票数 0

因此,我有一个spring引导应用程序,为了会话的目的,我试图连接到AWS上托管的redis集群,完全错误是:

代码语言:javascript
复制
  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里

代码语言:javascript
复制
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里

代码语言:javascript
复制
 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

我的安全配置

代码语言:javascript
复制
@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) {};
            }
        };
    }

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-14 20:11:07

您使用的是Spring和Spring会话的不兼容版本。

Spring 1.5与Session 1.3是一致的,可以在Spring的依赖性版本附录参考手册中看到。OTOH,SpringBoot2.0与Session 2.0是对齐的,可以看到这里

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49832024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档