Loading...
Loading...
Compare original and translation side by side
com.auth0:auth0-springboot-apiAgent instruction: Before providing SDK setup instructions, fetch the latest release version by running:bashgh api repos/auth0/auth0-auth-java/releases/latest --jq '.tag_name'Use the returned version in all dependency lines instead of any hardcoded version below. If the API call fails (e.g., no releases yet), use.1.0.0-beta.1
com.auth0:auth0-springboot-apiAgent指令: 在提供SDK设置说明之前,先运行以下命令获取最新版本:bashgh api repos/auth0/auth0-auth-java/releases/latest --jq '.tag_name'将返回的版本替换以下所有依赖行中的硬编码版本。如果API调用失败(例如暂无版本发布),则使用。1.0.0-beta.1
auth0-quickstartauth0-quickstart| Use Case | Recommended Skill |
|---|---|
| Server-rendered web applications (Spring MVC with sessions) | Use |
| Single Page Applications | Use |
| Mobile applications | Use |
| Non-Spring Java APIs | Use |
| 使用场景 | 推荐技能 |
|---|---|
| 服务端渲染的Web应用(带会话的Spring MVC) | 对带登录UI的Spring Boot Web应用使用 |
| 单页应用 | 客户端认证使用 |
| 移动应用 | 原生移动应用使用 |
| 非Spring Java API | 纯Spring Security使用 |
Agent instruction: If the user's prompt already provides Auth0 credentials (domain, audience), use them directly — skip the bootstrap script and credential questions. Only offer setup options when credentials are missing.
Agent指令: 如果用户的提示已提供Auth0凭证(domain、audience),请直接使用——跳过引导脚本和凭证问题。仅当凭证缺失时提供设置选项。
implementation 'com.auth0:auth0-springboot-api:1.0.0-beta.1'<dependency>
<groupId>com.auth0</groupId>
<artifactId>auth0-springboot-api</artifactId>
<version>1.0.0-beta.1</version>
</dependency>implementation 'com.auth0:auth0-springboot-api:1.0.0-beta.1'<dependency>
<groupId>com.auth0</groupId>
<artifactId>auth0-springboot-api</artifactId>
<version>1.0.0-beta.1</version>
</dependency>STOP — ask the user before proceeding.Ask exactly this question and wait for their answer before doing anything else:"How would you like to create the Auth0 API resource?
- Automated — I'll run Auth0 CLI scripts that create the resource and write the values to your application.yml automatically.
- Manual — You create the API yourself in the Auth0 Dashboard (or via
) and provide me the Domain and Audience.auth0 apis createWhich do you prefer? (1 = Automated / 2 = Manual)"Do NOT proceed to any setup steps until the user has answered. Do NOT default to manual.
application.ymlundefined暂停——继续前请询问用户。请准确询问以下问题,等待用户答复后再进行后续操作:"你希望如何创建Auth0 API资源?
- 自动化——我将运行Auth0 CLI脚本创建资源,并自动将值写入你的application.yml。
- 手动——你自行在Auth0控制台(或通过
)创建API,然后提供Domain和Audience。auth0 apis create你偏好哪种方式?(1 = 自动化 / 2 = 手动)"在用户答复前,请勿进行任何设置步骤。请勿默认选择手动方式。
application.ymlundefined
Or create manually in Auth0 Dashboard → Applications → APIs
或在Auth0控制台 → 应用程序 → API中手动创建auth0:
domain: "your-tenant.auth0.com"
audience: "https://my-springboot-api"https://application.propertiesauth0.domain=your-tenant.auth0.com
auth0.audience=https://my-springboot-apiauth0:
domain: "your-tenant.auth0.com"
audience: "https://my-springboot-api"https://application.propertiesauth0.domain=your-tenant.auth0.com
auth0.audience=https://my-springboot-api@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain apiSecurity(
HttpSecurity http,
Auth0AuthenticationFilter authFilter
) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(session ->
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/public").permitAll()
.requestMatchers("/api/protected").authenticated()
.requestMatchers("/api/admin/**").hasAuthority("SCOPE_admin")
.anyRequest().authenticated())
.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}@Configuration
@EnableMethodSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain apiSecurity(
HttpSecurity http,
Auth0AuthenticationFilter authFilter
) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(session ->
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/public").permitAll()
.requestMatchers("/api/protected").authenticated()
.requestMatchers("/api/admin/**").hasAuthority("SCOPE_admin")
.anyRequest().authenticated())
.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
.build();
}
}@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/public")
public ResponseEntity<Map<String, Object>> publicEndpoint() {
return ResponseEntity.ok(Map.of("message", "Public endpoint - no token required"));
}
@GetMapping("/protected")
public ResponseEntity<Map<String, Object>> protectedEndpoint(Authentication authentication) {
Auth0AuthenticationToken token = (Auth0AuthenticationToken) authentication;
return ResponseEntity.ok(Map.of(
"user", authentication.getName(),
"email", token.getClaim("email"),
"scopes", token.getScopes()
));
}
}@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/public")
public ResponseEntity<Map<String, Object>> publicEndpoint() {
return ResponseEntity.ok(Map.of("message", "Public endpoint - no token required"));
}
@GetMapping("/protected")
public ResponseEntity<Map<String, Object>> protectedEndpoint(Authentication authentication) {
Auth0AuthenticationToken token = (Auth0AuthenticationToken) authentication;
return ResponseEntity.ok(Map.of(
"user", authentication.getName(),
"email", token.getClaim("email"),
"scopes", token.getScopes()
));
}
}Agent instruction: After writing all code, verify the build succeeds:bash./gradlew bootRunor. If build fails, diagnose and fix. After 5-6 failed attempts, use./mvnw spring-boot:runto get help.AskUserQuestion
curl http://localhost:8080/api/publiccurl http://localhost:8080/api/protected \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"Agent指令: 编写完所有代码后,验证构建是否成功:bash./gradlew bootRun或。如果构建失败,请诊断并修复。若尝试5-6次仍失败,请使用./mvnw spring-boot:run寻求帮助。AskUserQuestion
curl http://localhost:8080/api/publiccurl http://localhost:8080/api/protected \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"| Mistake | Fix |
|---|---|
Domain includes | Use |
| Audience doesn't match API Identifier | Must exactly match the API Identifier set in Auth0 Dashboard |
| Created Application instead of API in Auth0 | Must create API resource in Auth0 Dashboard → Applications → APIs |
Missing | |
| Using ID token instead of access token | Must use access token for API auth, not ID token |
Checking | Scopes map to |
| Spring Boot env var binding | Use |
| 错误 | 修复方案 |
|---|---|
Domain包含 | 仅使用 |
| Audience与API标识符不匹配 | 必须与Auth0控制台中设置的API标识符完全一致 |
| 在Auth0中创建了应用程序而非API | 必须在Auth0控制台 → 应用程序 → API中创建API资源 |
SecurityConfig中缺少 | |
| 使用ID令牌而非访问令牌 | API认证必须使用访问令牌,而非ID令牌 |
错误格式检查 | 范围会映射为带 |
| Spring Boot环境变量绑定 | 使用 |
@PreAuthorize@PreAuthorizeauth0-quickstartauth0-javaauth0-quickstartauth0-javaapplication.ymlauth0.domainhttps://auth0.audienceauth0.dpop-modeDISABLEDALLOWEDREQUIREDauth0.dpop-iat-offset-secondsauth0.dpop-iat-leeway-secondsAuth0AuthenticationTokenauthentication.getName()subtoken.getClaim("email")token.getClaims()Map<String, Object>token.getScopes()Set<String>requestMatchers("/path").authenticated()hasAuthority("SCOPE_read:data")@PreAuthorizeapplication.ymlauth0.domainhttps://auth0.audienceauth0.dpop-modeDISABLEDALLOWEDREQUIREDauth0.dpop-iat-offset-secondsauth0.dpop-iat-leeway-secondsAuth0AuthenticationTokenauthentication.getName()subtoken.getClaim("email")token.getClaims()Map<String, Object>token.getScopes()Set<String>requestMatchers("/path").authenticated()hasAuthority("SCOPE_read:data")@PreAuthorize