Spring Boot中关于Spring Session和Redis的依赖错误。我需要使用哪个正确的依赖?

huangapple go评论146阅读模式
英文:

Dependency error in Spring boot for Spring session and Redis. What is the correct dependency i have to use?

问题

  1. 我有一个Angular 2前端和Spring Boot后端我想使用用户名和密码进行登录然后使用 `x-auth-token` 来检查每个从Angular发送的请求的会话我想使用Redis来存储会话但是当连接到Redis我一直在收到以下错误我的假设是`spring session`的依赖版本导致了问题但我无法理解为什么会这样
  2. 错误信息
  3. "尝试调用不存在的方法。尝试发生在以下位置:
  4. org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.getNotifyOptions(ConfigureNotifyKeyspaceEventsAction.java:74)
  5. 以下方法不存在:
  6. org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List;
  7. 方法的类,org.springframework.data.redis.connection.RedisConnection,可以从以下位置获得:
  8. jar:file:/C:/Users/Ajay/.m2/repository/org/springframework/data/spring-data-redis/2.3.3.RELEASE/spring-data-redis-2.3.3.RELEASE.jar!/org/springframework/data/redis/connection/RedisConnection.class
  9. 类层次结构从以下位置加载:
  10. org.springframework.data.redis.connection.RedisConnection: file:/C:/Users/Ajay/.m2/repository/org/springframework/data/spring-data-redis/2.3.3.RELEASE/spring-data-redis-2.3.3.RELEASE.jar
  11. 操作:
  12. 纠正应用程序的类路径,以便它包含单个兼容版本的 org.springframework.data.redis.connection.RedisConnection"
  13. Spring配置
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  16. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  17. @EnableRedisHttpSession
  18. public class HttpSessionConfig {
  19. @Bean
  20. public LettuceConnectionFactory connectionFactory() {
  21. return new LettuceConnectionFactory();
  22. }
  23. }
  24. @Configuration
  25. @EnableWebSecurity
  26. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  27. // ...
  28. }
  29. pom.xml依赖部分已略

请注意,我只返回了代码的翻译部分,您提供的内容中还包含了pom.xml的内容,但由于篇幅较长,我将其省略了。如果您需要翻译pom.xml的内容,请提供需要翻译的具体部分。

英文:

I have angular 2 front-end and spring boot back-end.I want to use username and password for login and then use x-auth-token to check session for each request sent from angular.I want to use Redis to store session.But i keep getting the below error when connecting to Redis.My assumption is my dependency version of spring session is causing the issue but i am not able to understand why that is? -

An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.getNotifyOptions(ConfigureNotifyKeyspaceEventsAction.java:74)
The following method did not exist:
org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List;
The method's class, org.springframework.data.redis.connection.RedisConnection, is available from the following locations:
jar:file:/C:/Users/Ajay/.m2/repository/org/springframework/data/spring-data-redis/2.3.3.RELEASE/spring-data-redis-2.3.3.RELEASE.jar!/org/springframework/data/redis/connection/RedisConnection.class
The class hierarchy was loaded from the following locations:
org.springframework.data.redis.connection.RedisConnection: file:/C:/Users/Ajay/.m2/repository/org/springframework/data/spring-data-redis/2.3.3.RELEASE/spring-data-redis-2.3.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.redis.connection.RedisConnection

Spring -

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  3. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  4. @EnableRedisHttpSession public class HttpSessionConfig {
  5. @Bean public LettuceConnectionFactory connectionFactory() { return new
  6. LettuceConnectionFactory(); }
  7. }
  8. @Configuration
  9. @EnableWebSecurity
  10. public class SecurityConfig extends WebSecurityConfigurerAdapter{
  11. @Autowired
  12. Environment env;
  13. @Autowired
  14. UserSecurityService useSecurityService;
  15. private BCryptPasswordEncoder passwordEncoder() {
  16. return SecurityUtility.passwordEncoder();
  17. }
  18. private static final String[] PUBLIC_MATHCES= {
  19. "/css/**",
  20. "/js/**",
  21. "/image/**",
  22. "/book/**",
  23. "/user/**"
  24. };
  25. @Override
  26. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  27. auth.userDetailsService(useSecurityService).passwordEncoder(passwordEncoder());
  28. }
  29. @Override
  30. protected void configure(HttpSecurity http) throws Exception {
  31. http.authorizeRequests()
  32. .antMatchers(PUBLIC_MATHCES).permitAll()
  33. .anyRequest().authenticated();
  34. http.csrf().disable()
  35. .cors()
  36. .and()
  37. .httpBasic();
  38. }
  39. @Bean public HttpSessionStrategy httpSessionStrategy() {
  40. return new HeaderHttpSessionStrategy();
  41. }
  42. }

pom.xml-

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.3.3.RELEASE</version>
  10. <relativePath /> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.bookstore</groupId>
  13. <artifactId>bookstore</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>BookStore</name>
  16. <description>BookStore backend</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-data-jpa</artifactId>
  29. </dependency>
  30. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-jdbc</artifactId>
  34. </dependency>
  35. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-security</artifactId>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/MySQL/mysql-connector-java -->
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. </dependency>
  45. <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session -->
  46. <dependency>
  47. <groupId>org.springframework.session</groupId>
  48. <artifactId>spring-session</artifactId>
  49. <version>1.3.5.RELEASE</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-starter-data-redis</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework.session</groupId>
  57. <artifactId>spring-session-data-redis</artifactId>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-starter-test</artifactId>
  62. <scope>test</scope>
  63. <exclusions>
  64. <exclusion>
  65. <groupId>org.junit.vintage</groupId>
  66. <artifactId>junit-vintage-engine</artifactId>
  67. </exclusion>
  68. </exclusions>
  69. </dependency>
  70. </dependencies>
  71. <build>
  72. <plugins>
  73. <plugin>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-maven-plugin</artifactId>
  76. </plugin>
  77. </plugins>
  78. </build>
  79. </project>

答案1

得分: 2

我认为 pom.xml 文件有些混乱。你的依赖项存在冲突。

使用以下命令查看加载了哪些依赖项,以及哪些被省略了的详细信息。

mvn dependency:tree -Dverbose -Dincludes=commons-collections

这将为你提供一些见解。

链接:https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

另外,我认为你可以尝试移除以下依赖项并运行应用程序。

  1. <dependency>
  2. <groupId>org.springframework.session</groupId>
  3. <artifactId>spring-session</artifactId>
  4. <version>1.3.5.RELEASE</version>
  5. </dependency>
英文:

I think pom.xml is bit messed up. You have conflicting dependencies.

Use below command to see details of which dependencies are loaded n which are omitted.

mvn dependency:tree -Dverbose -Dincludes=commons-collections

That will give you some insights.

https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

Also in my opinion can you try removing below dependency and run the application.

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-session&lt;/artifactId&gt;
  4. &lt;version&gt;1.3.5.RELEASE&lt;/version&gt;
  5. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月11日 03:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64297675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定