- @TransportProtected, a new annotation indicates whether the transport is confidential or none.
- @DenyAll will also be available at the TYPE level.
The mapping of the @DenyAll, @PermitAll, @RolesAllowed and @TransportProtected to security constraint are described in Chapter 13.4.1 of Servlet 3.0 specification. These annotations can be applied to:
- the servlet class
- one of the following methods in HttpServlet:
- doDelete
- doGet
- doHead
- doOptions
- doPost
- doPut
- doTrace
In this blog, we illustrate the usages of these annotations by examples.
Example 1: Type Level
@WebServlet("/myurl")
@RolesAllowed("javaee")
public class TestServlet extends HttpServlet {
...
}
In this case, all http methods are protected and accessible only by users with role javaee.
Example 2: Method Level
@WebServlet("/myurl")
public class TestServlet extends HttpServlet {
@PermitAll
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
@RolesAllowed("javaee")
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
@DenyAll
protected void doTrace(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
}
The behaviors of the above servlet can be summarized as follows:
| Http method | Behavior |
|---|---|
| GET | all can access GET method |
| POST | only authenticated users with role javaee can access POST method |
| TRACE | no one can access TRACE method |
Example 3: Type and Method Level
@WebServlet("/myurl")
@RolesAllowed("javaee")
public class TestServlet extends HttpServlet {
...
@RolesAllowed("staff")
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
@PermitAll
protected void doTrace(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
}
The behaviors of the above servlet can be summarized as follows:
| Http method | Behavior |
|---|---|
| POST | only authenticated users with role staff can access POST method |
| TRACE | all can access TRACE method |
| methods other than POST and TRACE | only authenticated users with role javaee can access |
Example 4: @TransportProtected and @RolesAllowed
@WebServlet("/myurl")
@TransportProtected
public class TestServlet extends HttpServlet {
...
@TransportProtected(false)
@RolesAllowed("javaee")
protected void doTrace(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
...
}
}
The behaviors of the above servlet can be summarized as follows:
| Http method | Behavior |
|---|---|
| TRACE | no https, only authenticated users with role javaee can access TRACE method |
| methods other than TRACE | require https |
Posted by Rajiv Mordani's Blog on May 08, 2009 at 01:58 PM PDT #
Posted by Arun Gupta's Blog on May 19, 2009 at 11:01 AM PDT #
Posted by Arun Gupta's Blog on May 20, 2009 at 06:16 PM PDT #
Since May 2009, Servlet 3.0 Security Annotations have been changed a lot. I have another blog, "Follow up on Servlet Security 3.0 Annotations", http://blogs.sun.com/swchan/entry/follow_up_on_servlet_3 .
Posted by Shing Wai Chan on October 29, 2009 at 02:21 PM PDT #