Jetty + Programatic SPNEGO配置

Che*_*tah 5 java jetty spnego embedded-jetty

我正在尝试配置嵌入式Jetty Web服务器以编程方式使用SPNEGO(不使用xml).

我试图将此转换为:http://www.eclipse.org/jetty/documentation/current/spnego-support.html到非基于xml的配置.这是我的尝试:

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

// ...

String domainRealm = "MY.DOMAIN.COM";

Constraint constraint = new Constraint();
constraint.setName(Constraint.__SPNEGO_AUTH);
constraint.setRoles(new String[] { domainRealm });
constraint.setAuthenticate(true);

ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");

SpnegoLoginService loginService = new SpnegoLoginService();
loginService.setConfig(System.getProperty("spnego.properties"));
loginService.setName(domainRealm);

ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
sh.setLoginService(loginService);
sh.setConstraintMappings(new ConstraintMapping[]{cm});
sh.setRealmName(domainRealm);

ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setErrorHandler(new ErrorHandler() { }); // TODO
contextHandler.setContextPath(contextPath);
contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), "/*");
contextHandler.addEventListener(new ContextLoaderListener(context));
contextHandler.setSecurityHandler(sh);

Server server = new Server(port);
server.setHandler(contextHandler);
Run Code Online (Sandbox Code Playgroud)

但是,当我点击服务器时,它正在尝试使用基本身份验证(base 64).

有任何想法吗?

jes*_*ell 1

在 ConstraintSecurityHandler 中,您需要将要使用的身份验证器设置为 SpnegoAuthenticator。

https://github.com/eclipse/jetty.project/blob/master/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/SpnegoAuthenticator.java