Example usage for javax.servlet.http HttpServletResponse SC_UNAUTHORIZED

List of usage examples for javax.servlet.http HttpServletResponse SC_UNAUTHORIZED

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_UNAUTHORIZED.

Prototype

int SC_UNAUTHORIZED

To view the source code for javax.servlet.http HttpServletResponse SC_UNAUTHORIZED.

Click Source Link

Document

Status code (401) indicating that the request requires HTTP authentication.

Usage

From source file:de.knightsoftnet.validators.server.security.HttpAuthenticationEntryPoint.java

@Override
public void commence(final HttpServletRequest prequest, final HttpServletResponse presponse,
        final AuthenticationException pauthException) throws IOException {
    presponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, pauthException.getMessage());
}

From source file:ch.wisv.areafiftylan.security.RESTAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}

From source file:br.com.rzandonai.web.filter.AuthFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    String authHeader = httpRequest.getHeader(AuthUtils.AUTH_HEADER_KEY);

    if (StringUtils.isBlank(authHeader) || authHeader.split(" ").length != 2) {
        httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, AUTH_ERROR_MSG);
    } else {//from   www  .java 2s  .  com
        JWTClaimsSet claimSet = null;
        try {
            claimSet = (JWTClaimsSet) AuthUtils.decodeToken(authHeader);
        } catch (ParseException e) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, JWT_ERROR_MSG);
            return;
        } catch (JOSEException e) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, JWT_INVALID_MSG);
            return;
        }

        // ensure that the token is not expired
        if (new DateTime(claimSet.getExpirationTime()).isBefore(DateTime.now())) {
            httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, EXPIRE_ERROR_MSG);
        } else {
            chain.doFilter(request, response);
        }
    }
}

From source file:nl.surfnet.coin.api.oauth.Http401UnauthorizedEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.addHeader("WWW-Authenticate", "Bearer realm=\"api.surfconext\"");
    httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");

}

From source file:de.steilerdev.myVerein.server.security.rest.RestAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException {
    logger.warn("[{}] Unauthorized access to authentication entry point",
            SecurityHelper.getClientIpAddr(request));
    if (!response.isCommitted()) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
    }//from  w w w  .ja va 2 s.  c  om
}

From source file:pdl.web.filter.JsonBasicAuthenticationEP.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    PrintWriter writer = response.getWriter();
    writer.printf("Error(%s) - %s", HttpServletResponse.SC_UNAUTHORIZED, "Bad credentials");
}

From source file:foam.nanos.blob.FileService.java

@Override
protected void download(X x) {
    OutputStream os = null;//  w  ww.jav  a2s.  co  m
    HttpServletRequest req = x.get(HttpServletRequest.class);
    HttpServletResponse resp = x.get(HttpServletResponse.class);

    try {
        String path = req.getRequestURI();
        String id = path.replaceFirst("/service/" + nspec_.getName() + "/", "");

        // find file from file dao
        File file = (File) fileDAO_.find_(x, id);
        if (file == null || file.getData() == null) {
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // check to see if current user has access to file owner
        if (userDAO_.find_(x, file.getOwner()) == null) {
            resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }

        // get blob and blob size
        // TODO: figure out why delegate is not being set for IdentifiedBlob
        String blobId = ((IdentifiedBlob) file.getData()).getId();
        Blob blob = getDelegate().find_(x, blobId);
        long size = blob.getSize();

        // set response status, content type, content length
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.setContentType(file.getMimeType());
        resp.setHeader("Content-Length", Long.toString(size, 10));
        resp.setHeader("Cache-Control", "public");

        // output data to response
        os = resp.getOutputStream();
        blob.read(os, 0, size);
        os.close();
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.thinkberg.webdav.UnlockHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());
    String lockTokenHeader = request.getHeader("Lock-Token");
    String lockToken = lockTokenHeader.substring(1, lockTokenHeader.length() - 1);
    LOG.debug("UNLOCK(" + lockToken + ")");

    if (LockManager.getInstance().releaseLock(object, lockToken)) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } else {//from   w  ww .  j a v a  2 s.  c  om
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }
}

From source file:de.knightsoftnet.validators.server.security.AuthFailureHandler.java

@Override
public void onAuthenticationFailure(final HttpServletRequest prequest, final HttpServletResponse presponse,
        final AuthenticationException pexception) throws IOException, ServletException {
    presponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    final PrintWriter writer = presponse.getWriter();
    writer.write(pexception.getMessage());
    writer.flush();/*  www. j a  v a  2 s  . co  m*/
}

From source file:org.ngrinder.security.SvnHttpBasicEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, // LB
        AuthenticationException authException) throws IOException, ServletException {
    // Get the first part of url path and use it as a realm.
    String pathInfo = request.getPathInfo();
    String[] split = StringUtils.split(pathInfo, '/');
    response.addHeader("WWW-Authenticate",
            "Basic realm=\"" + StringUtils.defaultIfBlank(split[0], "admin") + "\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
}