java - SimpleGrantedAuthority 无法解析

标签 java spring maven

我想将 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/

相关文章:

带有配置文件的 Java Spring Autowiring

java - 使用 SpringFramework 进行 Junit 测试,不使用 Maven : Error - NoSuchMethodError

java - 如何在applicationContext中从Spring中排除一个类?

java - JPA实体继承,无需额外的表

Java boolean 标志的排列组合

java - 即时更改 Eclipse 标题中按钮上的文本

java - 如何使用适用于 Java 的 Google Cloud 客户端库列出文件和文件夹

java - 外部资源(JS)在 spring mvc app 中的位置

postgresql - Liquibase 不会在 Postgres 上删除序列

java - HBase读取: To improve on performance ,如何使用hbase java REST api批量处理get请求