添加依赖项后,无法导入依赖项类

时间:2019-08-29 00:47:43

标签: spring spring-boot spring-security

向我的POM.xml文件添加依赖项并使用mvndependency:resolve重建后,我无法将依赖项导入到我的类中。例如,我将安全性依赖项添加到了pom.xml文件中,但无法从org.springframework导入Spring Boot Security程序包。 它说该符号无法解析。

我尝试重建我的项目,但是它不起作用。仅当我使用具有安全依赖性的Spring Intializr重新初始化我的项目时,才能导入。

我的POM:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.drkalamlibrary</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-backend</name>
    <description>Presidential Library </description>

    <properties>
        <java.version>11</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

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

</project>

我正在尝试在以下方面使用安全性依赖的类:

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.context.annotation.Configuration;


@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
     http.authorizeRequests()
    .anyRequest()
    .permitAll()
    .and().csrf().disable();
   }
}

每当我尝试从SpringBoot导入安全类时,在导入下方都会出现一个红色的下划线。

1 个答案:

答案 0 :(得分:0)

在删除test范围之后尝试导入您的spring安全依赖项。像这样:

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

test范围仅加载测试阶段的依赖项,就像this帖子中提到的那样。这就是为什么您无法为弹簧安全性加载导入文件的原因。