Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

Author:Hedon

Github:spring-security-oauth2.0

前篇:

Spring Cloud 微服务安全 | (一) API 安全

Spring Cloud 微服务安全 | (二) 网关安全

Spring Cloud 微服务安全 | (三) 登录功能


1. 密码模式

应用于 APP,不适合在 Web(不够安全)。

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

1. 请求令牌时需要传递的参数:

  • client_id
  • client_secret
  • grant_type=password
  • username
  • password

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式


2. 授权码模式

适合在 Web。

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

1. 申请授权码需要的参数列表:

  • client_id:客户端准入标识
  • response_type:授权码模式默认为 code
  • scope:客户端访问s
  • redirect_uri:跳转uri

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

2. 申请令牌需要的参数列表

  • client_id:客户端准入标识
  • client_secret:客户端**
  • grant_type:授权类型,授权码模式默认为 authorization_code
  • code:授权码(一次失效)
  • redirect_uri:跳转uri

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式


3. 简化模式

适用于没有前端服务器的情景,基本不用。

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

1. 请求授权时需要传递的参数:

  • client_id
  • response_type=token
  • scope
  • redirect_uir

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

2. 返回令牌

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式


4. 客户端模式

跟用户没有关系,相当于没授权。基本不会用。

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

1. 申请令牌时需要提交的参数:

  • client_id
  • client_secret
  • grant_type=client_credentials

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

测试令牌:

Spring Cloud 微服务安全 | (四) OAuth2.0 的四种授权模式

客户端没有从输入账号密码,所以不能从数据库中读取用户权限,所以没有权限信息。

客户端模式不会走public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException