예제 #1
2
  /*
   * curl --data "[email protected]&password=test" http://localhost:8080/prostalytics/rest/auth/login
   *
   */
  @Path("/login")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.TEXT_HTML)
  public Response login(
      @FormParam("email") String email,
      @FormParam("password") String password,
      @HeaderParam("Auth-Token") String token)
      throws URISyntaxException {

    String newToken = null;
    try {
      User user = dao.authenticate(email, hashPassword(password));
      if (user != null) {
        newToken = auth.loggedIn(user, token);
      }

      URI redir =
          uriInfo
              .getBaseUri()
              .resolve(Navigation.fromLogin(user != null ? Navigation.OK : Navigation.ERROR));
      URI uri = UriBuilder.fromUri(redir).build();
      Response.ResponseBuilder res = Response.seeOther(uri);
      if (newToken != null) {
        res.header("Auth-Token", newToken);
      }
      return res.build();
    } catch (Throwable e) {
      URI uri =
          UriBuilder.fromUri(
                  uriInfo.getBaseUri().resolve(Navigation.fromLogin(Navigation.ERROR, e)))
              .build();
      return Response.seeOther(uri).build();
    }
  }
예제 #2
0
  /*
   * curl --data '{"username":"******","password":"******"}' http://localhost:8080/prostalytics/rest/auth/login --header "Content-Type:application/json"
   *
   */
  @Path("/login")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response login(User user, @HeaderParam("Auth-Token") String token)
      throws URISyntaxException {

    String newToken = null;
    try {
      user = dao.authenticate(user.getUsername(), hashPassword(user.getPassword()));
      if (user != null) {
        newToken = auth.loggedIn(user, token);
      }

      Response.ResponseBuilder res = Response.status(Response.Status.OK).entity(user);
      if (newToken != null) {
        res.header("Auth-Token", newToken);
      }
      return res.build();

    } catch (Throwable e) {
      e.printStackTrace(); // TODO :)
      return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()).build();
    }
  }