Headers map = (Headers) t.getResponseHeaders(); map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\""); return new Authenticator.Retry (401);
/** * The filter's implementation, which is invoked by the server */ public void doFilter (HttpExchange t, Filter.Chain chain) throws IOException { if (authenticator != null) { Authenticator.Result r = authenticator.authenticate (t); if (r instanceof Authenticator.Success) { Authenticator.Success s = (Authenticator.Success)r; ExchangeImpl e = ExchangeImpl.get (t); e.setPrincipal (s.getPrincipal()); chain.doFilter (t); } else if (r instanceof Authenticator.Retry) { Authenticator.Retry ry = (Authenticator.Retry)r; consumeInput (t); t.sendResponseHeaders (ry.getResponseCode(), -1); } else if (r instanceof Authenticator.Failure) { Authenticator.Failure f = (Authenticator.Failure)r; consumeInput (t); t.sendResponseHeaders (f.getResponseCode(), -1); } } else { chain.doFilter (t); } } }
/** * Handles the {@link Result} in case of authorization required. * * @param aHttpExchange The {@link com.sun.net.httpserver.HttpExchange} * which's "WWW-Authenticate" header is to be modified. * * @return the {@link Result} of type {@link Retry}. */ private Authenticator.Result toBasicAuthRequired( HttpExchange aHttpExchange ) { Headers theHeaders = aHttpExchange.getResponseHeaders(); doBasicAuthRequired( theHeaders ); return new Authenticator.Retry( HttpStatusCode.UNAUTHORIZED.getStatusCode() ); }