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:cn.org.once.cstack.config.Http401EntryPoint.java

public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException arg)
        throws IOException, ServletException {
    // Maybe change the log level...
    log.warn("Access Denied [ " + request.getRequestURL().toString() + "] : " + arg.getMessage());
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access unauthorized");
}

From source file:org.simbasecurity.core.service.http.DBViewer.java

@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    if (enabled == null || !Boolean.valueOf(enabled)) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        return null;
    }/* ww w .ja v  a  2 s.c  o  m*/

    resp.setContentType("text/html");

    String table = req.getParameter("table");
    String query = req.getParameter("query");
    boolean asSQL = req.getParameter("asSQL") != null;
    PrintWriter out = resp.getWriter();
    printHeader(out);
    printForm(out, req.getRequestURI(), table, asSQL, query);

    if (!StringUtil.isEmpty(table)) {
        if (asSQL) {
            printRecordsAsSQL(out, table);
        } else {
            printRecords(out, "SELECT * FROM " + table);
        }
    } else if (!StringUtil.isEmpty(query)) {
        printRecords(out, query);
    }
    printFooter(out);
    out.close();

    return null;
}

From source file:com.intuit.tank.util.RestSecurityFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (config.isRestSecurityEnabled()) {
        User user = getUser((HttpServletRequest) request);
        if (user == null) {
            // send 401 unauthorized and return
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            return; // break filter chain, requested JSP/servlet will not be executed
        }//from w  w  w  .  j a va 2 s . co m
    }
    chain.doFilter(request, response);
}

From source file:fr.treeptik.cloudunit.config.UserAjaxAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    logger.warn("Authentication failed");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication failed");
}

From source file:gateway.auth.PiazzaBasicAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
        throws IOException, ServletException {
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.setContentType("application/json");
    PrintWriter writer = response.getWriter();
    // Create a Response Object
    ErrorResponse error = new ErrorResponse("Gateway is unable to authenticate the provided user.", "Gateway");

    try {/*  w w w  .  java  2 s  .c  o m*/
        // Log the request
        logger.log(
                String.format("Unable to authenticate a user with Auth Type %s and Header %s",
                        request.getAuthType(), request.getHeader("Authorization").toString()),
                PiazzaLogger.ERROR);
    } catch (Exception exception) {
        String errorString = String.format("Exception encountered during Authorization check: %s.",
                exception.getMessage());
        LOGGER.error(errorString, exception);
        logger.log(errorString, PiazzaLogger.ERROR);
    }

    // Write back the response
    writer.println(new ObjectMapper().writeValueAsString(error));
}

From source file:com.sg.rest.security.components.WebTokenAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
        throws IOException, ServletException {

    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    AuthenficationFailed dto;/*from   w w w .j ava  2s  .com*/
    if (authEx instanceof WebSecurityAccountNotFoundException) {
        dto = new AuthenficationFailed(
                AuthentificationFailureStatus.TOKEN_AUTHENTICATION_ACCOUNT_DO_NOT_EXISTS);
        //dto.setErrorCode(ErrorCodes.TOKEN_AUTHENTICATION_ACCOUNT_DO_NOT_EXISTS);
    } else if (authEx instanceof WebSecurityBadTokenException) {
        dto = new AuthenficationFailed(AuthentificationFailureStatus.TOKEN_AUTHENTICATION_BAD_TOKEN);
        //dto.setErrorCode(ErrorCodes.TOKEN_AUTHENTICATION_BAD_TOKEN);
    } else if (authEx instanceof WebSecurityTokenExpiredException) {
        dto = new AuthenficationFailed(AuthentificationFailureStatus.TOKEN_AUTHENTICATION_TOKEN_EXPIRED);
        //dto.setErrorCode(ErrorCodes.TOKEN_AUTHENTICATION_TOKEN_EXPIRED);
    } else if (authEx instanceof InsufficientAuthenticationException) {
        dto = new AuthenficationFailed(AuthentificationFailureStatus.TOKEN_AUTHENTICATION_NO_TOKEN);
        //dto.setErrorCode(ErrorCodes.TOKEN_AUTHENTICATION_NO_TOKEN);
    } else {
        dto = new AuthenficationFailed(AuthentificationFailureStatus.UNKNOWN);
    }

    LOGGER.error("Authentication failed " + dto.getEventRef().getId() + ": ", authEx);

    response.setContentType(CustomMediaTypes.APPLICATION_JSON_UTF8.getMediatype().toString());
    jacksonObjectMapper.writeValue(response.getWriter(), dto);
}

From source file:com.mirth.connect.server.servlets.UsageServlet.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {// w w w.  j  a  v  a 2 s .c om
        if (!isUserLoggedIn(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
        } else {
            PrintWriter out = response.getWriter();
            ObjectXMLSerializer serializer = ObjectXMLSerializer.getInstance();
            Operation operation = Operations.getOperation(request.getParameter("op"));
            UsageController usageController = ControllerFactory.getFactory().createUsageController();

            if (operation.equals(Operations.USAGE_DATA_GET)) {
                response.setContentType(TEXT_PLAIN);
                if (isUserAuthorized(request, null)) {
                    serializer.serialize(usageController.createUsageStats(true), out);
                } else {
                    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
                }
            }
        }
    } catch (RuntimeIOException rio) {
        logger.debug(rio);
    } catch (Throwable t) {
        logger.debug(ExceptionUtils.getStackTrace(t));
        throw new ServletException(t);
    }
}

From source file:cf.spring.HttpBasicAuthenticator.java

private void setUnathorizedResponse(HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
}

From source file:edu.harvard.hms.dbmi.bd2k.i2b2proxy.filter.SessionFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
        throws IOException, ServletException {

    String user = validateAuthorizationHeader((HttpServletRequest) req);

    if (user == null) {
        ((HttpServletResponse) res).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        res.getOutputStream().write("{\"message\":\"Session is not authorized\"}".getBytes());
        res.getOutputStream().close();//from   w  w w .j  ava 2 s  .  com
        return;
    }

    HttpSession session = ((HttpServletRequest) req).getSession();
    session.setAttribute("user", user);

    fc.doFilter(req, res);
}

From source file:es.galvarez.rest.config.RestAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.addHeader("Access-Control-Allow-Origin", "null");
    response.addHeader("WWW-Authenticate", "RestBasic realm=\"" + getRealmName() + "\"");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.setContentLength(0);/*from   w ww  . j a va2 s . com*/
}