一、所需的组件

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. 注意力论文解读(1) | Non-local Neural Network | CVPR2018 | 已复现

    文章转自微信公众号:[机器学习炼丹术] 参考目录: 目录 0 概述 1 主要内容 1.1 Non local的优势 1.2 pytorch复现 1.3 代码解读 1.4 论文解读 2 总结 论文名称: ...

  2. 为什么Elasticsearch不适合做数据存储?(转学习使用)

    一.问题描述 公司想尝试使用Elasticsearch来存一部分数据,以此缓解数据增长带来的对数据库的压力.在研究了一段时间后,发现Elasticsearch不适合作为数据存储使用. 二.理由如下 1 ...

  3. php利用腾讯ip分享计划获取地理位置示例分享

    <?php function getIPLoc_QQ($queryIP){ $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$quer ...

  4. 笔记:学习go语言的网络基础库,并尝试搭一个简易Web框架

    在日常的 web 开发中,后端人员常基于现有的 web 框架进行开发.但单纯会用框架总感觉不太踏实,所以有空的时候还是看看这些框架是怎么实现的会比较好,万一要排查问题也快一些. 最近在学习 go 语言 ...

  5. 【Flutter】功能型组件之数据共享

    前言   InheritedWidget提供了一种数据在widget树中从上到下传递.共享的方式,例如在应用的根widget中通过InheritedWidget共享了一个数据,那么便可以在子widge ...

  6. intellij idea2020将javaWeb项目打成war包并部署到阿里云服务器遇到java.lang. UnsupportedClass VersionError问题(已解决)

    首先将javaweb项目打包成war文件(有关如何打包参考 https://jingyan.baidu.com/article/20b68a88642829386cec62f7.html.https: ...

  7. torch.nn 的本质

    torch.nn 的本质 PyTorch 提供了各种优雅设计的 modules 和类 torch.nn,torch.optim,Dataset 和 DataLoader 来帮助你创建并训练神经网络.为 ...

  8. spring cloud config —— git配置管理

    目录 talk is cheep, show your the code Server端 pom.xml server的application.yml 配置文件 测试Server client端 po ...

  9. 一道有趣的golang排错题

    很久没写博客了,不得不说go语言爱好者周刊是个宝贝,本来想随便看看打发时间的,没想到一下子给了我久违的灵感. go语言爱好者周刊78期出了一道非常有意思的题目. 我们来看看题目.先给出如下的代码: p ...

  10. mysqlbinlog :unknown variable 'default-character-set=gbk'

    mysqlbinlog :unknown variable 'default-character-set=gbk'  解决方法: 在命令行中加入--no-defaults开关,使用mysqlbinlo ...