我有一个简单的dropwizard应用程序。现在当用户尝试登录时 无效的凭据,它只是不断弹出登录框(在一个 浏览器客户端)。 我希望能够抛出一个异常,返回一个允许用户使用的JSON 知道认证失败了。
我尝试了以下内容:
@Override
public Optional<SimplePrincipal> authenticate(BasicCredentials credentials)
throws AuthenticationException {
DPSLookup dps = new DPSLookup(credentials.getUsername(), credentials.
getPassword(), ldap);
if (!dps.dpsLookup()) {
throw new InvalidCredentialException("Access Denied");
}
else {
SimplePrincipal user = new SimplePrincipal(credentials.
getUsername());
return Optional.of(user);
}
}
然后我有一个InvalidCredentialException类: 公共类InvalidCredentialException扩展AuthenticationException {
public InvalidCredentialException(String message) {
super(message);
}
}
然而我现在得到500响应错误,但有以下异常:
WARN [2016-05-03 14:09:49,982]
io.dropwizard.auth.basic.BasicCredentialAuthFilter: Error authenticating
credentials
! bns.gwtd.dpm.auth.InvalidCredentialException: Access Denied
! at
bns.gwtd.dpm.auth.SimpleAuthenticator.authenticate(SimpleAuthenticator.java:50)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
bns.gwtd.dpm.auth.SimpleAuthenticator.authenticate(SimpleAuthenticator.java:24)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.auth.basic.BasicCredentialAuthFilter.filter(BasicCredentialAuthFilter.java:47)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:132)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:68)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:43)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:38)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:256)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:219)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.eclipse.jetty.server.Server.handle(Server.java:499)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
ERROR [2016-05-03 14:09:49,983]
io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a
request: 122bf997f092f693
! javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
! at
io.dropwizard.auth.basic.BasicCredentialAuthFilter.filter(BasicCredentialAuthFilter.java:74)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:132)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:68)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318)
~[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:43)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:38)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:256)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:219)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:240)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:51)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:95)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:159)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.eclipse.jetty.server.Server.handle(Server.java:499)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
[gwtd-auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
! at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
答案 0 :(得分:0)
这里最简单的方法是让InvalidCredentialException扩展NotAuthorizedException,Jersey可以理解扩展WebApplicationException的任何内容并将它们转换为适当的HTTP响应,而不是让它们在服务器上处理。由于InvalidCredentialException已经支持消息,因此最好直接抛出它。