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

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

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

问题

我有一个Angular 2前端和Spring Boot后端我想使用用户名和密码进行登录然后使用 `x-auth-token` 来检查每个从Angular发送的请求的会话我想使用Redis来存储会话但是当连接到Redis时我一直在收到以下错误我的假设是`spring session`的依赖版本导致了问题但我无法理解为什么会这样

错误信息

"尝试调用不存在的方法。尝试发生在以下位置:
org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.getNotifyOptions(ConfigureNotifyKeyspaceEventsAction.java:74)
以下方法不存在:
org.springframework.data.redis.connection.RedisConnection.getConfig(Ljava/lang/String;)Ljava/util/List;
方法的类,org.springframework.data.redis.connection.RedisConnection,可以从以下位置获得:
 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
类层次结构从以下位置加载:
    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
操作:
纠正应用程序的类路径,以便它包含单个兼容版本的 org.springframework.data.redis.connection.RedisConnection"

Spring配置

import org.springframework.context.annotation.Bean; 
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession
public class HttpSessionConfig {
    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }
}

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // ...
}

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 -

  import org.springframework.context.annotation.Bean; 
  import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  
  @EnableRedisHttpSession public class HttpSessionConfig {
  @Bean public LettuceConnectionFactory connectionFactory() { return new
  LettuceConnectionFactory(); }
  
  }




@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
	
	@Autowired
	Environment env; 

	@Autowired
	UserSecurityService useSecurityService;
	
	private BCryptPasswordEncoder passwordEncoder() {
		return SecurityUtility.passwordEncoder();
	}
	
	private static final String[] PUBLIC_MATHCES= {
			"/css/**",
			"/js/**",
			"/image/**",
			"/book/**",
			"/user/**"
	};

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(useSecurityService).passwordEncoder(passwordEncoder());
		
		
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
			.antMatchers(PUBLIC_MATHCES).permitAll()
			.anyRequest().authenticated();
		http.csrf().disable()
		    .cors()
		    .and()
			.httpBasic();
		
	}
	
	
	  @Bean public HttpSessionStrategy httpSessionStrategy() { 
		  return new HeaderHttpSessionStrategy(); 
	  }
		
}

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>2.3.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.bookstore</groupId>
	<artifactId>bookstore</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>BookStore</name>
	<description>BookStore backend</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

		<!-- https://mvnrepository.com/artifact/MySQL/mysql-connector-java -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		</dependency>


<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>



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

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



		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</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

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

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
    <version>1.3.5.RELEASE</version>
</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.

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
&lt;artifactId&gt;spring-session&lt;/artifactId&gt;
    &lt;version&gt;1.3.5.RELEASE&lt;/version&gt;
&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:

确定