Spring Security 初识一

先看看没用Spring Security时

定一个接口

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

@RestController
public class TestController {
    @RequestMapping("test")
    public String test(){
        return "tui 渣男";
    }
}

现在 我们在pom.xml中 引入 Spring Security相关的依赖

        <!-- spring-security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

重新启动项目

访问接口 test 接口

http://127.0.0.1:8001/test

出现如下页面,需要你进行登录

这是spring security 安全机制默认拦截所有请求,需要进行认证

查看控制台会输出:

使用 user  和 控制台输出的 密码进行认证后。才能正常访问接口

参考:https://blog.csdn.net/itguangit/article/details/78923993 

 这是一个初步认识。下一节 继续介绍 spring security

原文地址:https://www.cnblogs.com/wanjun-top/p/13545132.html