Spring Security(二):认证架构

Spring Security 认证架构

  • Spring Security在认证处理流程中涉及到的类和过滤器
    在这里插入图片描述

认证处理流程描述

  • 第一步: 外部的http请求访问内部受保护的Resful API之前,需要经过一系列的安全过滤器。这些被Spring Security设定的过滤器,用于认证和授权的目的。当用户的认证请求从外部进入时,将经过这条过滤器链,基于认证机制和模型,找到相应的Authentication Filter。比如说:
    • HTTP Basic authentication request 经过“过滤链”,直到它到达BasicAuthenticationFilter 过滤器进行处理
    • HTTP Digest authentication request 经过“过滤链”,直到它到达DigestAuthenticationFilter 过滤器进行处理
    • login form authentication request 经过“过滤链”,直到它到达UsernamePasswordAuthenticationFilter过滤器进行处理
      - x509 authentication request 经过“过滤链”,直到它到达X509AuthenticationFilter 过滤器进行处理
  • 第二步: 一旦鉴权请求被相应的Authentication Filter接收,该过滤器将会从接收的请求中提取出用户名和密码,基于这些用户信息,创建一个UsernamePasswordAuthenticationToken 对象,注意,该类实现了 Authentication 接口
  • 第三步: UsernamePasswordAuthenticationToken 对象创建成功后,被用来作为 AuthenticationManager 中 authenticate() 方法的入参。
    • AuthenticationManager 仅仅是一个接口:
public interface AuthenticationManager{
  			Authentication authenticate(Authentication authentication) throws AuthenticationException;
}
  • 实际实现类是 ProviderManager 。该实现类包含了一系列配置的 AuthenticationProvider (s),这些provider受ProvideManager委托,用于用户请求的认证。
  • 第四步: 具体到实现细节, ProviderManager 会遍历每个AuthenticationProvider,根据传入的Authentication对象(例如: UsernamePasswordAuthenticationToken )尝试认证用户。
    • AuthenticationProvider 接口如下所示:
public interface AuthenticationProvider {
    Authentication authenticate(Authentication authentication) throws AuthenticationException;
    boolean supports(Class<?> authentication);
}
  • 这里是一些Spring Security框架提供的AuthenticationProvider:
    • CasAuthenticationProvider
    • JaasAuthenticationProvider
    • DaoAuthenticationProvider
    • OpenIDAuthenticationProvider
    • RememberMeAuthenticationProvider
    • LdapAuthenticationProvider
  • 第五步: 有些AuthenticationProvider可能会使用 UserDetailsService ,用于获取用户的详细信息。
    • 比如说 DaoAuthenticationProvider 可能需要根据传入的用户名从数据库中获取该用户的详细信息。
public interface UserDetailsService{
                 UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}
  • 第六步:
    • UserDetailsService 返回 UserDetails 。而 User 是 UserDetails 的具体实现类,当然用户也可以自行实现 UserDetails 接口。
  • 第七步:
    • 同第六步
  • 第八步: 如果用户认证成功,则返回Authentication对象,相比于传入的未认证的对象,这个鉴权后的的对象更“丰满”,包含了用户的角色信息
    在这里插入图片描述
    • 从上图可以看出,认证成功的Authentication对象(Fully populated Authentication Object)包含:
      • authenticated- true
      • grant authorities list :关联的角色(角色和权限挂钩)
      • user credentials:用户凭证(仅仅包含用户名)
    • 如果鉴权未通过,则抛出异常 AuthenticationException 。该异常属于运行时异常,不期望用户通过try/catch去处理,spring security提供了一种通用的方式。即通过AuthenticationEntryPoint 处理:
	@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(unauthorizedHandler);
    }
  • 第九步: Authentication is done! 认证成功后,AuthenticationManager返回包含完整信息的鉴权对象给相关的Authentication Filter。
  • 第十步: 将返回的认证对象保存到SecurityContext,用于后续过滤器的使用。比如Authorization Filters
SecurityContextHolder.getContext().setAuthentication(authentication);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值