• Spring Security 学习笔记 认识 Spring Security


    Spring Security

    基于 Spring 的权限管理框架,权限管理主要由身份认证Authentication和权限管理Authorization两部分组成。身份认证负责对用户进行身份鉴定,如验证用户名+口令的方式,鉴定的结果就是通过或者不通过。权限管理负责对用户(准确的说是通过了身份认证的用户)在系统内的权限提供支持,如管理员A允许访问文件,管理员B不允许导出数据。

    graph TD A(Spring Security)-->B1(Authentication) A-->B2(Authorization) B1-->C1(AuthenticationManager) B1-->C2(Authentication) B1-->C3(SecurityContextHolder) B2-->D1(AccessDeclsionManager) B2-->D2(AccessDecisionVoter) B2-->D3(ConfigAttribute)

    AuthenticationManager

    public interface AuthenticationManager
    
    Authentication authenticate(Authentication authentication) throws AuthencationException;
    

    AuthenticationManager 是一个接口,定义了authenticate方法,在系统中用来进行身份认证的方法:

    • 返回 Authencition 表示认证成功
    • 抛出 AuthenticationException 异常表示认证失败

    AuthenticationManager主要实现类是ProviderManager,ProviderManager中管理众多AuthenticationProvider。因此,在一次完整的认证流程中,实际允许存在多个(种)认证,例如表单认证、用户名密码认证、指纹认证,每一个认证都是一个AuthenticationProvider,都由ProviderManager进行统一管理。

    Authentication

    Authentication保存认证以及认证成功的信息,其接口定义为:

    public interface Authentication extends Principal, Serializable {
    Collection<? extends GrantedAuthority> getAuthorities();
    Object getCredentials();
    Object getdetails();
    Object getPrincipal();
    Boolean isAuthenticated();
    setAuthenticated();
    }
    
    • getAuthorities 获取权限
    • getCredentials 获取凭证,如密码
    • getdetails 获取用户详情
    • getPrincipal 获取用户身份信息,如用户名等
    • isAuthenticated 用户是否认证成功

    SecurityContextHolder

    Spring Security基于ThreadLocal管理,因此SecurityContextHolder是用来获取Authentication的工具或者说方法。用户登录成功后,Spring Security将用户信息保存到SecurityContextHolder,SecurityContextHolder通过ThreadLocal实现本次认证信息与当前请求线程绑定,不能被其它线程访问。当请求结束,SecurityContextHolder将用户信息放到Session保存,并清空SecurityContextHolder中的数据。下次请求来到时,SecurityContextHolder从Session中获取用户信息,请求结束时保存回Session并清空SecurityContextHolder。

    AccessDecisionManager

    AccessDecisionManager 访问决策管理器,用来决定此次访问是否被允许。需要AccessDecisionVoter配合。

    AccessDecisionvoter

    AccessDecisionVoter 访问决策投票器,检查用户是否具备应有的角色,进而投出赞成、返回或弃权票。AccessDecisionManager和AccessDecisionvoter都有众多实现类,AccessDecisionManager会挨个遍历AccessDecisionvoter,进而决定是否允许用户访问。

    ConfigAttribute

    ConfigAttribute 用来保存授权时的角色信息。

    bilibili - 编程不良人 - SpringSecurity最新实战

  • 相关阅读:
    C语言 assert 函数
    C语言 exit 函数
    C语言 main 函数参数 main(int argc, char *argv[])
    《一线架构师实践指南》读后感(一)
    机器学习十讲第八讲
    机器学习十讲第七讲
    Python通过IMAP实现邮箱客户端
    ABAP-PS操作
    ABAP-AES加解密
    ABAP-HmacSHA256
  • 原文地址:https://www.cnblogs.com/cinlap/p/16294715.html
Copyright © 2020-2023  润新知