SpringSecurity安全框架---概念以及入门代码

前言

SpringSecurity说起来真的很重,在SSM中整合需要配置的项太多,但是流程比较清晰。在springboot中却很简单,但是流程又不太清晰,所以近期研究学习spring boot 整合SpringSecurity并且梳理流程

SpringSecurity是什么?

Spring Security 是一个能够为基于 Spring 的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在 Spring 应用上下文中配置的 Bean,充分利用了Spring IoC,DI(控制反转 Inversion of Control ,DI:Dependency Injection 依赖注入)和 AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

说的直白一点,Spring Security就是一个登陆的安全框架,不用大量的开发登陆安全代码

让我们回想一下,在传统的开发中的,我们登陆是怎么做的呢?

1.开发登陆页面

2.登陆请求后台,后台去判断账号密码的正确性、用户的权限、传输之间的加解密、防注入攻击代码、防跨站攻击代码

这样写其实没问题,Spring Security只是整合这些并且简化代码。但是可读性,个人认为变差。

Spring Security提供的功能

  • 认证
  • 授权
  • 攻击防护(防止伪造身份)

认证(authentication)

认证指的是,建立西永使用者信息的过程。这里的使用者可以是一个用户,设备,和可以在应用程序中执行某种操作的其他系统。我们这里直说用户认证。

用户认证一般要求用户提供用户名和密码,系统通过检验用户名和密码的正确性来完成认证的通过或者拒绝的过程。

Spring Security支持主流的认证方式,包括HTTP基本认证、HTTP表单验证、HTTP摘要认证、OpenID和LDAP等。(后面会有demo,使用HTTP表单验证)

Spring Security进行验证的步骤如下:

1.用户使用用户名和密码登陆

2.过滤器(UsernamePasswordAuthenticationFilter)获取到用户名、密码,然后封装成Authentication.

3.AuthenticationManager认证token(Authentication的实现类传递)

4.AuthenticationManager认证成功,返回一个封装了用户权限信息的Authentication对象,用户的上下文信息(角色列表等)

5.Authentication对象赋值给当前的SecurityContext,建立这个用户的安全上下文(通过调用SecurityContextHolder.getContext().setAuthentication())

6.用户进行一些受到访问控制机制保护的操作,访问控制机制会依据当前安全上文信息检查这个操作所需的权限。

授权(authorization)

授权其实就是允许用户去操作那些页面,或者那些页面不需要用户信息就直接能访问。权限格式是固定格式:“ROLE_“,例如ROLE_ADMIN

入门代码

使用idea创建springboot项目引入spring security组件即可

生成的pom.xml,使用idea引入默认spring security 5

<?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.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.security</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</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>
        </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>

我们入门的项目就搭建好了,让我们启动一下

我们可以看到控制台上输出了security password,security默认提供登陆页面,自动生成的一个登陆密码(此登陆密码,每启动一次会改变一次),账号是user。(在实际开发不会使用这个页面的),让我们访问一下http://localhost:8080/login

使用上面的账号密码登陆

看到这个错误页面,说明已经登陆成功了,因为在跳转的时候,我们没有设置跳转页面,所以404了

好了,入门代码就到此为止了。

下一篇文章,我们来做security的登陆验证。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值