gpt4 book ai didi

java - SimpleGrantedAuthority 无法解析

转载 作者:行者123 更新时间:2023-12-01 11:29:02 25 4
gpt4 key购买 nike

我想将 spring security 添加到我的项目中,因此我创建了一个类来管理身份验证:

public class MyAuthenticationProvider implements AuthenticationProvider {

private static final List<GrantedAuthority> AUTHORITIES
= new ArrayList<GrantedAuthority>();

static {
AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
}

@Override
public Authentication authenticate(Authentication auth)
throws AuthenticationException {

if (auth.getName().equals(auth.getCredentials())) {
return new UsernamePasswordAuthenticationToken(auth.getName(),
auth.getCredentials(), AUTHORITIES);
}

throw new BadCredentialsException("Bad Credentials");

}

@Override
public boolean supports(Class<?> authentication) {

if ( authentication == null ) return false;
return Authentication.class.isAssignableFrom(authentication);

}
}

问题是它无法解析为 simplegranted 对象,导致出现此错误:

The import org.springframework.security.core.authority.SimpleGrantedAuthority cannot be resolved.

我认为这是一个依赖问题,所以这是我的 pom.xml:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Spring3HibernateMaven</groupId>
<artifactId>Spring3HibernateMaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<description></description>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
</plugin>
</plugins>
</build>
<!-- this is used to download jta-1.0.1B.jar dependency fo hibernate entity -->
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- spring security dependency -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${org.springframework.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- end spring security dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
</dependencies>
<properties>
<org.springframework.version>3.0.2.RELEASE</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

我使用的是 spring 版本 3.0.2.RELEASE

最佳答案

org.springframework.security.core.authority.SimpleGrantedAuthority 在 Spring Security 3.0.x 中不存在。

它是在 Spring Security 3.1.0 中添加的。

Spring Security 的稳定版本现在已经到了 4.0.1...你真的打算使用 3.0.2 吗?

顺便说一句,Spring 并没有在其所有产品中使用单一版本。 Spring框架最高版本为4.1.6。

关于java - SimpleGrantedAuthority 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com