4.5. 编写应用程序
我们编写一个简单的 Jakarta REST 资源,该资源在授权代码授予响应中返回的所有令牌:
package org.acme.security.openid.connect.web.authentication; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import org.eclipse.microprofile.jwt.Claims; import org.eclipse.microprofile.jwt.JsonWebToken; import io.quarkus.oidc.IdToken; import io.quarkus.oidc.RefreshToken; @Path("/tokens") public class TokenResource { /** * Injection point for the ID token issued by the OpenID Connect provider */ @Inject @IdToken JsonWebToken idToken; /** * Injection point for the access token issued by the OpenID Connect provider */ @Inject JsonWebToken accessToken; /** * Injection point for the refresh token issued by the OpenID Connect provider */ @Inject RefreshToken refreshToken; /** * Returns the tokens available to the application. * This endpoint exists only for demonstration purposes. * Do not expose these tokens in a real application. * * @return an HTML page containing the tokens available to the application. */ @GET @Produces("text/html") public String getTokens() { StringBuilder response = new StringBuilder().append("<html>") .append("<body>") .append("<ul>"); Object userName = this.idToken.getClaim(Claims.preferred_username); if (userName != null) { response.append("<li>username: ").append(userName.toString()).append("</li>"); } Object scopes = this.accessToken.getClaim("scope"); if (scopes != null) { response.append("<li>scopes: ").append(scopes.toString()).append("</li>"); } response.append("<li>refresh_token: ").append(refreshToken.getToken() != null).append("</li>"); return response.append("</ul>").append("</body>").append("</html>").toString(); } }
package org.acme.security.openid.connect.web.authentication;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import org.eclipse.microprofile.jwt.Claims;
import org.eclipse.microprofile.jwt.JsonWebToken;
import io.quarkus.oidc.IdToken;
import io.quarkus.oidc.RefreshToken;
@Path("/tokens")
public class TokenResource {
/**
* Injection point for the ID token issued by the OpenID Connect provider
*/
@Inject
@IdToken
JsonWebToken idToken;
/**
* Injection point for the access token issued by the OpenID Connect provider
*/
@Inject
JsonWebToken accessToken;
/**
* Injection point for the refresh token issued by the OpenID Connect provider
*/
@Inject
RefreshToken refreshToken;
/**
* Returns the tokens available to the application.
* This endpoint exists only for demonstration purposes.
* Do not expose these tokens in a real application.
*
* @return an HTML page containing the tokens available to the application.
*/
@GET
@Produces("text/html")
public String getTokens() {
StringBuilder response = new StringBuilder().append("<html>")
.append("<body>")
.append("<ul>");
Object userName = this.idToken.getClaim(Claims.preferred_username);
if (userName != null) {
response.append("<li>username: ").append(userName.toString()).append("</li>");
}
Object scopes = this.accessToken.getClaim("scope");
if (scopes != null) {
response.append("<li>scopes: ").append(scopes.toString()).append("</li>");
}
response.append("<li>refresh_token: ").append(refreshToken.getToken() != null).append("</li>");
return response.append("</ul>").append("</body>").append("</html>").toString();
}
}
此端点注入了 ID、访问和刷新令牌。它从 ID 令牌、访问令牌中的 范围
声明和刷新令牌可用性状态返回 preferred_username
声明。
只有在端点需要使用 ID 令牌与当前经过身份验证的用户交互时,您只需要注入令牌,或使用访问令牌代表此用户访问下游服务。
如需更多信息,请参阅参考指南中的 访问 ID 和访问令牌 部分。