SpringSecurity-基本概念和处理流程

本文介绍了SpringSecurity的基本概念和处理流程,包括认证和授权问题、基本类架构、Authentication与UserDetails接口、UserDetails的获取、Authentication的来源以及比较认证过程。重点讲解了AuthenticationManager和AuthenticationProvider在认证过程中的作用,以及SecurityContextHolder如何存储安全上下文信息。
摘要由CSDN通过智能技术生成

SpringSecurity-基本概念和处理流程分析

优秀文章推荐:
1、https://www.cnkirito.moe/spring-security-1/
2、https://www.jianshu.com/p/e8e0e366184e
文章较长,望耐心阅读。

首先,需要明白SpringSecurity它解决什么问题?
1、认证:证明你是你的问题,通常就是用用户名和密码来证明。
2、授权:你能干什么的问题,就像类似VIP和VVIP的区别。

1、基本类架构

图片参考优秀文章中
图片来自文章:https://www.cnkirito.moe/spring-security-1/

以上当作一个普通的类图就行了,是一个经典的委托模式,后面给大家娓娓道来。

2、SpringSecurity是如何认证的呢?

无非就是比较你输入的数据和服务器保存的数据是否一致嘛!
对,就是这样的

1) 你输入的用户名和密码,再SpringSecurity中的就映射为Authentication
2)服务器记录你的用户名和密码,在SpringSecurity中就映射为UserDetails;
3) 那么比较Authentication对象和UserDetails的相关内容,就可以解决你是不是你的问题了。

2.1 Authentication是一个接口

这个接口,可以得到用户拥有的权限信息列表,密码,用户细节信息,用户身份信息,认证信息。
源码中该接口描述:后面会讲一下这是什么意思,不慌!
Represents the token for an authentication request or for an authenticated principal once the request has been processed by the
{@link AuthenticationManager#authenticate(Authentication)} method.

package org.springframework.security.core;
/**
Represents the token for an authentication request or for an authenticated principal 
 once the request has been processed by the
  {@link AuthenticationManager#authenticate(Authentication)} method.
*/
public interface Authentication extends Principal, Serializable {
	//权限信息列表,默认是 GrantedAuthority 接口的一些实现类,通常是代表权限信息的一系列字符串。
	Collection<? extends GrantedAuthority> getAuthorities();
	//密码信息,用户输入的密码字符串,在认证过后通常会被移除,用于保障安全。
	Object getCredentials();
	//细节信息,web 应用中的实现接口通常为 WebAuthenticationDetails,它记录了访问者的 ip 地址和 sessionId 的值。
	Object getDetails();
	//最重要的身份信息,大部分情况下返回的是 UserDetails 接口的实现类,也是框架中的常用接口之一。
	Object getPrincipal();
	boolean isAuthenticated();
	void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

Object getPrincipal();这个方法会返回我们的UserDetails
Authentication是一个接口,Spring给我们提供了很多实现类,可以直接使用

  1. UsernamePasswordAuthenticationToken
  2. RememberMeAuthenticationToken
  3. AnonymousAuthenticationToken
  4. 。。。

2.2 UserDetails也是一个接口

这个接口主要由用户实现,封装了服务器中保存的用户详细信息,并且最后Authentication中的部分信息也由该对象提供
我自己可能说的不太清楚,可以上一段源码描述:
Implementations are not used directly by Spring Security for security purposes. They simply store user information which is later encapsulated into {@link Authentication} objects

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值