Example usage for org.springframework.http HttpStatus UNAUTHORIZED

List of usage examples for org.springframework.http HttpStatus UNAUTHORIZED

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus UNAUTHORIZED.

Prototype

HttpStatus UNAUTHORIZED

To view the source code for org.springframework.http HttpStatus UNAUTHORIZED.

Click Source Link

Document

401 Unauthorized .

Usage

From source file:org.systemexception.springmongorest.Application.java

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {

    return new EmbeddedServletContainerCustomizer() {
        @Override//w  ww.  j a v  a2 s .  co m
        public void customize(ConfigurableEmbeddedServletContainer container) {

            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

            container.addErrorPages(error401Page, error404Page, error500Page);
        }
    };
}

From source file:org.openmhealth.dsu.controller.GenericExceptionHandlingControllerAdvice.java

@ExceptionHandler(AuthenticationCredentialsNotFoundException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public void handleAuthenticationCredentialsNotFoundException(Exception e, HttpServletRequest request) {

    log.debug("A {} request for '{}' failed authentication.", request.getMethod(), request.getPathInfo(), e);
}

From source file:io.pivotal.ecosystem.service.HelloControllerTest.java

@Test
public void testIt() {
    ResponseEntity<String> greeting = helloController.greeting(USER);
    assertNotNull(greeting);/* w  w  w.j ava2s. co  m*/
    assertEquals("Sorry, I don't think we've met.", greeting.getBody());
    assertEquals(HttpStatus.UNAUTHORIZED, greeting.getStatusCode());

    ResponseEntity<User> in = helloController.createUser(new User(USER, ROLE));

    assertNotNull(in);
    assertEquals(HttpStatus.CREATED, in.getStatusCode());
    User u = in.getBody();
    assertNotNull(u);
    assertEquals(USER, u.getName());
    assertNotNull(u.getPassword());

    greeting = helloController.greeting(USER);
    assertNotNull(greeting);
    assertEquals("Hello, foo !", greeting.getBody());
    assertEquals(HttpStatus.OK, greeting.getStatusCode());

    ResponseEntity<Void> out = helloController.deleteUser(USER);
    assertNotNull(out);
    assertEquals(HttpStatus.OK, out.getStatusCode());

    greeting = helloController.greeting(USER);
    assertNotNull(greeting);
    assertEquals("Sorry, I don't think we've met.", greeting.getBody());
    assertEquals(HttpStatus.UNAUTHORIZED, greeting.getStatusCode());
}

From source file:com.companyname.plat.commons.client.HttpRestfulClient.java

public Object getObject(Map<String, String> params, Class clz) {
    RestTemplate restTemplate = new RestTemplate();

    try {//from www  . j a  v  a  2  s .  c  o m
        ResponseEntity<Object> entity = restTemplate.exchange(getEndPoint(), HttpMethod.GET,
                getHttpRequest(params), clz);

        setResponseStatus(entity.getStatusCode().toString());
        return entity.getBody();
    } catch (HttpClientErrorException ex) {
        if (HttpStatus.UNAUTHORIZED == ex.getStatusCode()) {
            System.out
                    .println("Unauthorized call to " + this.getEndPoint() + "\nWrong login and/or password (\""
                            + this.getUserName() + "\" / \"" + this.getPassword() + "\")");

            System.out.println("Cause: \n" + ex.getMessage());
        }
    }

    return null;
}

From source file:com.boxedfolder.carrot.Application.java

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override//from   ww  w.j  a  v  a2s .  c o  m
        public void customize(ConfigurableEmbeddedServletContainer container) {
            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
            container.addErrorPages(error401Page, error404Page, error500Page);
        }
    };
}

From source file:io.sevenluck.chat.controller.LoginController.java

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<?> handleException(LoginException e) {
    logger.error("login failed:", e.getMessage());
    return new ResponseEntity<>(ExceptionDTO.newUnauthorizedInstance(e.getMessage()), new HttpHeaders(),
            HttpStatus.UNAUTHORIZED);
}

From source file:com.jiwhiz.rest.RestErrorHandler.java

/**
 * Return 401 Unauthorized if user provided wrong credentials.
 * //from www  .ja  v a 2  s  .c  om
 * @param ex
 */
@ExceptionHandler(AuthenticationCredentialsNotFoundException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public void handleAuthenticationCredentialsNotFoundException(AuthenticationCredentialsNotFoundException ex) {
    LOGGER.debug("User provided wrong credentials.");
}

From source file:com.marklogic.samplestack.testing.LoginTestsImpl.java

/**
 * Bad credentials is a 401 Error body is in JSON
 * /*from ww w. j a v  a 2  s  . c om*/
 * @throws Exception
 */
public void loginBadCredentials() throws Exception {

    // no username/password match
    mockMvc.perform(post("/login").with(csrf()).param("username", "nobody").param("password", "nopassword"))
            .andExpect(status().is(HttpStatus.UNAUTHORIZED.value())).andReturn().getRequest().getSession();

    // bad credentials, existing user
    mockMvc.perform(post("/login").with(csrf()).param("username", "joeUser@marklogic.com").param("password",
            "notJoesPassword")).andExpect(status().is(HttpStatus.UNAUTHORIZED.value())).andReturn().getRequest()
            .getSession();

    String errorString = mockMvc
            .perform(post("/login").with(csrf()).param("username", "joeUser@marklogic.com").param("password",
                    "notJoesPassword"))
            .andExpect(status().is(HttpStatus.UNAUTHORIZED.value())).andReturn().getResponse()
            .getContentAsString();

    // ensure parsing
    logger.debug("Response from mock bad auth: " + errorString);

    JsonNode errorNode = mapper.readValue(errorString, JsonNode.class);
    assertEquals("Error node has 401 in status", errorNode.get("status").asText(), "401");

}

From source file:com.oneops.security.APIAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    logger.error("Exception serving request" + exception.getMessage());
    CmsBaseException ce = new CmsBaseException(CmsError.RUNTIME_EXCEPTION, exception.getMessage());
    ErrorResponse error = new ErrorResponse(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.value(),
            exception.getMessage());/* w ww.ja v  a 2  s. co  m*/
    response.setStatus(error.getCode());
    response.getWriter().write(gson.toJson(error));
}