OIDC Integration
Use this skill to design or implement authentication flows that rely on an OpenID Connect provider.
Objective
Help the user integrate OIDC in a way that matches their architecture, avoids unsafe defaults, and produces code that fits the current stack instead of dumping generic samples.
Working Style
Start by identifying the actual integration shape before writing code.
Check these points first:
- - Which side owns auth state: SPA only, backend session, or BFF.
- Which provider is involved: Keycloak, Auth0, IdentityServer, Authing, Azure AD, or another OIDC-compliant IdP.
- Whether there is one provider or multiple providers.
- Whether the client is public or confidential.
- Whether refresh tokens are required.
- Whether the user needs login only, login plus logout, route protection, token validation, or full end-to-end flow.
- Whether the codebase already uses auth libraries that should be extended rather than replaced.
If the user only needs one layer, stay scoped to that layer. Do not generate both frontend and backend integration unless the task actually spans both.
Security Defaults
Apply these defaults unless the user explicitly needs something different:
- - Prefer Authorization Code Flow.
- For browser-based public clients, use PKCE.
- Prefer provider metadata discovery from
/.well-known/openid-configuration over hardcoding endpoints. - Prefer server-managed sessions or httpOnly cookies over storing long-lived tokens in
localStorage. - If a pure SPA must hold tokens in browser storage, call out the tradeoff and keep token lifetime short.
- Request
offline_access only when refresh tokens are actually needed. - Validate
iss, aud, signature, expiry, and key rotation behavior. - Treat logout as two separate concerns: local app logout and provider logout.
Do not normalize insecure shortcuts as the default recommendation.
Preferred Implementation Path
Prefer framework-native or well-supported libraries before hand-rolled auth code.
If the user asks for concrete implementation examples, read only the relevant reference file instead of expanding the main skill body:
- -
references/react-spa.md for React or TypeScript SPA login, callback, guards, and API calls. - INLINECODE6 for Spring Boot bearer-token validation and route protection.
- INLINECODE7 for dual-provider or multi-issuer setups.
Frontend
Prefer these options when they fit the existing stack:
- - INLINECODE8
- INLINECODE9
- Existing auth wrapper already present in the repo
Generate custom PKCE and token exchange code only when the project constraints require it.
Spring Boot Backend
Prefer these options before writing custom JWT validation services:
- -
spring-boot-starter-oauth2-client for login flows - INLINECODE11 for bearer token validation
- Spring Security configuration over manual interceptors when standard resource server behavior is enough
Only fall back to manual JWKS lookup and custom validation logic when the provider or architecture requires non-standard behavior.
Implementation Checklist
When producing code or a plan, cover the relevant items below.
Frontend Deliverables
- - OIDC provider configuration
- Login entry point
- Callback handling
- Auth state restoration on app startup
- Route protection or guard logic
- API client auth header strategy
- Token refresh behavior if required
- Logout flow
- Return-to-original-page behavior after login
- Multi-provider selection if applicable
Backend Deliverables
- - Provider configuration and environment variables
- Metadata or JWKS discovery
- Token validation strategy
- User principal or claims mapping
- Protected and public route rules
- CORS and cookie strategy when frontend and backend are split
- Multi-issuer handling if applicable
Architecture Guidance
Use the architecture that matches the product, not the most code-heavy option.
SPA Talking Directly to APIs
Use Authorization Code Flow with PKCE.
Include:
- - provider config
- callback page
- auth state provider
- refresh strategy if needed
- 401 retry or re-login behavior
Be explicit about token storage tradeoffs.
Backend or BFF Owns the Session
Prefer backend-managed sessions and httpOnly cookies.
This is usually the safer default when:
- - the backend already exists
- the frontend is same-origin with the backend
- the app does not need browser-side access tokens
- the team wants to reduce token exposure in JavaScript
Multi-Provider Authentication
Keep provider-specific configuration explicit.
- - Separate provider configs.
- Persist which provider initiated the login flow.
- Keep callback handling able to resolve the active provider.
- On the backend, support issuer-based selection rather than guessing.
Output Expectations
When responding to a user task, produce only what is needed for that repository and request.
Prefer this order:
- 1. State the chosen flow and why it fits.
- List the files or components that need to exist.
- Implement the minimal complete path.
- Call out required environment variables and provider settings.
- Mention important follow-up checks such as logout, refresh, and expired-token handling.
Avoid pasting large tutorial blocks if the task only needs a narrow change.
Common Pitfalls
Watch for these failure modes:
- - Hardcoding token or JWKS endpoints instead of using discovery metadata.
- Recommending implicit flow.
- Storing refresh tokens in browser storage without acknowledging the risk.
- Forgetting to persist and restore the original return URL.
- Implementing refresh but not handling refresh failure.
- Validating signature only, while skipping issuer or audience checks.
- Assuming a single issuer when the app supports multiple providers.
- Mixing frontend login concerns with backend bearer-token validation concerns.
Minimal Patterns
Use compact patterns instead of oversized examples.
Frontend Config Shape
CODEBLOCK0
If the app is a public client, add PKCE support.
Spring Resource Server Direction
CODEBLOCK1
Prefer this direction over handwritten JWT parsing when standard Spring Security support is sufficient.
For fuller examples, read the relevant file from references/ rather than pasting every variant by default.
Verification
Before considering the integration complete, verify the relevant flows:
- - Login succeeds from a clean session.
- Callback handles success and provider error responses.
- Protected routes redirect or reject correctly.
- Expired access tokens are handled correctly.
- Refresh behavior works or fails cleanly.
- Backend rejects tokens with wrong issuer or audience.
- Logout clears local state and, when needed, signs out from the provider.
Final Reminder
This skill should help the model make a correct architectural choice first, then implement the smallest sound solution for that stack. Favor safe defaults, existing framework support, and repository-specific adaptation over generic auth boilerplate.
OIDC 集成
使用此技能设计或实现依赖 OpenID Connect 提供者的身份验证流程。
目标
帮助用户以匹配其架构、避免不安全默认设置、生成适合当前技术栈而非通用示例代码的方式集成 OIDC。
工作方式
在编写代码之前,先确定实际的集成形态。
首先检查以下几点:
- - 哪一方持有认证状态:仅 SPA、后端会话或 BFF。
- 涉及哪个提供者:Keycloak、Auth0、IdentityServer、Authing、Azure AD 或其他符合 OIDC 标准的身份提供者。
- 是一个提供者还是多个提供者。
- 客户端是公开的还是机密的。
- 是否需要刷新令牌。
- 用户只需要登录、登录加登出、路由保护、令牌验证,还是完整的端到端流程。
- 代码库是否已使用应扩展而非替换的身份验证库。
如果用户只需要某一层,则保持在该层范围内。除非任务实际跨越前端和后端,否则不要同时生成前端和后端集成。
安全默认设置
除非用户明确需要不同的设置,否则应用以下默认设置:
- - 优先使用授权码流程。
- 对于基于浏览器的公开客户端,使用 PKCE。
- 优先从 /.well-known/openid-configuration 发现提供者元数据,而非硬编码端点。
- 优先使用服务器管理的会话或 httpOnly cookie,而非在 localStorage 中存储长期令牌。
- 如果纯 SPA 必须在浏览器存储中持有令牌,请说明权衡并保持令牌生命周期较短。
- 仅在确实需要刷新令牌时请求 offline_access。
- 验证 iss、aud、签名、过期时间和密钥轮换行为。
- 将登出视为两个独立的问题:本地应用登出和提供者登出。
不要将不安全的捷径规范化为默认推荐。
首选实现路径
在手动编写身份验证代码之前,优先使用框架原生或得到良好支持的库。
如果用户要求具体的实现示例,请仅读取相关的参考文件,而不是展开主要技能主体:
- - references/react-spa.md 用于 React 或 TypeScript SPA 的登录、回调、守卫和 API 调用。
- references/spring-resource-server.md 用于 Spring Boot 的承载令牌验证和路由保护。
- references/multi-provider.md 用于双提供者或多签发者设置。
前端
当以下选项适合现有技术栈时优先使用:
- - oidc-client-ts
- react-oidc-context
- 仓库中已有的身份验证包装器
仅在项目约束要求时生成自定义 PKCE 和令牌交换代码。
Spring Boot 后端
在编写自定义 JWT 验证服务之前,优先使用以下选项:
- - spring-boot-starter-oauth2-client 用于登录流程
- spring-boot-starter-oauth2-resource-server 用于承载令牌验证
- 当标准资源服务器行为足够时,使用 Spring Security 配置而非手动拦截器
仅当提供者或架构需要非标准行为时,才回退到手动 JWKS 查找和自定义验证逻辑。
实现检查清单
在生成代码或计划时,涵盖以下相关项目。
前端交付物
- - OIDC 提供者配置
- 登录入口点
- 回调处理
- 应用启动时的认证状态恢复
- 路由保护或守卫逻辑
- API 客户端认证头策略
- 令牌刷新行为(如果需要)
- 登出流程
- 登录后返回原始页面的行为
- 多提供者选择(如果适用)
后端交付物
- - 提供者配置和环境变量
- 元数据或 JWKS 发现
- 令牌验证策略
- 用户主体或声明映射
- 受保护和公开的路由规则
- 前后端分离时的 CORS 和 Cookie 策略
- 多签发者处理(如果适用)
架构指导
使用与产品匹配的架构,而非代码量最多的选项。
SPA 直接与 API 通信
使用带有 PKCE 的授权码流程。
包括:
- - 提供者配置
- 回调页面
- 认证状态提供者
- 刷新策略(如果需要)
- 401 重试或重新登录行为
明确说明令牌存储的权衡。
后端或 BFF 持有会话
优先使用后端管理的会话和 httpOnly cookie。
在以下情况下,这通常是更安全的默认选择:
- - 后端已存在
- 前端与后端同源
- 应用不需要浏览器端访问令牌
- 团队希望减少 JavaScript 中的令牌暴露
多提供者认证
保持提供者特定配置明确。
- - 独立的提供者配置。
- 持久化记录哪个提供者发起了登录流程。
- 保持回调处理能够解析当前活动的提供者。
- 在后端,支持基于签发者的选择而非猜测。
输出期望
在响应用户任务时,仅生成该仓库和请求所需的内容。
优先按以下顺序:
- 1. 说明选择的流程及其适用原因。
- 列出需要存在的文件或组件。
- 实现最小完整路径。
- 指出所需的环境变量和提供者设置。
- 提及重要的后续检查,如登出、刷新和过期令牌处理。
如果任务仅需要小幅更改,避免粘贴大段教程。
常见陷阱
注意以下失败模式:
- - 硬编码令牌或 JWKS 端点,而非使用发现元数据。
- 推荐隐式流程。
- 在浏览器存储中存储刷新令牌而未说明风险。
- 忘记持久化和恢复原始返回 URL。
- 实现刷新但未处理刷新失败。
- 仅验证签名,跳过签发者或受众检查。
- 在应用支持多个提供者时假设单一签发者。
- 混淆前端登录关注点与后端承载令牌验证关注点。
最小模式
使用紧凑模式而非过大的示例。
前端配置形状
typescript
export const oidcConfig = {
authority: import.meta.env.VITEOIDCAUTHORITY,
clientId: import.meta.env.VITEOIDCCLIENT_ID,
redirectUri: ${window.location.origin}/auth/callback,
scope: openid profile email,
responseType: code,
};
如果应用是公开客户端,添加 PKCE 支持。
Spring 资源服务器方向
yaml
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://idp.example.com
当标准 Spring Security 支持足够时,优先使用此方向而非手写 JWT 解析。
对于更完整的示例,请从 references/ 读取相关文件,而非默认粘贴所有变体。
验证
在认为集成完成之前,验证相关流程:
- - 从干净会话成功登录。
- 回调处理成功和提供者错误响应。
- 受保护路由正确重定向或拒绝。
- 过期访问令牌被正确处理。
- 刷新行为正常工作或干净地失败。
- 后端拒绝具有错误签发者或受众的令牌。
- 登出清除本地状态,并在需要时从提供者登出。
最后提醒
此技能应帮助模型首先做出正确的架构选择,然后为该技术栈实现最小的合理解决方案。优先使用安全默认设置、现有框架支持和仓库特定适配,而非通用身份验证样板代码。