Search code examples
javajettyopenidembedded-jetty

How do add an OpenID service to an embedded Jetty server. Getting No IdentityService for org.eclipse.jetty.security.openid.OpenIdAuthenticator


I am trying to add an OpenId service to an embedded Jetty Server. I followed the Jetty documentation Here and now I am getting the following error.

No IdentityService for org.eclipse.jetty.security.openid.OpenIdAuthenticator

I can't seem to find a full example of setting this up in Jetty and I am not sure about how to set up an Identity Service. The following is my code.

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();

}

If anyone has any guidance I would appreciate it. Thanks.


Solution

  • You are setting the identity service to be null because no IdentityService has been set on the OpenIdLoginService.

    You can set the IdentityService explicitly with:

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

    Alternatively if you define a realm name without setting an IdentityService, then one will automatically be created when starting.

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