Loading

SpringSecurity 学习笔记

SpringSecurity

  • Authentication: 认证, 验证用户的身份并将用户身份信息 Authentication 对象放入 SecurityContext
  • Authorization: 授权, 从 SecurityContext 取出 Authentication 对象判断是否拥有指定权限

授权的两种形式:

  1. 权限 (Authority)
  2. 角色 (Role)

在 SpringSecurity 中将角色定义为以 ROLE_ 开头的权限

请求流程

SpringSecurity

自定义配置

配置对象

通过自定义 builder 中的属性来修改目标构建对象的属性

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    super.configure(auth);
}
// 设置 ProviderManager (AuthenticationManager 的一个实现类) 的配置内容

@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);
}
// 设置 FilterChainProxy 的配置内容

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(auth);
}
// 设置 DefaultSecurityFilterChain 的配置内容, 为 http 请求配置安全规则

放行资源

  1. 静态资源放行

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/assets");
    }
    
    // 一般只用来放行静态资源, 无法 SecurityContextHolder 获取到 SecurityContext 上下文对象
    // 通过这种方法放行的请求相当于在 FilterChainProxy 的 filterChains 属性上添加了一条过滤器链, 但是在这条过滤器链上没有任何 SecurityFilter, 也就是说这条过滤器链会被直接放行
    
  2. 权限放行

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/login").permitAll();
    }
    
    // 放行无需用户登录的请求, 同理可以为请求指定权限验证, AccessDecisionManager 根据设置的规则来进行权限鉴定
    

自定义登陆器

  1. 使用 Controller 处理登录请求, 并在 SpringSecurity 中放行该请求
  2. 模仿 UsernamePasswordAuthenticationFilter 添加自定义的登陆器注册到 FilterChainProxy

配置 token 登录

  1. 自定义过滤器放入 SecurityFilterChain 中, 手动构造 Authentication 对象放入上下文环境中
  2. 以 UsernamePasswordAuthenticationFilter 为参考继承 AbstractAuthenticationProcessingFilter 或 参考 BasicAuthenticationFilter, 自定义一系列的登录逻辑完成自动登录 (如: 在自定义的 Filter 中将 token 作为用户凭证构造 UsernamePasswordAuthenticationToken 对象, 并提供一个从缓存中查询用户的 UserDetailsService 实现类来进行 token 有效性的验证, 从而完成 token 自动登录)
  3. 自定义 SecurityContextRepository, 来提供上下文环境对象 SecurityContextImpl

Json 响应

  1. 自定义 AuthenticationEntryPoint 实现类来处理 401 异常
  2. 自定义 AccessDeniedHandler 实现类来处理 403 异常
  3. 配置 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 来关闭 session 中存储 SecurityContext 的策略
  4. 配置 http.formLogin().disable(); 来关闭默认提供的登录页
  5. 配置 http.logout().disable(); 来关闭默认提供的退出页面
  6. 配置 http.csrf().disable(); 来关闭 CsrfFilter, 或者自定义 CsrfTokenRepository 存储策略

参考文章: http://itboyhub.com/2021/01/26/spring-security-guide/

posted @ 2021-11-04 09:09  xtyuns  阅读(154)  评论(0编辑  收藏  举报