AuthenticationManager是一个接口:

public interface AuthenticationManager {
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}

ProviderManager是AuthenticationManager的实现类:

public class ProviderManager implements AuthenticationManager, MessageSourceAware,
InitializingBean { ...... private List<AuthenticationProvider> providers = Collections.emptyList(); ...... public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
......
}
}

从以上代码中可以看到ProviderManager有一个List<AuthenticationProvider> providers成员变量。AuthenticationProvider也是一个接口:

public interface AuthenticationProvider {
Authentication authenticate(Authentication authentication)
throws AuthenticationException;
boolean supports(Class<?> authentication);
}

可以看到包含两个成员函数authenticate和supports。

接下来我们看一下整个的认证过程:

认证是通过AuthenticationManager的authenticate函数实现的。也就是通过AuthenticationManager实现类ProviderManager的authenticate函数认证,ProviderManager的authenticate函数会轮训ProviderManager的List<AuthenticationProvider> providers成员变量,如果该providers中如果有一个AuthenticationProvider的supports函数返回true,那么就会调用该AuthenticationProvider的authenticate函数认证,如果认证成功则整个认证过程结束。如果不成功,则继续使用下一个合适的AuthenticationProvider进行认证,只要有一个认证成功则为认证成功。

如果上述过程没有认证成功,且该ProviderManager的成员变量AuthenticationManager parent不为null,那么会使用该parent继续认证。一般不会用到该AuthenticationManager parent,稍微留意以下即可。

另:Authentication

可以看到authenticate函数返回Authentication,Authentication是一个接口,通过该接口可以获得用户相信信息,代码:

public interface Authentication extends Principal, Serializable {
Collection<? extends GrantedAuthority> getAuthorities();
Object getCredentials();
Object getDetails();
Object getPrincipal();
boolean isAuthenticated();
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

另:DaoAuthenticationProvider

<authentication-provider>默认实例化AuthenticationProvider的一个实现:DaoAuthenticationProvider。DaoAuthenticationProvider通过接口UserDetailsService的实现类从内存或DB中获取用户信息UserDetails(UserDetails十分类似Authentication,也是一个接口,但是与Authentication用途不同,不要搞混)。DaoAuthenticationProvider通过函数authenticate比较入参authentication与UserDetails是否相符,来判断用户是否可以登录。如果相符,会将获得的UserDetails中的信息补全到一个Authentication实现类,并将该实现类作为认证实体返回。以后便可以通过当前上下文的认证实体Authentication获取当前登录用户的信息。

UserDetails代码:

public interface UserDetails extends Serializable {
Collection<? extends GrantedAuthority> getAuthorities();
String getPassword();
String getUsername();
boolean isAccountNonExpired();
boolean isAccountNonLocked();
boolean isCredentialsNonExpired();
boolean isEnabled();
}

UserDetails和Authentication区别:

接口 目的
Authentication

它存储安全实体的标识、密码以及认证请求
的上下文信息。它还包含用户认证后的信息
(可能会包含一个 UserDetails 的实例)
。通常
不会被扩展,除非是为了支持某种特定类型
的认证。

UserDetails

为了存储一个安全实体的概况信息,包含名
字、e-mail、电话号码等。通常会被扩展以支
持业务需求。

AuthenticationManager, ProviderManager 和 AuthenticationProvider的更多相关文章

  1. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  2. Spring Security(三十二):10. Core Services

    Now that we have a high-level overview of the Spring Security architecture and its core classes, let ...

  3. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  4. spring security梳理

    核心服务:AuthenticationManager,UserDetailsService和AccessDecisionManager The AuthenticationManager, Provi ...

  5. AuthenticationManager、ProviderManager

    本篇主要讲述以下几点: 1.AuthenticationManager.ProviderManager和AuthenticationProvider三者之间的关系 2.以UsernamePasswor ...

  6. spring security 配置多个AuthenticationProvider

    前言 发现很少关于spring security的文章,基本都是入门级的,配个UserServiceDetails或者配个路由控制就完事了,而且很多还是xml配置,国内通病...so,本文里的配置都是 ...

  7. Spring Security 实战干货:AuthenticationManager的初始化细节

    1. 前言 今天有个同学告诉我,在Security Learning项目的day11分支中出现了一个问题,验证码登录和其它登录不兼容了,出现了No Provider异常.还有这事?我赶紧跑了一遍还真是 ...

  8. Spring Security Filter详解

    Spring Security Filter详解 汇总 Filter 作用 DelegatingFilterProxy Spring Security基于这个Filter建立拦截机制 Abstract ...

  9. Spring Security 概念基础 验证流程

    Spring Security 概念基础 验证流程 认证&授权 认证:确定是否为合法用户 授权:分配角色权限(分配角色,分配资源) 认证管理器(Authentication Manager) ...

随机推荐

  1. Java实践 — SSH远程执行Shell脚本(转)

    原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介         SSH是Secure Shell的缩写,一 ...

  2. 【.NET-EF】Entity Framework学习笔记1 - VS2013没有EF的解决方法

    解决方法:我本来也没有,百度了一下,在C:\ProgramData\Package Cache\{08AEF86A-1956-4846-B906-B01350E96E30}v12.0.20912.0\ ...

  3. DOG角点检测——opencv实现

    1.原理 Difference of Gaussian(DOG)是高斯函数的差分.将两幅图像在不同参数下的高斯滤波结果相减,得到DoG图.步骤: 处理一幅图像在不同高斯参数下的DoG 用两个不同的5x ...

  4. Java学习笔记-Thread-线程

    //2015年5月5日16:55:00 //Main package com.alfredsun.thread; public class Main { public static void main ...

  5. Ubuntu14.04安装PHP5

    因为任务需要在Ubuntu14.04的server版本下安装PHP5,所以总结一下   使用root进行安装  要么在前面加上sudo进行安装. apt-get install php5-cgi ap ...

  6. 加班计时App

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 拿来之笔 希望铭记 笔记 出处 http://www.jianshu.com/p/acb8885283dc

    最近有机会对不同岗位的应聘者进行面试,其中有架构师.技术经理.开发岗位.谈谈几个印象深刻的. 面试者一,女性.重点大学硕士,从事软件技术工作十四年,应聘架构师岗位.按照套路问了下对于软件架构的认识和理 ...

  8. hdu1722

    链接 一份切成q份需要q刀,切成p份需要p刀:切的部分总会有重复,即gcd(p,q),减去重复部分就是要切的刀数 #include<stdio.h> int gcd(int n,int m ...

  9. [DP之普通系列]

    noip快要来了 要练练dp 难度也挺接近 还是挺好的 [Usaco2013 Nov]Pogo-Cow 这一道题要下一段大于这一段 所以的话我们就要记录每一段的状态 F[i,j]=F[j,k]+A[i ...

  10. Hadoop集群出现no data node to stop的解决方案

    问题描述: 今天stop hadoop集群的时候出现no datanode to stop ,寻找解决方案,并不是网上资料所说的什么DFS Used .Non DFS Used等于0 .所有的节点都是 ...