Loading...
Loading...
Compare original and translation side by side
com.auth0:mvc-auth-commonsAuthenticationControllerAgent instruction: Before providing SDK setup instructions, fetch the latest release version by running:bashgh api repos/auth0/auth0-java-mvc-common/releases/latest --jq '.tag_name'Use the returned version in all dependency lines instead of any hardcoded version below. If the API call fails, use.1.12.0
com.auth0:mvc-auth-commonsAuthenticationControllerAgent 指令: 在提供SDK安装说明前,请先运行以下命令获取最新版本:bashgh api repos/auth0/auth0-java-mvc-common/releases/latest --jq '.tag_name'将返回的版本号替换以下所有依赖配置中的硬编码版本。若API调用失败,则使用。1.12.0
auth0-quickstartauth0-quickstart| Use Case | Recommended Skill |
|---|---|
| Spring Boot web applications with auto-configuration | Use Spring Boot + Okta starter for auto-configured Spring Boot login |
| Spring Boot REST APIs (stateless JWT) | Use |
| Single Page Applications | Use |
| Mobile applications | Use |
| Machine-to-machine API calls | Use Auth0 Management API SDK for server-to-server |
| 使用场景 | 推荐技能 |
|---|---|
| 带自动配置的Spring Boot Web应用 | 使用Spring Boot + Okta starter实现自动配置的Spring Boot登录 |
| Spring Boot REST API(无状态JWT) | 使用 |
| 单页应用(SPA) | 使用 |
| 移动应用 | 使用 |
| 机器对机器API调用 | 使用Auth0 Management API SDK实现服务器间认证 |
Agent instruction: If the user's prompt already provides Auth0 credentials (domain, client ID, client secret), use them directly — skip the bootstrap script and credential questions. Only offer setup options when credentials are missing.
Agent 指令: 如果用户的提示中已提供Auth0凭证(域名、客户端ID、客户端密钥),请直接使用——跳过引导脚本和凭证询问步骤。仅当凭证缺失时才提供设置选项。
implementation 'com.auth0:mvc-auth-commons:1.12.0'<dependency>
<groupId>com.auth0</groupId>
<artifactId>mvc-auth-commons</artifactId>
<version>1.12.0</version>
</dependency>implementation 'com.auth0:mvc-auth-commons:1.12.0'<dependency>
<groupId>com.auth0</groupId>
<artifactId>mvc-auth-commons</artifactId>
<version>1.12.0</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 application?
- Automated — I'll run Auth0 CLI scripts that create the application and write the values to your config automatically.
- Manual — You create the application yourself in the Auth0 Dashboard (or via
) and provide me the Domain, Client ID, and Client Secret.auth0 apps 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.
undefined暂停操作——请先询问用户。请严格按照以下问题提问,等待用户回复后再继续:"你希望如何创建Auth0应用?
- 自动化——我将运行Auth0 CLI脚本创建应用,并自动将配置值写入你的配置文件。
- 手动——你自行在Auth0控制台(或通过
命令)创建应用,并提供域名、客户端ID和客户端密钥。auth0 apps create你更倾向于哪种方式?(1 = 自动化 / 2 = 手动)"在用户回复前,请勿进行任何后续设置步骤,不要默认选择手动方式。
undefined
Or create manually in Auth0 Dashboard → Applications → Applications → Create Application → Regular Web Applications
或手动在Auth0控制台创建:应用 → 应用 → 创建应用 → 常规Web应用export AUTH0_DOMAIN="your-tenant.auth0.com"
export AUTH0_CLIENT_ID="your-client-id"
export AUTH0_CLIENT_SECRET="your-client-secret".env.gitignoreAUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secretAgent instruction: Java does not auto-loadfiles..envonly reads OS-level environment variables. If you generate aSystem.getenv()file, you must also either: (1) add dotenv-java as a dependency and use.envinstead ofDotenv.load().get("AUTH0_DOMAIN"), or (2) instruct the user to runSystem.getenv()before starting the server. Do not generate code that uses both asource .envfile and.envwithout a loading mechanism — the values will beSystem.getenv().null
https://export AUTH0_DOMAIN="your-tenant.auth0.com"
export AUTH0_CLIENT_ID="your-client-id"
export AUTH0_CLIENT_SECRET="your-client-secret".env.gitignoreAUTH0_DOMAIN=your-tenant.auth0.com
AUTH0_CLIENT_ID=your-client-id
AUTH0_CLIENT_SECRET=your-client-secretAgent 指令: Java不会自动加载文件。.env仅读取系统级环境变量。若生成System.getenv()文件,你必须同时执行以下操作之一:(1) 添加dotenv-java作为依赖,并使用.env替代Dotenv.load().get("AUTH0_DOMAIN");(2) 指导用户在启动服务器前运行System.getenv()。请勿生成同时使用source .env文件和.env但无加载机制的代码——否则值会为System.getenv()。null
https://AuthenticationControllerimport com.auth0.AuthenticationController;
import com.auth0.jwk.JwkProviderBuilder;
import com.auth0.jwk.JwkProvider;
public class Auth0Config {
private static final AuthenticationController controller = createController();
private static AuthenticationController createController() {
String domain = System.getenv("AUTH0_DOMAIN");
String clientId = System.getenv("AUTH0_CLIENT_ID");
String clientSecret = System.getenv("AUTH0_CLIENT_SECRET");
JwkProvider jwkProvider = new JwkProviderBuilder(domain).build();
return AuthenticationController.newBuilder(domain, clientId, clientSecret)
.withJwkProvider(jwkProvider)
.build();
}
public static AuthenticationController getAuthController() {
return controller;
}
}AuthenticationControllerimport com.auth0.AuthenticationController;
import com.auth0.jwk.JwkProviderBuilder;
import com.auth0.jwk.JwkProvider;
public class Auth0Config {
private static final AuthenticationController controller = createController();
private static AuthenticationController createController() {
String domain = System.getenv("AUTH0_DOMAIN");
String clientId = System.getenv("AUTH0_CLIENT_ID");
String clientSecret = System.getenv("AUTH0_CLIENT_SECRET");
JwkProvider jwkProvider = new JwkProviderBuilder(domain).build();
return AuthenticationController.newBuilder(domain, clientId, clientSecret)
.withJwkProvider(jwkProvider)
.build();
}
public static AuthenticationController getAuthController() {
return controller;
}
}import com.auth0.AuthenticationController;
import com.auth0.AuthorizeUrl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AuthenticationController controller = Auth0Config.getAuthController();
// Build callback URL — omit port for standard ports (80/443) to avoid
// mismatch with the URL registered in Auth0 Dashboard, especially behind proxies.
String scheme = request.getScheme();
int port = request.getServerPort();
String redirectUrl = scheme + "://" + request.getServerName()
+ ((port == 80 || port == 443) ? "" : ":" + port) + "/callback";
AuthorizeUrl authorizeUrl = controller.buildAuthorizeUrl(request, response, redirectUrl)
.withScope("openid profile email");
response.sendRedirect(authorizeUrl.build());
}
}import com.auth0.AuthenticationController;
import com.auth0.AuthorizeUrl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AuthenticationController controller = Auth0Config.getAuthController();
// 构建回调URL — 标准端口(80/443)省略端口号,避免与Auth0控制台中注册的URL不匹配,尤其是在代理环境下。
String scheme = request.getScheme();
int port = request.getServerPort();
String redirectUrl = scheme + "://" + request.getServerName()
+ ((port == 80 || port == 443) ? "" : ":" + port) + "/callback";
AuthorizeUrl authorizeUrl = controller.buildAuthorizeUrl(request, response, redirectUrl)
.withScope("openid profile email");
response.sendRedirect(authorizeUrl.build());
}
}import com.auth0.AuthenticationController;
import com.auth0.IdentityVerificationException;
import com.auth0.Tokens;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/callback"})
public class CallbackServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AuthenticationController controller = Auth0Config.getAuthController();
try {
Tokens tokens = controller.handle(request, response);
request.getSession().setAttribute("accessToken", tokens.getAccessToken());
request.getSession().setAttribute("idToken", tokens.getIdToken());
response.sendRedirect("/dashboard");
} catch (IdentityVerificationException e) {
response.sendRedirect("/login?error=" + e.getCode());
}
}
}import com.auth0.AuthenticationController;
import com.auth0.IdentityVerificationException;
import com.auth0.Tokens;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/callback"})
public class CallbackServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
AuthenticationController controller = Auth0Config.getAuthController();
try {
Tokens tokens = controller.handle(request, response);
request.getSession().setAttribute("accessToken", tokens.getAccessToken());
request.getSession().setAttribute("idToken", tokens.getIdToken());
response.sendRedirect("/dashboard");
} catch (IdentityVerificationException e) {
response.sendRedirect("/login?error=" + e.getCode());
}
}
}import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(urlPatterns = {"/dashboard/*", "/api/private/*"})
public class AuthenticationFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("idToken") == null) {
response.sendRedirect("/login");
return;
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
}import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(urlPatterns = {"/dashboard/*", "/api/private/*"})
public class AuthenticationFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("idToken") == null) {
response.sendRedirect("/login");
return;
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
}Agent instruction: After writing all code, verify the build succeeds:bash./gradlew buildor. If build fails, diagnose and fix. After 5-6 failed attempts, usemvn packageto get help.AskUserQuestion
http://localhost:3000/login/dashboardAgent 指令: 写完所有代码后,验证构建是否成功:bash./gradlew build或。若构建失败,请排查并修复。若尝试5-6次仍失败,请使用mvn package寻求帮助。AskUserQuestion
http://localhost:3000/login/dashboard| Mistake | Fix |
|---|---|
Domain includes | Use |
| Client secret hardcoded in source | Use environment variables or |
| Created SPA or Native app instead of Regular Web | Must create Regular Web Application in Auth0 Dashboard |
| Callback URL mismatch | Callback URL in code must exactly match what's registered in Auth0 Dashboard |
Missing | Always include |
Not handling | Always catch this in the callback handler to show login errors |
Using | Regular web apps must use |
| Session not invalidated on logout | Call |
| 错误 | 修复方案 |
|---|---|
域名包含 | 仅使用 |
| 客户端密钥硬编码到源码中 | 使用环境变量或 |
| 创建了SPA或原生应用而非常规Web应用 | 必须在Auth0控制台创建常规Web应用 |
| 回调URL不匹配 | 代码中的回调URL必须与Auth0控制台中注册的URL完全一致 |
缺少 | 权限中必须包含 |
未处理 | 必须在回调处理器中捕获该异常以显示登录错误 |
使用 | 常规Web应用必须使用 |
| 登出时未使会话失效 | 重定向到Auth0登出页面之前,调用 |
DomainResolverDomainResolverauth0-quickstartauth0-springboot-apiauth0-quickstartauth0-springboot-apiAuthenticationControllerAuthenticationController.BuildernewBuilder(domain, clientId, clientSecret)AuthorizeUrl/authorizeTokensIdentityVerificationExceptionDomainResolverAuthorizeUrl.withScope("openid profile email").withAudience("https://my-api").withOrganization("org_xxx").withInvitation("invite_xxx").withConnection("google-oauth2").withParameter("key", "value")Tokenstokens.getAccessToken()tokens.getIdToken()tokens.getRefreshToken()offline_accesstokens.getExpiresIn()tokens.getType()tokens.getDomain()tokens.getIssuer()AuthenticationControllerAuthenticationController.BuildernewBuilder(domain, clientId, clientSecret)AuthorizeUrl/authorizeTokensIdentityVerificationExceptionDomainResolverAuthorizeUrl.withScope("openid profile email").withAudience("https://my-api").withOrganization("org_xxx").withInvitation("invite_xxx").withConnection("google-oauth2").withParameter("key", "value")Tokenstokens.getAccessToken()tokens.getIdToken()tokens.getRefreshToken()offline_accesstokens.getExpiresIn()tokens.getType()tokens.getDomain()tokens.getIssuer()