We would like to hide some code features based on user login in Tomcat. We are using the basic authentications. Any suggestions?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
IF what you meant was just hiding some resources depending on whether the user is logged in or not then it is just a matter of restricting access to some pages (see the references below).
IF you want to hide some feature based on the who is logged in, then one of the solutions is to check the user role right inside JSP and output the content accordingly.
Primitive example:
sample.jsp
NB!
To be able to invoke methods with arguments using EL you must use minimum Servlet version 3.
Quote from here: https://stackoverflow.com/tags/el/info
Another way to hide / restrict access to some of your pages depending on the user role is to make security configurations in web.xml, or use annotations (minimum Java EE 5), or create your own Filter that checks the role of the user making request.
To create your own Filter, create a class that implements javax.servlet.Filter interface and in the doFilter() method check the role of the user that made a request by using HttpServletRequest method isUserInRole().
Here is a simple example of implementing custom Filter:
RoleCheckFilter.java
Add the appropriate filter configuration in web.xml:
Of course in your case, considering the fact that you use Basic Authentication, it is much easier to make security configurations right in web.xml (declarative security) or use programmatic security.
Quote from the official Java EE documentation:
Check out official Java EE documentation related to securing Java EE applications (in your case pay attention to Specifying an Authorization Constraint part):
Java EE 6: Securing Web Applications
Java EE 5: Securing Web Applications
Check out also examples from the official documentation:
Java EE 6. Examples: Securing Web Applications
Java EE 5. Examples: Securing Web Applications