属性“security.basic.enabled”已弃用:安全自动配置不再可自定义

新手上路,请多包涵

我正在使用 spring-boot-starter-parent 版本 2.0.1.RELEASE 处理 Spring Cloud 项目。

我低于警告,看起来像

属性“security.basic.enabled”已弃用:安全自动配置不再可自定义。而是提供您自己的 WebSecurityConfigurer bean。

security: basic: enabled: false 在 spring security 最新版本中被禁用。

你能指导我应该用什么代替吗?

应用.yml

 ---
server:
  port: 8888

security:
  basic:
    enabled: false

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls

          search-paths:
          - 'station*'
          repos:
            perf:
              pattern:
                - '*/perf'
              uri: https://github.com/rseroter/pluralsight-spring-cloudconfig-wa-tolls-perf
              search-paths:
               - 'station*'

pom.xml

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

在此处输入图像描述

这是我的测试课。

 @RunWith(SpringRunner.class)
@SpringBootTest
public class PluralsightSpringcloudM2ConfigserverGitApplicationTests {

    @Test
    public void contextLoads() {
    }

}

和 [在此处输入图像描述

与另一个问题无关

原文由 Jeff Cook 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
2 个回答

Spring Boot 2.0 改变了它的自动配置(包括一些属性),现在有一个单一的行为,一旦你添加了你自己的 WebSecurityConfigurerAdapter,它就会退缩。默认配置看起来像

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .and()
        .httpBasic();
}

默认配置具有生成密码的单个用户。要自定义此用户,请使用 spring.security.user 下的属性。

 spring.security.user.name=user # Default user name.
spring.security.user.password= # Password for the default user name.
spring.security.user.roles= # Granted roles for the default user name.

从 Spring Boot 2 开始,以下属性已被删除:

 security.basic.authorize-mode
security.basic.enabled
security.basic.path
security.basic.realm
security.enable-csrf
security.headers.cache
security.headers.content-security-policy
security.headers.content-security-policy-mode
security.headers.content-type
security.headers.frame
security.headers.hsts
security.headers.xss
security.ignored
security.require-ssl
security.sessions

可在此处找到替换(如果存在): 附录 A. 常见应用程序属性

要清楚:如果您创建自定义 WebSecurityConfigurerAdapter,则默认安全配置将替换为您的自定义配置:

 @EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // For example: Use only Http Basic and not form login.
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .httpBasic();
    }
}

有关详细信息,请访问 Spring 2.0 迁移指南

原文由 sn42 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是因为当您编写 security.basic.enabled = false 时,您基本上是在告诉应用程序我不关心安全性并允许所有请求。在 spring boot 2.0 之后,你不能只写那 1 个配置来使应用程序不安全。您需要编写一些代码来执行此操作。或者您可以只复制以下内容。

 package com.LockheedMartin.F22Simulator;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }
}

顺便说一下,你应该从你的 application.properties 中删除 security.basic.enabled = false ,因为 spring 2.*.* 不再理解该属性,如果你有正确的 Intellij 设置,你应该看到一条警告说 'unsupported property'

原文由 sapy 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题