Security context
The security context object is used to programmatically check a user's authority to access a specific resource. This is very useful when you need to do some custom behavior upon having an invalid request from the user, rather than the default one specified by the security API.
In the following example, we're going to forward the user to another page if, and only if, they have access to this page:
@WebServlet("/home") public class HomeServlet extends HttpServlet { @Inject private SecurityContext securityContext; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (securityContext.hasAccessToWebResource("/anotherServlet", "GET")) { req.getRequestDispatcher("/anotherServlet").forward(req, res); } else { req.getRequestDispatcher("/logout").forward(req, res); } } }
As you can see, we have used the CDI's...