In this section, you will learn about how to specifying security for basic authentication using @ServletSecurity.
In a multitier enterprise application, several containers are needed to deploy various components of Enterprise tiers. These container also provide security to these components. Two types of security is provided by the container :
@ServletSecurity annotation is used to define security for a Servlet. This annotation offers alternative mechanism which you could achieve through <security-constraint> elements of deployment descriptor or through ServletRegistration interface's setServletSecurity method ()(programmatically).
This method provide us ability to define specific constraints on HTTP functions. Instead of this, you can also define more general constraint that suits to all HTTP methods.
Inside @ServletSecurity annotation, you can implement the following annotations :
The common elements of @HttpMethodConstraint and @HttpConstraint annotations are given below :
Given below the code in which @ServletSecurity annotation is used for defining authentication :
@WebServlet(name = "UserServlet", urlPatterns = {"/user"})
@ServletSecurity(
@HttpConstraint(transportGuarantee = TransportGuarantee.CONFIDENTIAL,
rolesAllowed = {"TutorialUser"}))
The above annotation specifies that only authorized user will be able to access the /user URL, who(user) has verified role as TutorialUser. And to protect user name and password data, it will be transported securely during transit.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Specifying Security for Basic Authentication using @ServletSecurity
Post your Comment