默认情况下,Spring Security是否使用基本身份验证或基于表单的身份验证?

时间:2020-10-22 10:05:50

标签: java spring-boot spring-security forms-authentication basic-authentication

我正在Spring Boot应用程序中学习有关Spring Security的知识,我尝试了解默认情况下哪种身份验证方式使用Spring Security。我知道Spring Security默认使用基于表单的身份验证,但是我使用基本身份验证从Postman进行了测试,并且可以正常工作。

我有一个非常简单的Spring Boot应用程序。

其他控制器:

package com.dgs.demsec.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello World!";
    }
}

我在pom.xml中添加了spring-security依赖项:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dgs</groupId>
    <artifactId>dem-sec</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dem-sec</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我没有Spring Security的任何配置类。它使用默认配置。 我启动了应用程序,然后尝试访问端点:http:// localhost:8080 / hello之后,我使用默认的用户名user和Spring Security生成的密码,并且可以正常工作,响应为“ Hello World!”。 。在Google Chrome开发者工具中,/ login终结点没有Authorization标头,因此它不使用基本身份验证。而且我可以在/ login端点的表单数据中看到这一点: enter image description here

因此,Spring Security默认使用基于表单的身份验证。 然后我向Postman提出了2个请求,第一个请求基于表单的身份验证,第二个请求与基本身份验证,两者都可以使用。因此,如果我在“授权”标头中发送凭据,该请求也将起作用。 如果是基于表单的身份验证,有人可以解释为什么使用基本身份验证发出请求的原因吗? 谢谢!

0 个答案:

没有答案