OIDC 单点登录
平台以 OpenID Connect 1.0 为主要的身份认证协议,基于 OAuth 2.0 授权码模式,支持 PKCE、nonce 与 refresh_token。
一、发现文档
GET /.well-known/openid-configuration
返回关键端点:
{
"issuer": "http://sso.ingu.cn",
"authorization_endpoint": "http://sso.ingu.cn/oauth/authorize",
"token_endpoint": "http://sso.ingu.cn/oauth/token",
"userinfo_endpoint": "http://sso.ingu.cn/oauth/userinfo",
"jwks_uri": "http://sso.ingu.cn/oauth/jwks",
"end_session_endpoint": "http://sso.ingu.cn/oauth/logout",
"revocation_endpoint": "http://sso.ingu.cn/oauth/revoke",
"introspection_endpoint": "http://sso.ingu.cn/oauth/introspect",
"id_token_signing_alg_values_supported": ["RS256"],
"scopes_supported": ["openid", "profile", "email", "phone", "org", "roles", "offline_access", "points:read", "points:write", "usage:write"]
}
直接使用
openid-client、oidc-client-ts、spring-security-oauth2、keycloak等标准库,只需填入 Issuer 即可自动发现全部配置。
二、授权端点
GET /oauth/authorize
| 参数 | 必填 | 说明 |
|---|---|---|
response_type |
是 | 固定 code |
client_id |
是 | 应用 client_id |
redirect_uri |
是 | 必须与后台登记完全一致 |
scope |
是 | 空格分隔,至少包含 openid |
state |
建议 | 防 CSRF,回调原样返回 |
nonce |
建议 | 防重放,会写入 id_token |
code_challenge |
视应用 | PKCE:BASE64URL(SHA256(verifier)) |
code_challenge_method |
视应用 | S256(推荐)或 plain |
prompt |
否 | none 表示静默(已登录则不展示页面) |
行为说明
- 未登录 → 302 跳转平台登录页,登录成功后自动回到授权请求
- 已登录但未授权(或 scope 扩大)→ 展示授权确认页
- 已授权且 scope 覆盖 → 直接 302 回调
redirect_uri?code=...&state=... - 用户拒绝 → 302 回调
redirect_uri?error=access_denied&state=...
授权码有效期 300 秒,且只能使用一次(使用后立即失效)。
三、令牌端点
POST /oauth/token
Content-Type: application/x-www-form-urlencoded
1) authorization_code
curl -X POST http://sso.ingu.cn/oauth/token \
-d grant_type=authorization_code \
-d client_id=ingu_xxxx \
-d client_secret=sk_xxxx \
-d code=CODE \
-d redirect_uri=https://your-app.com/callback \
-d code_verifier=PKCE_VERIFIER # 使用 PKCE 时必填
2) refresh_token(轮换)
curl -X POST http://sso.ingu.cn/oauth/token \
-d grant_type=refresh_token \
-d client_id=ingu_xxxx \
-d client_secret=sk_xxxx \
-d refresh_token=REFRESH_TOKEN
每次刷新都会签发新 refresh_token,旧的立即作废。请务必覆盖保存,否则下次刷新会失败并需要用户重新登录。
3) client_credentials(服务间调用)
curl -X POST http://sso.ingu.cn/oauth/token \
-d grant_type=client_credentials \
-d client_id=ingu_xxxx \
-d client_secret=sk_xxxx \
-d scope="points:read points:write usage:write"
返回的令牌不包含用户身份,调用积分接口时需要显式传 user_id。
响应
{
"access_token": "3xJ9...kQ",
"token_type": "Bearer",
"expires_in": 7200,
"scope": "openid profile email",
"id_token": "eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOi...",
"refresh_token": "9aF2...Zx"
}
四、id_token 声明
| 声明 | 说明 |
|---|---|
iss |
固定 http://sso.ingu.cn |
sub |
用户唯一标识(UUID,永不复用) |
aud / azp |
你的 client_id |
exp / iat / auth_time |
时间戳 |
nonce |
与请求时一致 |
name / nickname / preferred_username |
需 profile |
email / email_verified |
需 email |
phone_number / phone_number_verified |
需 phone |
org |
需 org,含 id/name/code/path |
roles / is_admin |
需 roles |
验签步骤(RS256)
- 请求
http://sso.ingu.cn/oauth/jwks取公钥(按kid匹配) - 使用 RS256 验签
- 校验
iss、aud、exp、nonce - 取
sub建立本地会话
五、UserInfo
GET /oauth/userinfo
Authorization: Bearer ACCESS_TOKEN
{
"sub": "0f8a1c2e-...",
"name": "张一鸣",
"email": "zhangym@ingu.cn",
"org": { "id": 2, "name": "AI 绘图事业部", "code": "AI-IMAGE" },
"roles": ["developer"],
"points_balance": 1234.5678
}
points_balance为平台扩展字段,方便前端直接展示积分。
六、登出
GET /oauth/logout?post_logout_redirect_uri=https://your-app.com&state=xyz
平台会清除 SSO 会话,然后跳回你登记的登出地址(必须在应用的「登出后跳转地址」中登记)。也可调用本地登出后直接跳转该地址实现「单点登出」。
七、令牌撤销与自省
# 撤销(RFC 7009)
curl -X POST http://sso.ingu.cn/oauth/revoke -d token=ACCESS_OR_REFRESH_TOKEN
# 自省(RFC 7662)
curl -X POST http://sso.ingu.cn/oauth/introspect -d token=ACCESS_TOKEN
自省响应示例:
{ "active": true, "scope": "openid profile", "client_id": "ingu_xxx", "username": "zhangym@ingu.cn", "exp": 1764030000, "sub": "0f8a1c2e-..." }
八、常见错误
| error | 含义 | 排查 |
|---|---|---|
invalid_client |
client_id 不存在或已停用 | 检查应用状态 |
invalid_grant |
code 过期 / 已使用 / redirect_uri 不一致 / PKCE 校验失败 | 重新发起授权 |
invalid_scope |
申请了未授权的 scope | 后台补勾选 scope |
unsupported_grant_type |
grant_type 不支持 | 检查拼写 |
access_denied |
用户在确认页点拒绝 | 正常业务分支 |