I'm currently using a object @SessionScoped "SessionBean" to store information of the logged user and to return him a custom ID, for instance.
@Path("/auth")
public class AuthenticationResource {
@SessionScoped
@Inject
SessionBean sessionBean;
@Inject
HttpSession httpSession;
@PermitAll
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public String login(@FormParam("username") String username, @FormParam("password") String password) {
// if OK, validate the session and store info in the 'sessionBean'
if ( customService.isValidUser(username, password) ) {
sessionBean.setJessionId(httpSession.getId());
return sessionBean.getId();
} else {
httpSession.invalidate();
}
return null;
}
@POST
@Path("/logout")
public void logout() {
sessionBean.setJessionId(null);
httpSession.invalidate();
}
}
@SessionScoped
public class SessionBean {
...
}
And to make sure other REST API are protected, I use this sessionScoped in a ContainerRequestFilter to make sure the user is logged in:
@Provider
@Priority(Priorities.AUTHORIZATION)
public class AuthenticationRequestFilter implements ContainerRequestFilter {
@SessionScoped
@Inject
SessionBean sessionBean;
@Inject
HttpSession httpSession;
@Context
ResourceInfo resourceInfo;
@Override
public void filter(ContainerRequestContext crc) throws IOException {
// method annotated @PermitAll is for login
if (resourceInfo.getResourceMethod().isAnnotationPresent(PermitAll.class)) {
return;
}
if (httpSession.isNew() || sessionBean.getJessionId() == null) {
crc.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
}
}
My question: is it safe and is the @SessionScoped + JESSIONID is enough to protect the server?
Note: I use Quarkus 2.6.2 + Undertow.