Getting started with Spring Security

foreword

The previous article talked about some basic content about Spring Security, the two core modules and the basic skills needed to learn it. Next, take everyone into a basic entry case! ! !

Steps

1. Create a Springboot project

First, create a basic SpringBoot project through the idea development tool. The specific creation steps are as follows:

According to the steps in the picture above, we will operate step by step, and a project project will be created in the idea tool. Then, we will adjust the Maven of the tool to configure our Maven as our local tool.

Here is adjusted to your own maven information

2. Introduce dependencies

Next, let's adjust the dependencies in our pom file.

Because this is a web project, we will keep the content in the second red box consistent!

 After adjusting the correct dependencies, we can re-update the application dependencies of the pom!

3. Write a controller for testing

In the project, create the controller package structure, and then create a test class TestController.

 The specific code is as follows:

package com.example.demos.springsecurinty.controller;

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

/**
 * @version 1.0
 * @user: Camel
 * @date: 2023/4/11 11:09
 * @description:
 */
@RestController
@RequestMapping("test/")
public class TestController {

    @GetMapping("hello")
    public String hello(){
        return "hello security";
    }
}

As of now unknown, our simple case is done! Next, let's look at the test results

test

Start our project, the default port number is 8080, from the above we can see that our request address is: /test/hello

Enter in the browser: http://localhost:8080/test/hello

The effect picture is:

 From the figure above, we can see that after we requested the interface, the hello security string was not returned as we expected, but was intercepted by a login page. That's right, this is the default login interception page of our Spring Security security framework.

In fact, there is a default user user, and his password has been printed on the console, we can log in after entering it, and the hello security string is displayed

The user's password is printed at the position shown in the figure below:

 After entering the password, it will be displayed normally.

At this point, our simple Spring Security entry case has been successfully completed!

Summarize

Here is just a simple basic introduction to Spring Security, telling us how to create a Spring Boot project, and how to introduce the Spring Security jar package to complete a simple test case!

Let’s stop here for the introductory case, and then we will really enter the world of Spring Security.

Welcome to click on the card below to pay attention to "coder trainees"

Guess you like

Origin blog.csdn.net/ybb_ymm/article/details/130078323
Recommended