关于springboot:Spring-Boot-集成-SpringSecurity-入门教程

2次阅读

共计 2074 个字符,预计需要花费 6 分钟才能阅读完成。

Spring-Security 简介

官网简介

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

Spring 是一个十分风行和胜利的 Java 利用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 利用安全性的残缺解决方案。一般来说,Web 利用的安全性包含用户认证(Authentication)和用户受权(Authorization)两个局部。用户认证指的是验证某个用户是否为零碎中的非法主体,也就是说用户是否拜访该零碎。用户认证个别要求用户提供用户名和明码。零碎通过校验用户名和明码来实现认证过程。用户受权指的是验证某个用户是否有权限执行某个操作。在一个零碎中,不同用户所具备的权限是不同的。比方对一个文件来说,有的用户只能进行读取,而有的用户能够进行批改。一般来说,零碎会为不同的用户调配不同的角色,而每个角色则对应一系列的权限。

对于下面提到的两种利用情景,Spring Security 框架都有很好的反对。在用户认证方面,Spring Security 框架反对支流的认证形式,包含 HTTP 根本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户受权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),能够对利用中的畛域对象进行细粒度的管制。

环境搭建

在线生成示例工程,需集成 Spring Web 和 Thymeleaf,我抉择的 springboot 版本为 2.7.15,截图如下:

工程 POM 文件减少 spring-security 依赖:

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

工程减少动态资源文件:

application.properties 文件:

# 敞开模板引擎
spring.thymeleaf.cache=false

减少管制层文件:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {@RequestMapping({"/","/index"})
    public String index(){return "index";}

    @RequestMapping(value = "/toLogin")
    public String toLogin() {return "views/login";}

    @RequestMapping(value = "/level1/{id}")
    public String level1(@PathVariable("id") Integer id) {return "views/level1/" + id;}

    @RequestMapping(value = "/level2/{id}")
    public String level2(@PathVariable("id") Integer id) {return "views/level2/" + id;}

    @RequestMapping(value = "/level3/{id}")
    public String level3(@PathVariable("id") Integer id) {return "views/level3/" + id;}
}

启动工程,拜访 http://localhost:8080,测试是否能够关上主页:

官网文档
Spring-Security 5.3.0.RELEASE reference
Spring-Security 5.7 在线官网文档

参考博客
https://www.codenong.com/cs109481518/
https://www.cnblogs.com/shunWcs/p/14893585.html

正文完
 0