sa-token-oauth2
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSa-Token OAuth2.0 服务端
Sa-Token OAuth2.0 Server
基于 全部 17 个文档。基础认证使用 技能。
sa-token-doc/oauth2/sa-tokenBased on all 17 documents in . Basic authentication uses the skill.
sa-token-doc/oauth2/sa-tokenCapability Boundaries
Capability Boundaries
✅ Strong Suits
✅ Core Capabilities
- 授权码模式(Authorization Code) — 有后端的Web应用,最常用,安全性最高
- 隐式模式(Implicit) — 纯前端应用
- 密码模式(Password) — 信任的客户端(自家APP)
- 客户端凭证模式(Client Credentials) — 服务间调用,无需用户授权
- Scope权限控制 — 自定义Scope+Scope等级划分
- OIDC协议 — OpenID Connect身份层
- OpenId/UnionId — 用户唯一标识生成与管理
- 自定义grant_type — 扩展授权类型
- 自定义登录/授权页 — 定制化前端界面
- 注解校验Access-Token — @SaCheckAccessToken、@SaCheckClientToken
- Authorization Code — Web applications with a backend; the most commonly used and safest mode
- Implicit — Pure front-end applications
- Password — Trusted clients, such as first-party apps
- Client Credentials — Service-to-service calls without user authorization
- Scope Permission Control — Custom Scope definitions and tiered Scope levels
- OIDC Protocol — An OpenID Connect identity layer
- OpenId/UnionId — Generation and management of unique user identifiers
- Custom grant_type — Extension of grant types
- Custom Login/Authorization Pages — Customized front-end interfaces
- Annotation-Based Access-Token Validation — @SaCheckAccessToken and @SaCheckClientToken
⚠️ Requirements
⚠️ Requirements
- 需要JDK 8+环境
- OAuth2-Server需要可被Client访问的网络
- 需实现 SaOAuth2DataLoader 接口提供数据
- A JDK 8+ environment is required
- The OAuth2-Server must be accessible over the network by Clients
- The SaOAuth2DataLoader interface must be implemented to provide data
❌ Out of Scope
❌ Out of Scope
- 基础登录/权限认证 → sa-token
- SSO单点登录 → sa-token-sso
- 微服务鉴权 → sa-token-micro
- Basic login/permission authentication → sa-token
- SSO authentication → sa-token-sso
- Microservice authentication → sa-token-micro
四种授权模式
Four Grant Modes
| 模式 | 适用场景 | 说明 |
|---|---|---|
| 授权码(Authorization Code) | 有后端的Web应用 | 最常用,安全性最高 |
| 隐式(Implicit) | 纯前端应用 | 已不推荐使用 |
| 密码(Password) | 信任的客户端 | 自家APP使用 |
| 客户端凭证(Client Credentials) | 服务间调用 | 无需用户授权 |
| Mode | Use Case | Description |
|---|---|---|
| Authorization Code | Web applications with a backend | Most commonly used and safest |
| Implicit | Pure front-end applications | No longer recommended |
| Password | Trusted clients | For first-party apps |
| Client Credentials | Service-to-service calls | No user authorization required |
什么时候用哪种模式
Choosing a Grant Mode
- 开发开放平台,给第三方开发者使用 → 授权码模式,标准OAuth2流程
- 自己的前后端分离应用 → 密码模式,简单直接
- 微服务之间调用API → 客户端凭证模式,无需用户参与
- 纯前端无后端应用(已不推荐) → 隐式模式
- Developing an open platform for third-party developers → Authorization Code mode, following the standard OAuth2 flow
- Your own separated front-end and back-end application → Password mode, which is simple and direct
- Calling APIs between microservices → Client Credentials mode, with no user participation required
- Pure front-end application without a backend (no longer recommended) → Implicit mode
工作流
Workflow
Step 1. 引入依赖 — 添加 依赖
Step 2. 实现DataLoader — 加载客户端和Scope数据
Step 3. 配置Server端点 — 注册 authorize/token/validate/refresh 接口
Step 4. 配置OAuth2参数 — 设置Token有效期、授权模式等
Step 5. 测试授权流程 — 使用授权码模式验证端到端流程
Step 6. 上线前安全加固 — 开启域名校验、配置回调白名单
sa-token-oauth2SaOAuth2DataLoaderImplStep 1. Add the dependency — Add the dependency
Step 2. Implement the DataLoader — Use to load client and Scope data
Step 3. Configure server endpoints — Register the authorize/token/validate/refresh endpoints
Step 4. Configure OAuth2 parameters — Set token validity periods, grant modes, and other options
Step 5. Test the authorization flow — Use Authorization Code mode to verify the end-to-end flow
Step 6. Harden security before deployment — Enable domain validation and configure a callback allowlist
sa-token-oauth2SaOAuth2DataLoaderImpl参考文档
Reference Documentation
| 主题 | 文件 | 来源 |
|---|---|---|
| Server搭建+DataLoader | references/oauth2-server.md | oauth2-server、oauth2-apidoc |
| DataLoader+域名校验+自定义 | references/oauth2-data-loader.md | data-loader、check-domain |
| Scope权限自定义与分级 | references/oauth2-scope.md | custom-scope、scope-level |
| Topic | File | Source |
|---|---|---|
| Server setup + DataLoader | references/oauth2-server.md | oauth2-server, oauth2-apidoc |
| DataLoader + domain validation + customization | references/oauth2-data-loader.md | data-loader, check-domain |
| Custom Scope permissions and levels | references/oauth2-scope.md | custom-scope, scope-level |
FAQ
FAQ
Q: OAuth2和SSO有什么区别?
A: OAuth2解决授权问题(第三方应用访问用户数据),SSO解决认证问题(统一登录)。需要第三方授权选OAuth2,仅需统一登录选SSO。也可两者结合使用。
Q: 四种授权模式选哪种?
A: 有后端的Web应用选授权码模式(最安全)。自家APP选密码模式。服务间调用选客户端凭证模式。隐式模式已不推荐。
Q: 如何自定义Scope?
A: 实现 方法,返回 对象,可设置scope名称、描述、是否默认选中。
SaOAuth2DataLoader.getScope()SaScopeObjectQ: 如何保证OAuth2服务安全?
A: 1) 配置 校验回调域名;2) 配置 白名单;3) 生产环境关闭 ;4) 使用HTTPS。
is-check-domain: trueallow-urlisAutoModeQ: 支持刷新Token吗?
A: 支持。 默认30天,调用 接口刷新。
refresh-token-timeout/oauth2/refreshQ: 前后端分离怎么对接OAuth2?
A: 前端通过URL参数接收code,调用后端 接口换取AccessToken,保存到本地存储。
/oauth2/tokenQ: What is the difference between OAuth2 and SSO?
A: OAuth2 solves authorization issues by allowing third-party applications to access user data, while SSO solves authentication issues through unified login. Choose OAuth2 when third-party authorization is required, and SSO when only unified login is needed. They can also be used together.
Q: Which of the four grant modes should I use?
A: Use Authorization Code mode for web applications with a backend because it is the safest. Use Password mode for your own app. Use Client Credentials mode for service-to-service calls. Implicit mode is no longer recommended.
Q: How do I customize Scope?
A: Implement the method and return a . You can configure the Scope name, description, and whether it is selected by default.
SaOAuth2DataLoader.getScope()SaScopeObjectQ: How do I secure the OAuth2 service?
A: 1) Set to validate callback domains; 2) configure an allowlist; 3) disable in production; 4) use HTTPS.
is-check-domain: trueallow-urlisAutoModeQ: Is token refresh supported?
A: Yes. defaults to 30 days. Call the endpoint to refresh the token.
refresh-token-timeout/oauth2/refreshQ: How do I integrate OAuth2 with a separated front end and back end?
A: The front end receives the code through URL parameters and calls the back-end endpoint to exchange it for an AccessToken, which is then stored in local storage.
/oauth2/tokenGotchas
Gotchas
- 必须实现 SaOAuth2DataLoader — Server启动后不会自动加载数据,需要自行实现接口
- allow-url 必须配置 — 不配置回调URL白名单会导致授权失败
- 生产环境关闭 isAutoMode — 自动确认授权模式仅用于开发调试
- AccessToken有效期默认2小时 — 可通过 配置
access-token-timeout - Scope需要在 DataLoader 和 ClientModel 两端配置 — 只配一端不会生效
- 隐式模式不推荐生产使用 — AccessToken直接暴露在URL中,有安全风险
- OIDC需要额外配置 iss 和 idTokenTimeout — 不是默认开启就能用
- 域名校验不通过会返回错误 — 配置 时要包含所有合法客户端
allow-domains
- SaOAuth2DataLoader must be implemented — The Server does not load data automatically after startup, so the interface must be implemented manually
- allow-url must be configured — Authorization will fail if the callback URL allowlist is not configured
- Disable isAutoMode in production — Automatic authorization confirmation is intended only for development and debugging
- The AccessToken validity period defaults to 2 hours — It can be configured through
access-token-timeout - Scope must be configured on both the DataLoader and ClientModel sides — Configuring only one side will not take effect
- Implicit mode is not recommended for production — The AccessToken is exposed directly in the URL, creating a security risk
- OIDC requires additional configuration of iss and idTokenTimeout — Merely enabling it by default is not sufficient
- A domain validation failure returns an error — When configuring , include every legitimate client
allow-domains
Data Privacy
Data Privacy
本技能不收集、存储或传输任何用户数据。所有代码示例仅供本地开发参考。
This skill does not collect, store, or transmit any user data. All code examples are provided solely for local development reference.