一、所需的组件

SpringBoot项目需要的POM依赖:

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

具体的版本号是跟随你的Boot版本走的:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

其他常规依赖:

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

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

二、上手入门

在static目录下编写一个首页:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project-Index</title>
</head>
<body>
<h1>Hello Spring-Security !!!</h1>
</body>
</html>

启动项目访问项目地址:

http://locahost:8080

会发现被重定向到这个地址,并且要求登陆账号

http://localhost:8080/login

Spring-Security默认的用户是user,但是密码是由组件Spring-Security-Test随机生成的

在控制台输出可以找到:

登陆成功之后,才会跳转到页面中来:

参考地址:

https://www.bilibili.com/video/BV12D4y1U7D8?p=3

三、UserDetailService接口分析

package org.springframework.security.core.userdetails;
public interface UserDetailsService {
UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException;
}

只有一个接口方法,通过用户名加载账号信息

注意,返回的是一个UserDetails类型对象,账号细节实例

再来看UserDetails接口:

package org.springframework.security.core.userdetails;

import java.io.Serializable;
import java.util.Collection;
import org.springframework.security.core.GrantedAuthority; public interface UserDetails extends Serializable {
Collection<? extends GrantedAuthority> getAuthorities(); String getPassword(); String getUsername(); boolean isAccountNonExpired(); boolean isAccountNonLocked(); boolean isCredentialsNonExpired(); boolean isEnabled();
}

该类型也是一个接口,不过多出了其他一些方法:

1、Collection<? extends GrantedAuthority> getAuthorities();
获取账户对应的所有权限的集合对象 2、String getPassword();
获取密码 3、String getUsername();
获取用户名 4、boolean isAccountNonExpired();
账户是否过期,过期的账户不可以被认证 5、boolean isAccountNonLocked();
账户状态是否为锁定,锁定的账户不可以被认证 6、boolean isCredentialsNonExpired();
凭证是否过期,凭证即密码 7、boolean isEnabled();
账户状态是否为可用

相比于Shiro来说,UserDetailsService更像Shiro的Realm

而这个UserDetails对象在ShIro中是需要我们自己来实现的,例如之前的项目中的ActivateUser。

注意上面权限集合类型,官方文档特别注释该方法返回不可以为NULL。

当然,Security也提供了对应的一些实现类:

四、密码加密 PasswordEncoder接口

package org.springframework.security.crypto.password;

public interface PasswordEncoder {
String encode(CharSequence var1); boolean matches(CharSequence var1, String var2); default boolean upgradeEncoding(String encodedPassword) {
return false;
}
}

API说明:

1、String encode(CharSequence var1);
设置密码加密的方法,加密逻辑自行实现 2、matches(CharSequence var1, String var2);
匹配判断,var1 是原始密码,var2 是加密的密码
匹配逻辑自行实现 3、upgradeEncoding(String encodedPassword) {
判断是否能够二次加密密码,一般不使用,且默认false

该接口下的实现类:

官方推荐的实现类使用这个:

org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

API测试:

package cn.zeal4j;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; @SpringBootTest
class SecurityApplicationTests { @Test
void contextLoads() {
cryptTest();
} private static void cryptTest() {
final String PASSWORD = "123456";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encode = passwordEncoder.encode(PASSWORD);
System.out.println(encode); boolean matches = passwordEncoder.matches(PASSWORD, encode);
System.out.println(matches);
} }

结果:

$2a$10$NNySuHkEHtzLHodCivAFN.FvakFpR6/tSpkgzDW4QPd8PMF5IBaza
true

【Spring-Security】Re01 入门上手的更多相关文章

  1. spring security 简单入门

    spring security 简单入门示例 一.概述 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架 . 其中最主要的安全操作有两 ...

  2. Spring Security框架入门

    1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框 ...

  3. springboot集成spring security安全框架入门篇

    一. :spring security的简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下 ...

  4. Spring Boot中使用 Spring Security 构建权限系统

    Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...

  5. Spring Security(一):官网向导翻译

    原文出自  https://spring.io/guides/topicals/spring-security-architecture Spring Security Architecture   ...

  6. SpringBoot安全篇Ⅵ --- 整合Spring Security

    知识储备: 关于SpringSecurity的详细学习可以查看SpringSecurity的官方文档. Spring Security概览 应用程序的两个主要区域是"认证"和&qu ...

  7. [转]Spring Security架构

    作者:before31原文:https://my.oschina.net/xuezi/blog/3126351 本指南是Spring Security的入门,它提供了对该框架的设计和基本构建的见解.我 ...

  8. Spring Security 入门(1-1)Spring Security是什么?

    1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...

  9. Spring Security 入门

    一.Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配 ...

随机推荐

  1. SQL Server 数据库还原进度查看

    SQL Server 数据库还原进度查看 关键字:数据库,还原,进度,查看 文档说明: 本文档受某实际需求启发,某约500G大小数据库还原,由于对应服务器性能较差(内存仅4G且可用内存仅2.8G),数 ...

  2. WPF时间长度自定义选择控件TimeSpanBox

    以下控件采用https://www.cnblogs.com/cssmystyle/archive/2011/01/17/1937361.html部分代码 以下控件采用https://www.cnblo ...

  3. VsCode/Pycharm配合python env 使用

    前言 用惯了vscode,这几天试了一下pycharm,还是回来了. pycharm一个好处就是python env 环境支持的比较好, vscode虽然也支持但是要改一些东西 env的使用查看我的上 ...

  4. Maven项目编译之后xml文件不存在

    如题: 问题请看图,target目录是编译之后的,发现并没有对应的mapper.xml文件 原因: maven项目默认不加载此类文件 解决办法有两个: 其一是将mybatis的xml映射文件放在mav ...

  5. 谈谈你不知道的gist

    1.Gist是什么关于Gist的详细介绍,请阅读官方文档About gists,下面只简略介绍部分功能: Gist是一种与其他人共享代码片段和粘贴的简单方法. 当您需要与同事或朋友共享示例代码或技术时 ...

  6. Docker Java 镜像基础(四)

    基于官方提供的centos 7.2.1511 基础镜像构建JDK 和tomcat 镜像,先构建JDK镜像,然后在基于JDK镜像构建tomcat镜像 构建 centos:latest 基础镜像: # 下 ...

  7. React & Vue2 Butterfly图编排——让数据更自由地驱动DAG

    一.简介 Butterfly是由阿里云-数字产业产研部孵化出来的的图编辑器引擎,由咱们部门以及其他开发者共同维护开发,具有使用自由.定制性高的优势,已支持集团内外上百张画布,不夸张的说,我觉得可以算的 ...

  8. 【Linux】常用的Linux可插拔认证模块(PAM)应用举例:pam_limits.so、pam_rootok.so和pam_userdb.so模块

    常用的Linux可插拔认证模块(PAM)应用举例:pam_limits.so.pam_rootok.so和pam_userdb.so模块 pam_limits.so模块: pam_limits.so模 ...

  9. centos7安装宝塔面板

    在终端下执行如下命令 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.s ...

  10. apijson简单使用

    apijson简单使用 介绍 APIJSON 是一种专为 API 而生的 JSON 网络传输协议 以及 基于这套协议实现的 ORM 库.为简单的增删改查.复杂的查询.简单的事务操作 提供了完全自动化的 ...