无法解析 spring boot 应用程序中的符号“security”

我正在尝试学习 Java Spring 并像这里一样做:https ://spring.io/guides/gs/securing-web/


但是我的 IDE 在导入配置时说“无法解析‘安全’符号”。我使用相同版本的 spring 并具有相同的代码。我不知道出了什么问题。


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);

    }

}


翻阅古今
浏览 226回答 1
1回答

UYOU

看起来您当前的spring-security依赖项仅适用于test范围。如果我们按照教程进行操作,它会声明您必须为添加以下依赖项WebSecurityConfig:<dependencies>&nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-security</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; ...</dependencies>请注意,此处未设置范围,即它将是默认范围,即compile.您可能需要在更改依赖项后手动重新导入 maven 项目:重新导入后,里面的依赖pom.xml应该不会显示为红色了。轻微更新可以 146% 确定,这是我的 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"&nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>org.springframework</groupId>&nbsp; &nbsp; <artifactId>gs-securing-web</artifactId>&nbsp; &nbsp; <version>0.1.0</version>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.6.RELEASE</version>&nbsp; &nbsp; </parent>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-thymeleaf</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.security</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-security-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-security</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build></project>更新 2 - 修复实际当前问题实际上问题(以及问题文本本身)import只是.HttpSecurityWebSecurityConfig所以加入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 也开始显示导入的类。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java