I'm using the JAX-RS framework, to make a restless backend for an application, and I have a resource, to authorize, user, as they login, and here is my code in my login resource so far:
@Path("authentication")
public class AuthenticationRessource {
private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static AuthenticationFacade authenticationFacade = new AuthenticationFacade();
@GET
@Path("login")
@Produces(APPLICATION_JSON)
public Response authenticateUser(@FormParam("email") String email, @FormParam("password") String password) {
try {
//skal måske ændres til bruger
User user = authenticationFacade.authenticateUser(email, password);
String token = authenticationFacade.generateAuthenticationToken(user);
return Response.ok().header(AUTHORIZATION, "Bearer " + token).build();
} catch (Exception e) {
Response.status(Response.Status.UNAUTHORIZED).build();
System.err.print("det fuckede op, på grund af Thomas har ødelagt koden");
e.printStackTrace();
}
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
right now I have a resource for authenticating, but maybe this belongs inside of my user resource, and I should assign a path, for authenticating for each user (user, superuser, admin etc.)
Should I add a provider annotation, for this resource, would it make a difference?
Is it the best practice to add AUTHORIZATION to the header, when I add the token?