The opening

I recently learned about Spring Security and successfully integrated it into the project (based on Sping-boot2.x).

Here, prepare to document the learning process and describe how it can be applied step by step to the project.

Create a Springboot project (SpringBoot2.x)

The source code for this and subsequent articles is uploaded to the following address.

Github.com/nimo10050/s…

Introduce POM dependencies


      
<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.7. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-01</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>demo-01</name>

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

    </dependencies>

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

</project>

Copy the code

Create a new Controller class

package com.example.demo.controller;

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

@RestController
public class IndexController {

    @GetMapping("/hello")
    public Object sayHello(a){
	return "hello world"; }}Copy the code

Start the project

After the success of the project started, use the browser to access interface, http://localhost:8080/hello, if the project is not introduced in the Spring Security dependence, interface the return value is the string “hello world”.

But because we the actual effect is here, the browser will automatically jump to the http://localhost:8080/login login page.

As shown below:

Why did this happen

To put it simply, when we introduced the Spring Secutiry dependency without doing any configuration, there was already a filter in place and by default all interfaces required login to access.

Because we are now based on actual combat, so we will not discuss the principle inside, and we will write articles to explain it.

How do we get the results we want

Spring Secutiry provides a user name by default. The default value is user, which can be found in the source code.

The password is already printed on the console at project startup.

When we enter the user name and password on the login page, we get the interface return value we expect.