My question is very similar to this question
I am trying to use JAX-RS framework with Jersey along with JSF. I understand I can run them both together on the same app but mapped to different locations (which I have now)
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Rest Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
What I want to do is similar to this where users can access the REST API, but only get back data that they are allowed to see based on their role. For example, if they are a teacher they can go to foo.com/api/students
and will only get a json response that holds students that are in their classes, not all students in the school.
My issue is that users login using foo.com/login.xhtml
, which is under /*.xhtml
, so their role is managed by the JSF session. I think this means that the REST API at /api/students
has no way of knowing what user is logged in, who is trying to access it, and therefore what results it should display. It can't even redirect users who aren't logged in to the login page if they just put the url foo.com/api/students
into their web browser, it can only display all of the students.
I hope I am mistaken in thinking this and there is a way to know who is logged in, protect my /api/students
REST API from people that aren't logged in, and even return the right data based on their user role if they are logged in.
Please help me understand if what I am trying to do is possible. Thank you.