一、所需的组件

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. 风炫安全web安全学习第三十二节课 Python代码执行以及代码防御措施

    风炫安全web安全学习第三十二节课 Python代码执行以及代码防御措施 Python 语言可能发生的命令执行漏洞 内置危险函数 eval和exec函数 eval eval是一个python内置函数, ...

  2. shell实现99乘法表

    #!/usr/bin/env sh for ((x=1;x<10;x++));do for ((y=1;x>=y;y++));do printf "${y}x${x}=$(exp ...

  3. Web Service 服务无法连接Oracle数据库

    这个问题之前部署就遇到过,但是后来忘了,所以记录一下吧. 我部署Web Service服务的时候,服务没法正常运行,与Oracle数据库无法正常通信. 检查了数据库连接字没有任何问题,写了个测试接口, ...

  4. 九个最容易出错的 Hive sql 详解及使用注意事项

    阅读本文小建议:本文适合细嚼慢咽,不要一目十行,不然会错过很多有价值的细节. 文章首发于公众号:五分钟学大数据 前言 在进行数仓搭建和数据分析时最常用的就是 sql,其语法简洁明了,易于理解,目前大数 ...

  5. es6 Array.from + new Set 去重复

    // es6 set数据结构 生成一个数据集 里面的元素是唯一的 const items = new Set([1, 2, 3, 4, 5, 5, 5, 5]); // items 是个对象 item ...

  6. 【JavaWeb】i18n 国际化

    i18n 国际化 什么是 i18n 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问. 希望相同的一个网站,不同人访问的时候 ...

  7. ctfhub技能树—信息泄露—hg泄露

    打开靶机 查看页面信息 使用dvcs-ripper工具进行处理 ./rip-hg.pl -v -u http://challenge-cf630b528f6f25e2.sandbox.ctfhub.c ...

  8. win10打开IIS服务并发布网站

    1.打开控制面板 win+x后点击控制面板 2.点击程序集下边的解除安装程式 3.点击开启或关闭windows功能 4.找到Internet information services并勾选前面的复选框 ...

  9. Ubuntu Terminal命令行新建仓库并推送到远程仓库

    通常情况下,在本地新建一个仓库之后,需要在远端网页端也新建一个空的同名仓库,然后将两者进行关联才能推送. 那有没有办法直接在命令行就完成从新建到推送的过程而不需要中间在网页端也操作一番呢?办法当然是有 ...

  10. python3.8.1安装cx_Freeze

    按照官网的提示命令python -m pip install cx_Freeze --upgrade安装,不成功,报了一个错误,说cx_Freeze找不到需要的版本,还有一些警告说PIP需要升级,没理 ...