当前位置: 首页 > news >正文

主题之家wordpress宁波seo外包公司

主题之家wordpress,宁波seo外包公司,企业管理培训课程排行榜,软件制作视频这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解,知道它是如何进行用户认证的 考虑一个大家比较熟悉的标准认证过程: 1.用户使用username和password登录 2.系统验证这个password对于该username是正确的 3.假设第二步验证成功&am…

这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解,知道它是如何进行用户认证的

考虑一个大家比较熟悉的标准认证过程:

1.用户使用username和password登录

2.系统验证这个password对于该username是正确的

3.假设第二步验证成功,获取该用户的上下文信息(如他的角色列表)

4.围绕该用户建立安全上下文(security context)

5.用户可能继续进行的一些操作被一个验证控制机制潜在的管理,这个验证机制会根据当前用户的安全上下文来验证权限。

 

认证过程就是又前三项构成的。在Spring Security中是这样处理这三部分的:

1.username和password被获得后封装到一个UsernamePasswordAuthenticationToken(Authentication接口的实例)的实例中

2.这个token被传递给AuthenticationManager进行验证

3.成功认证后AuthenticationManager将返回一个得到完整填充的Authentication实例

4.通过调用SecurityContextHolder.getContext().setAuthentication(...),参数传递authentication对象,来建立安全上下文(security context)

 

可以从一个示例代码中观察整个过程(完整代码参考Spring-Security文档9.3.1节):

复制代码
 1 public class AuthenticationExample {2     3     private static AuthenticationManager am = new SampleAuthenticationManager();4 5     public static void main(String[] args) throws Exception {6         7         String name = "";8         String password = "";9         try {
10             // request就是第一步,使用name和password封装成为的token
11             Authentication request = new UsernamePasswordAuthenticationToken(name, password);
12             // 将token传递给Authentication进行验证
13             Authentication result = am.authenticate(request);
14             SecurityContextHolder.getContext().setAuthentication(result);
15             break;
16         } catch (AuthenticationException e) {
17             System.out.println("认证失败:" + e.getMessage());
18         }
19         System.out.println("认证成功,Security context 包含:" + SecurityContextHolder.getContext().getAuthentication());
20     }
21 }
22 
23 // 自定义验证方法
24 class SimpleAuthenticationManager implements AuthenticationManager {
25     static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
26 
27     // 构建一个角色列表
28     static {
29         AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
30     }
31 
32     // 验证方法
33     public Authentication authenticate(Authentication auth) throws AuthenticationException {
34         // 这里我们自定义了验证通过条件:username与password相同就可以通过认证
35         if (auth.getName().equals(auth.getCredentials())) {
36             return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
37         }
38         // 没有通过认证则抛出密码错误异常
39         throw new BadCredentialsException("Bad Credentials");
40     }
41 }
复制代码

通常不需要像上边这样写代码,这些过程都是内部自动进行的。当SecurityContextHolder包含一个完整填充的Authentication对象,用户就是验证通过了。

 

Web Application

考虑一个典型的Web应用认证过程:

1.访问首页,随便点击一个链接

2.发送一个请求到服务器,服务器判断你是否在访问一个收到保护的资源

3.此时你还没有进行认证,服务器会返回一个响应告诉你必须先通过认证。这个响应可以是一个HTTP响应码或者是重定向到指定的web页面

4.根据认证机制,你的浏览器可能会重定向到一个登录页面,或者通过某种方式恢复你的身份(通过一个基础的认证对话框,cookie,X.509证明等)

5.浏览器回应服务器。这可以是一个HTTP POST请求,包含你所填写的表单信息,也可以是一个HTTP请求头,包含你的认证详情

6.接下来服务器会判定提交的凭证是否通过认证。如果认证通过,那么继续下一步。如果没有通过认证,那么重新进行上边的步骤

7.你在认证之前,原始的请求(即触发认证的请求)将会重新发起。

 

Spring Security已经实现了上述的大多数过程。主要有ExceptionTranslationFilter,AuthenticationEntryPoint和一个认证机制,负责调用上面讨论过的AuthenticationManager。

ExceptioTranslationFilter

ExceptionTranslationFilter是用来检测Spring Security抛出的任何异常的过滤器。

 

AuthenticationProvider和UserDetails

There is often some confusion about UserDetailsService. It is purely a DAO for user data and performs no other function other than to supply that data to other components within the framework. In particular, it does not authenticate the user, which is done by the AuthenticationManager. In many cases it makes more sense to implement AuthenticationProvider directly if you require a custom authentication process.

AuthenticationManager

用来处理一个认证请求。只有一个authentication(Authentication authentication)函数。

尝试去认证传入的Authentication对象,如果认证成功,返回一个完整填充的Authentication对象(包括授予的权限)。

一个AuthenticationManager必须处理以下异常:

  • DisabledException:当一个账户被禁用且AuthenticationManager可以检测出来这个状态,要抛出该异常
  • LockedException:当一个账户被锁且AuthenticationManager可以检测这个状态,要抛出该异常
  • BadCredentialsException:当账户认证失败,必须抛出该异常。(一个AuthenticationManager必须检测这个状态)

这些异常应该按照顺序抛出,(比如如果一个账户被锁定,那么不进行账户认证)。

AuthenticationProvider

用来处理一个指定的认证。有一个authenticate(Authentication authentication)函数和一个supports(Class<?> authentication)函数。

其中authenticate函数的用法与AuthenticationManager的authenticate一样。

supports函数用来指明该Provider是否适用于该类型的认证,如果不合适,则寻找另一个Provider进行验证处理。

ProviderManager

通过AuthenticationProviders迭代认证请求。

AuthenticationProviders通常按照顺序尝试,知道返回一个不为null的响应。非空响应代表provider可以提供认证并且不会继续请求下一个provider。如果后边的provider成功进行了验证,那么前边provider抛出的异常将被忽略。

转载于:https://www.cnblogs.com/renjiaqi/p/11186172.html

http://www.lbrq.cn/news/2434429.html

相关文章:

  • 政府网站建设的重要性网络营销最基本的应用方式是什么
  • 新手学习网站建设搜狗网址导航
  • 做搜狗网站快速排名软百度推广seo怎么学
  • Wordpress多站点共享用户百度收录工具
  • 做网站超链接网络营销实施方案
  • 市委网站建设营销计划
  • 贵阳网站建设方案报价网站推广平台有哪些
  • 电子商务网站设计原理知识点手机app推广平台
  • 一个网站怎么推广seo公司推广
  • 移动端网站 用什么软件做线上销售方案
  • 大丰做网站价格怎么在百度投放广告
  • 网络工程师中级职称考试内容厦门百度seo公司
  • 如何写网站建设方案百度霸屏全网推广
  • 威县企业做网站关键词整站优化
  • 如何做网站压力测试排名网站
  • 在日本做色情网站品牌营销策划方案
  • 怎么做老虎机网站的宁波seo网络推广产品服务
  • 购物平台排行榜2020seo搜索引擎优化关键词
  • 做网站卖产品要注册公司吗抖音seo源码搭建
  • wordpress配置数据库文件夹电脑优化是什么意思
  • 域名和网站的区别北京昨晚出什么大事
  • 为公益组织做网站重庆高端seo
  • wordpress在文章中加背景云seo
  • 日本做暧小视频在线观看网站网站seo优化皆宣徐州百都网络不错
  • 做网站采集内容广东疫情最新数据
  • 如何判断一个网站的关键词是否难做营销软文范例
  • 做代加工的网站发布成人培训机构
  • wordpress缓存首页不正常seo中心
  • docker做网站百度爱采购平台官网
  • 做艺术品的网站烘焙甜点培训学校
  • 【已解决】YOLO11模型转wts时报错:PytorchStreamReader failed reading zip archive
  • Packmol聚合物通道模型建模方法
  • LWIP学习记录2——MAC内核
  • GoLand 部署第一个项目
  • python学习-读取csv大文件
  • 在 Ubuntu 22.04 上安装并优化 Nginx nginx入门操作 稍难,需要有一定理论 多理解 多实践