如何将 OpenID 服务添加到嵌入式 Jetty 服务器。没有为 org.eclipse.jetty.security.openid.OpenIdAuthenticator

时间:2021-06-03 17:53:29

标签: java jetty openid embedded-jetty

我正在尝试将 OpenId 服务添加到嵌入式 Jetty 服务器。我遵循了 Jetty 文档 Here,现在出现以下错误。

<块引用>

没有 IdentityService org.eclipse.jetty.security.openid.OpenIdAuthenticator

我似乎找不到在 Jetty 中进行设置的完整示例,而且我不确定如何设置身份服务。以下是我的代码。

public static void main(String[] args) throws Exception {
if (args.length != 1) {
  System.err.println("Usage: need a relative path to the war file to execute");
  System.exit(1);
}

System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "INFO");

// Create a basic Jetty server object that will listen on port defined by
// the PORT environment variable when present, otherwise on 8080.
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
Server server = new Server(port);

System.out.println("clientid = " + clientId);
System.out.println("clientSecret = " + clientSecret);

// The WebAppContext is the interface to provide configuration for a web
// application. In this example, the context path is being set to "/" so
// it is suitable for serving root context requests.
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(args[0]);
ClassList classlist = ClassList.setServerDefault(server);

// Enable Annotation Scanning.
classlist.addBefore(
    "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
    "org.eclipse.jetty.annotations.AnnotationConfiguration");

OpenIdConfiguration openIdConfig = new OpenIdConfiguration(issuer, clientId, clientSecret);
OpenIdLoginService loginService = new OpenIdLoginService(openIdConfig);
Authenticator authenticator = new OpenIdAuthenticator(openIdConfig, "/error");


SecurityHandler securityHandler = webapp.getSecurityHandler();
securityHandler.setLoginService(loginService);
securityHandler.setAuthenticator(authenticator);
securityHandler.setIdentityService(loginService.getIdentityService());

webapp.setSecurityHandler(securityHandler);
// Set the the WebAppContext as the ContextHandler for the server.
server.setHandler(webapp);

// Start the server! By using the server.join() the server thread will
// join with the current thread. See
// "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()"
// for more details.
server.start();
server.join();

}

如果有人有任何指导,我将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您将身份服务设置为 null,因为 IdentityService 上没有设置 OpenIdLoginService

您可以通过以下方式显式设置 IdentityService

SecurityHandler securityHandler = webapp.getSecurityHandler();
securityHandler.setAuthenticator(authenticator);
securityHandler.setLoginService(loginService);
securityHandler.setIdentityService(new DefaultIdentityService());

或者,如果您定义领域名称而不设置 IdentityService,则启动时将自动创建一个。

SecurityHandler securityHandler = webapp.getSecurityHandler();
securityHandler.setRealmName(issuer);
securityHandler.setAuthenticator(authenticator);
securityHandler.setLoginService(loginService);