无法解析符号“安全"在 Spring Boot 应用程序中 [英] Can not resolve symbol "security" in spring boot application

查看:54
本文介绍了无法解析符号“安全"在 Spring Boot 应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 Java Spring 并且完全像这里一样:

重新导入后,pom.xml中的依赖项不应显示为红色.

小幅更新

146% 肯定,这是我的 pom.xml 有效:

<modelVersion>4.0.0</modelVersion><groupId>org.springframework</groupId><artifactId>gs-securing-web</artifactId><version>0.1.0</version><父母><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><范围>测试</范围></依赖><依赖><groupId>org.springframework.security</groupId><artifactId>spring-security-test</artifactId><范围>测试</范围></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></依赖></依赖项><属性><java.version>1.8</java.version></属性><构建><插件><插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>

更新 2 - 修复当前的实际问题

实际上问题(以及问题文本本身)只是在 WebSecurityConfig 中缺少 HttpSecurity 类的 import.

所以添加

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

将使代码编译并SpringSiteApplication启动.

更新 3 - 在我们通过 TeamViewer 进行调查后,真正的问题是什么.

Spring 依赖项尚未下载,因为计算机无法访问中央 maven 存储库 (https://repo.maven.apache.org/maven2).禁用 Windows 防火墙后,连接开始工作.

IDEA 在更新 repo 索引时速度太慢,但命令行 mvn install 有助于最终下载库.之后,IDEA 也开始显示导入的类.

I'm trying to learn Java Spring and doing exactly like here: https://spring.io/guides/gs/securing-web/

But my IDE says "cannot resolve 'security' symbol" while importing config. I'm using the same version of spring and have the same code. I don't know what is wrong.

package com.github.SpringSite.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
                User.withDefaultPasswordEncoder()
                        .username("user")
                        .password("password")
                        .roles("USER")
                        .build();

        return new InMemoryUserDetailsManager(user);
    }
}

My 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 http://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.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.github</groupId>
    <artifactId>SpringSite</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringSite</name>
    <description>Simple website using Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


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

</project>

I would also be grateful for the recommendation of a good guide to learn spring.

解决方案

It looks that your current spring-security dependency is only for test scope.

If we follow the tutorial, it states that you have to add the following dependency for WebSecurityConfig:

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

Note that the scope is not set here, i.e. it will be the default scope, which is compile.

You may need to manually re-import the maven project after changing dependencies:

After re-importing, the dependencies in pom.xml shouldn't be displayed as red.

Slight update

To be 146% sure, here is my pom.xml that works:

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

    <groupId>org.springframework</groupId>
    <artifactId>gs-securing-web</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

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


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

</project>

Update 2 - fix of actual current problem

Actually the problem (as well as in the question text itself) is just a missing import of HttpSecurity class in WebSecurityConfig.

So adding

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

will make the code compile and SpringSiteApplication start.

Update 3 - what was the REAL problem after we investigated it via TeamViewer.

The Spring dependencies haven't been downloaded, since the computer had no access to central maven repo (https://repo.maven.apache.org/maven2). After disabling Windows firewall, the connections started to work.

IDEA was too slow when updating repo indexes, but a command-line mvn install helped to finally download the libraries. After it, the IDEA started to show the imported classes as well.

这篇关于无法解析符号“安全"在 Spring Boot 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆