Spring Security原理与应用

Spring Security是什么

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean(注:包括认证与权限获取、配置、处理相关实例),充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)(注:代理增强类)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

核心类库与认证流程

核心验证器

AuthenticationManager

该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数;

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

验证逻辑

AuthenticationManager 接收 Authentication 对象作为参数,并通过 authenticate(Authentication) 方法对其进行验证;AuthenticationProvider实现类用来支撑对 Authentication 对象的验证动作;UsernamePasswordAuthenticationToken实现了 Authentication主要是将用户输入的用户名和密码进行封装,并供给 AuthenticationManager 进行验证;验证完成以后将返回一个认证成功的 Authentication 对象;

ProviderManager

它是 AuthenticationManager 的一个实现类,提供了基本的认证逻辑和方法;它包含了一个 List<AuthenticationProvider> 对象,通过 AuthenticationProvider 接口来扩展出不同的认证提供者(当Spring Security默认提供的实现类不能满足需求的时候可以扩展AuthenticationProvider 覆盖supports(Class<?> authentication) 方法);

实现逻辑

public Authentication authenticate(Authentication authentication) throws AuthenticationException { //#1.获取当前的Authentication的认证类型 Class<? extends Authentication> toTest = authentication.getClass(); AuthenticationException lastException = null; Authentication result = null; boolean debug = logger.isDebugEnabled(); //#2.遍历所有的providers使用supports方法判断该provider是否支持当前的认证类型,不支持的话继续遍历 for (AuthenticationProvider provider : getProviders()) { if (!provider.supports(toTest)) { continue; } if (debug) { logger.debug("Authentication attempt using " + provider.getClass().getName()); } try { #3.支持的话调用providerauthenticat方法认证 result = provider.authenticate(authentication); if (result != null) { #4.认证通过的话重新生成Authentication对应的Token copyDetails(authentication, result); break; } } catch (AccountStatusException e) { prepareException(e, authentication); // SEC-546: Avoid polling additional providers if auth failure is due to // invalid account status throw e; } catch (InternalAuthenticationServiceException e) { prepareException(e, authentication); throw e; } catch (AuthenticationException e) { lastException = e; } } if (result == null && parent != null) { // Allow the parent to try. try { #5.如果#1 没有验证通过,则使用父类型AuthenticationManager进行验证 result = parent.authenticate(authentication); } catch (ProviderNotFoundException e) { // ignore as we will throw below if no other exception occurred prior to // calling parent and the parent // may throw ProviderNotFound even though a provider in the child already // handled the request } catch (AuthenticationException e) { lastException = e; } } #6. 是否擦敏感信息 if (result != null) { if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) { // Authentication is complete. Remove credentials and other secret data 

转载于:https://www.cnblogs.com/free-wings/p/9308592.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/353313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javafx2_JavaFX 2 GameTutorial第5部分

javafx2介绍 这是与JavaFX 2 Game Tutorial相关的六部分系列的第五部分。 我知道自从我写关于游戏的博客以来已经有很长时间了&#xff0c;但希望您仍然与我在一起。 如果您想回顾一下&#xff0c;请阅读第1部分 &#xff0c; 第2 部分 &#xff0c; 第3 部分和第4 部分 &#…

Matlab各种最值问题

最大最小maxmin求多个最小值,并返回其位置[B,ind] sort(A);B(1:n);前n个最小值ind(1:n);前n个最小值的位置

最长递增子序列问题的求解

最长递增子序列问题是一个很基本、较常见的小问题&#xff0c;但这个问题的求解方法却并不那么显而易见&#xff0c;需要较深入的思考和较好的算法素养才能得出良好的算法。由于这个问题能运用学过的基本的算法分析和设计的方法与思想&#xff0c;能够锻炼设计较复杂算法的思维…

史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

转载请标明出处&#xff1a; 原文首发于&#xff1a;https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章&#xff0c;讲了服务的注册和发现。在微服务架构中&#xff0c;业务都会被拆分成一个独立的服务&#xff0c;服务与服…

忽略已检查的异常,所有出色的开发人员都在这样做–基于600,000个Java项目

Github和Sourceforge上超过600,000个Java项目中的异常处理概述 Java是使用检查异常的少数语言之一。 它们在编译时强制执行&#xff0c;并且需要某种处理。 但是……实践中会发生什么&#xff1f; 大多数开发人员实际上处理任何事情吗&#xff1f; 以及他们如何做到的&#xf…

Matlab积分

yint(fx,x,a,b);%函数表达式&#xff0c;自变量&#xff0c;下限&#xff0c;上限注&#xff1a;求得到y为sym类型&#xff0c;不是数值&#xff0c;可以使用subs转换如&#xff1a;subs(y,1); subs(y,a,1);%第一个给y赋1&#xff0c;第二个给y中的a赋1

最长有序子序列—动态规划算法

动态规划使用范围&#xff1a;&#xff08;http://baike.baidu.com/view/28146.htm&#xff09; 任何思想方法都有一定的局限性&#xff0c;超出了特定条件&#xff0c;它就失去了作用。同样&#xff0c;动态规划也并不是万能的。适用动态规划的问题必须满足最优化原理和无后效…

Codeforces 666E. Forensic Examination

Description 给出串 \(S\) ,和 \(m\) 个串 \(T_i\) ,每次询问 \((l,r,pl,pr)\) 表示 \(S[pl...pr]\) 在 \(T[l...r]\) 中哪一个出现次数最多,求出现次数和编号题面 Solution 基础题... 对于 \(S,T[l...r]\) 放在一起建广义后缀自动机 然后每次倍增到 S[pl,pr] ,然后查询子树内出…

Matlab控制精度

控制精度matlab控制运算精度用的是digits和vpa这两个函数digits用于规定运算精度&#xff0c;比如&#xff1a;digits(20);这个语句就规定了运算精度是20位有效数字。但并不是规定了就可以使用&#xff0c;因为实际编程中&#xff0c;我们可能有些运算需要控制精度&#xff0c;…

虚拟机环境下DPDK运行时的一些错误解决

在绑定网卡到DPDK模块时 报错 &#xff1a;is active. Not modifyingRouting table indicates that interface 0000:02:01.0 is active. Not modifying 解决方法&#xff1a; ifconfig <网卡名称> down 运行testpmd时无法分配内存&#xff1a;EAL: Error - exiting with …

ACM 网址和一些建议

USACO http://ace.delos.com/usacogate 美国著名在线题库&#xff0c;专门为信息学竞赛选手准备 TJU http://acm.tongji.edu.cn/ 同济大学在线题库&#xff0c;唯一的中文题库&#xff0c;适合NOIP选手 ZJU http://acm.zju.edu.cn/ 浙江大学在线题库 JLU http://acm.jlu…

使用Boxfuse轻松在云中运行Spring Boot应用程序

几天前&#xff0c;我开始构建一个将使用REST API检索和存储数据的iOS应用。 该REST API将是我也必须构建的服务器应用程序。 由于我熟悉Java和Spring &#xff0c;因此决定使用Spring Boot作为框架。 为了能够在我的iPhone上使用它&#xff0c;如果我可以在服务器而不是我自己…

Matlab各种求和

%普通求和sum(x);sum(x,1);sum(x,2); %累加求和cumsum(x);cumsum(x,1);cumsun(x,2); %累加求和的结果可以用diff实现逆运算

JavaScript内置对象Date----格式化时间

格式化时间日期: function getDate(dt) { //获取年份 var year dt.getFullYear(); //获取月份 var month dt.getMonth(); //获取日 var day dt.getDate(); //获取小时 var hour dt.getHours(); //获取分钟 var minute dt.getMinutes(); …

Matlab求欧式距离

pdist(x,‘euclidean’)1. %该函数还可以求其他距离&#xff0c;详见help2. %该函数得到的是一个向量&#xff0c;可以用squareform(Y)函数转换为对称矩阵形式。

川流不息

网站收藏&#xff1a; 1、站长网 网页教程与代码 2、博客制作 3、Java实例编程 贪吃蛇游戏开发视频教程

dao层通用封装_DAO层–救援通用

dao层通用封装泛型可以是使用编译时验证&#xff08;类型安全性&#xff09;的功能来创建可重用代码的强大工具。 不幸的是&#xff0c;我感到主流开发人员仍然对此感到恐惧。 但是&#xff0c;比喻海格的蜘蛛&#xff0c;我会说&#xff0c;泛型是被严重误解的生物……:-) 我…

(转) Linux 内核运行参数修改——sysctl命令

原文&#xff1a;https://blog.csdn.net/u012707739/article/details/78254241 sysctl命令被用于在内核运行时动态地修改内核的运行参数&#xff0c;可用的内核参数在目录/proc/sys中。它包含一些TCP/ip堆栈和虚拟内存系统的高级选项&#xff0c;用sysctl可以读取设置超过五百个…

Matlab求平均值函数mean

amean(A,1) %按列平均bmean(A,2) %按行平均cmean(A(:)) %全部平均

HDU 4514 湫湫系列故事——设计风景线

一次dfs判断有没有环 两次dfs求最长路 第一次记录最长路和次长路 第二次求出答案 #include <iostream>#include <string>#include <cstring>#include <algorithm>#include <cstdio>#define maxn 100010#pragma comment(linker, "/STACK:367…