我正在使用平针织过滤器
在AuthenticationFilter.java
中的我的代码逻辑中,如果授权标头为空,则返回拒绝访问错误消息
我第一次使用URL通过rest客户端工具点击应用程序而不附加标题
http://localhost:8080/JerseyDemos2/rest/pocservice
获取状态401,错误消息"您无法访问此资源" 。这是对的。
当我试图第二次打击休息客户端工具时,服务器返回异常消息。
我在 tomcat 7.x windows和linux
中部署了我的应用程序为什么我们第二次碰到时会出错。 如何解决这个问题
@Provider
public class AuthenticationFilter implements javax.ws.rs.container.ContainerRequestFilter {
@Context
private ResourceInfo resourceInfo;
private static final String AUTHORIZATION_PROPERTY = "Authorization";
private static final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED).entity("You cannot access this resource").build();
@Override
public void filter(ContainerRequestContext requestContext) {
// Get request headers
final MultivaluedMap<String, String> headers = requestContext.getHeaders();
// Fetch authorization header
final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
// If no authorization information present; block access
if (authorization == null || authorization.isEmpty()) {
requestContext.abortWith(ACCESS_DENIED);
return;
}
}
} }
错误讯息:
Dec 19, 2016 6:26:18 PM org.glassfish.jersey.server.ServerRuntime$Responder writeResponse
SEVERE: An I/O error has occurred while writing a response message entity to the container output stream.
java.lang.IllegalStateException: The output stream has already been closed.
at org.glassfish.jersey.message.internal.CommittingOutputStream.setStreamProvider(CommittingOutputStream.java:147)
at org.glassfish.jersey.message.internal.OutboundMessageContext.setStreamProvider(OutboundMessageContext.java:803)
......
请帮帮我 提前谢谢。
答案 0 :(得分:1)
我删除了静态变量
private static final Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED).entity("You cannot access this resource").build();
我声明了局部变量。现在它的工作正常。
@Provider
public class AuthenticationFilter implements javax.ws.rs.container.ContainerRequestFilter {
@Context
private ResourceInfo resourceInfo;
private static final String AUTHORIZATION_PROPERTY = "Authorization";
@Override
public void filter(ContainerRequestContext requestContext) {
Response ACCESS_DENIED = Response.status(Response.Status.UNAUTHORIZED).entity("You cannot access this resource").build();
// Get request headers
final MultivaluedMap<String, String> headers = requestContext.getHeaders();
// Fetch authorization header
final List<String> authorization = headers.get(AUTHORIZATION_PROPERTY);
// If no authorization information present; block access
if (authorization == null || authorization.isEmpty()) {
requestContext.abortWith(ACCESS_DENIED);
return;
}
}
} }
答案 1 :(得分:0)
您尝试写入之前编写的回复。完整的日志显示它发生的位置。上传日志和使用/修改httpresponse的代码。