Ezio's Blog
Posts Categories Tags Music Mood About
Ezio's Blog· Light
☰ Menu
Posts Categories Tags Music Mood About
Expand all Back to top Go to bottom

Spring Security

Author: Ezio Date: September 19, 2022  13:54:06 Category: Spring

Spring Security 是 Spring 项目中的一个安全模块,在 Spring boot 中已经默认集成和开启了 Spring Security。

Spring Security 核心组件

spring security 核心组件有:Userdetails 、Authentication,UserDetailsService、AuthenticationProvider、AuthenticationManager。

Authentication

在 Spring Security 中 Authentication 用来表示当前用户是谁,一般来讲你可以理解为 authentication 就是一组用户名密码信息。Authentication 也是一个接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface Authentication extends Principal, Serializable {

Collection<? extends GrantedAuthority> getAuthorities();

Object getCredentials();

Object getDetails();

Object getPrincipal();

boolean isAuthenticated();

void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
}

这几个方法作用如下:

  • getAuthorities(): 获取用户权限,一般情况下获取到的是用户的角色信息。
  • getCredentials():获取证明用户认证的信息,通常情况下获取到的是密码等信息。
  • getDetails(): 获取用户的额外信息,(这部分信息可以是我们的用户表中的信息)
  • getPrincipal(): 获取用户身份信息,在未认证的情况下获取到的是用户名,在已认证的情况下获取到的是 UserDetails (UserDetails也是一个接口,里边的方法有getUsername,getPassword等)。
  • isAuthenticated():获取当前 Authentication 是否已认证。
  • setAuthenticated():设置当前 Authentication 是否已认证(true or false)。

UserDetails

UserDetails,用户详情。其存储的就是用户信息,其定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface UserDetails extends Serializable {

Collection<? extends GrantedAuthority> getAuthorities();

String getPassword();

String getUsername();

boolean isAccountNonExpired();

boolean isAccountNonLocked();

boolean isCredentialsNonExpired();

boolean isEnabled();
}

其方法含义如下:

  • getAuthorites():获取用户权限,本质上是用户的角色信息。
  • getPassword():获取密码。
  • getUserName():获取用户名。
  • isAccountNonExpired():账户是否过期。
  • isAccountNonLocked():账户是否被锁定。
  • isCredentialsNonExpired(): 密码是否过期。
  • isEnabled(): 账户是否可用。

UserDetailsService

UserDetailsService 是一个接口,且只有一个方法 loadUserByUsername,它可以用来获取 UserDetails。

1
2
3
4
public interface UserDetailsService {

UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}

通常在 Spring Security 应用中,我们会自定义一个 CustomUserDetailsService 来实现 UserDetailsService 接口,并实现 loadUserByUsername(); 方法。一般通过查询数据库(或者是缓存、或者是其他的存储形式)来获取用户信息,然后组装成一个 UserDetails,(通常是一个 org.springframework.security.core.userdetails.User,它继承自 UserDetails )并返回。

在实现 loadUserByUsername 方法的时候,如果通过查数据库没有查询到相关记录,需要抛出一个异常来告诉 Spring Security 来“善后”。这个异常是 org.springframework.security.core.userdetails.UsernameNotFoundException。

AuthenticationManager

认证是由 AuthenticationManager 来管理的,但是真正进行认证的是 AuthenticationManager 中定义的 AuthenticationProvider。AuthenticationManager 中可以定义有多个 AuthenticationProvider。

AuthenticationManager 是一个接口,其定义如下:

1
2
3
4
5
public interface AuthenticationManager {

Authentication authenticate(Authentication authentication)
throws AuthenticationException;
}

AuthenticationManager 的作用就是校验 Authentication,如果验证失败会抛出 AuthenticationException 异常。AuthenticationException 是一个抽象类,因此代码逻辑并不能实例化一个 AuthenticationException 异常并抛出,实际上抛出的异常通常是其实现类,如 DisabledException,LockedException,BadCredentialsException 等。BadCredentialsException 可能会比较常见,即密码错误的时候。

AuthenticationProvider

身份认证提供者,负责真正的认证处理。它是一个接口,在未指定 provider 时,Spring Security 默认使用 DaoAuthenticationProvider 来进行身份认证,DaoAuthenticationProvider 在认证时通过 UserDetailsService 获取 UserDetails ,其中包括用户名、密码和所拥有的权限等。

如果想自定义认证逻辑,可以通过继承 AuthenticationProvider 接口,并通过 AuthenticationManager 配置自定义 provider。

1
2
3
4
5
6
7
public interface AuthenticationProvider {

Authentication authenticate(Authentication authentication)
throws AuthenticationException;

boolean supports(Class<?> authentication);
}
  • authenticate() 表示认证的动作。
  • supports() 表示所支持的 Authentication 类型。Authentication 包含很多子类,如果 AbstractAuthenticationToken 。

Author: Ezio

Permalink: https://ezioy.cn/2022/09/19/Spring-Security/

License: Copyright (c) 2019 CC-BY-NC-4.0 LICENSE

Slogan: Nothing is true,Everything is permitted

Tag(s): # Spring # Spring Security
back · home
MySQL数据库开发规范 Kafka入门
Ezio © 2019 - 2026 | Powered by Hexo & Chic | 访客数量:   浏览次数: | 渝公网安备50011302222043 | 渝ICP备2023013933号-1