示例#1
0
 private SecurityContext basicAuthentication(HttpRequest request, HttpResponse response)
     throws IOException {
   List<String> headers = request.getHttpHeaders().getRequestHeader(HttpHeaderNames.AUTHORIZATION);
   if (!headers.isEmpty()) {
     String auth = headers.get(0);
     if (auth.length() > 5) {
       String type = auth.substring(0, 5);
       type = type.toLowerCase();
       if ("basic".equals(type)) {
         String cookie = auth.substring(6);
         cookie = new String(Base64.decodeBase64(cookie.getBytes()));
         String[] split = cookie.split(":");
         Principal user = null;
         try {
           user = domain.authenticate(split[0], split[1]);
           return new NettySecurityContext(user, domain, "BASIC", true);
         } catch (SecurityException e) {
           response.sendError(HttpResponseCodes.SC_UNAUTHORIZED);
           return null;
         }
       } else {
         response.sendError(HttpResponseCodes.SC_UNAUTHORIZED);
         return null;
       }
     }
   }
   return null;
 }
  protected void handleFailure(HttpRequest request, HttpResponse response, Failure failure) {
    if (failure.isLoggable())
      logger.error(
          "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(),
          failure);
    else
      logger.debug(
          "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(),
          failure);

    if (failure.getResponse() != null) {
      writeFailure(request, response, failure.getResponse());
    } else {
      try {
        if (failure.getMessage() != null) {
          response.sendError(failure.getErrorCode(), failure.getMessage());
        } else {
          response.sendError(failure.getErrorCode());
        }
      } catch (IOException e1) {
        throw new UnhandledException(e1);
      }
    }
  }