1
How do I control the access of pages with Java? I have a filter already implemented, which controls whether the user is logged in or not.
However, I have a page that I need to check the user’s permission... The page will only be accessed if the user is an administrator.
When the user is accessing the processing page and is not an ADMINISTRATOR user, the page should not be accessed.
I want to know how to do this in the filter part in Java. My code is this:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String context = request.getServletContext().getContextPath();
try{
HttpSession session = ((HttpServletRequest)request).getSession();
User user = null;
if(session != null){
user = (User) session.getAttribute("user");
}
if(user == null){
HttpServletResponse resp =((HttpServletResponse)response);
resp.sendRedirect(context + "/");
} else {
chain.doFilter(request, response);
JPAUtil.closeEntityManager();
}
}catch(Exception e){
e.printStackTrace();
}
}
Only this way, it will give Resupply.sendError(400) to all pages... But this is only to happen if it is the treatment page.
– Guilherme Nass
So you should do the Filter mapping, sorry, think you would understand, I’ll edit the answer
– RickPariz