3
You can help me implement a java filter access control?
This was the implementation I tried to do so far, but I couldn’t make it work.
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String uri = req.getRequestURI();
String usuario = getUsuario(req);
if((usuario != null)
||(req.getRequestURI().endsWith("/WEB-INF/adm.jsp"))
||(req.getRequestURI().endsWith("/WEB-INF/alterar.jsp"))){
}else{
req.getRequestDispatcher("index.html").forward(request, response);
}
chain.doFilter(request, response);
}
private String getUsuario(HttpServletRequest req) {
Usuario usuario = (Usuario) req.getSession().getAttribute("usuarioLogado");
if(usuario==null)
return "<deslogado>";
return usuario.getUsuario();
}
my filter is configured as follows:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String uri = req.getRequestURI();
String usuario = getUsuario(req);
if((usuario != null)||(req.getRequestURI().endsWith("/adm.jsp"))){
}else{
req.getRequestDispatcher("index.jsp").forward(request, response);
}
System.out.println("Usuario " + usuario + " acessando a URI " + uri);
/**eliminando o cache dos formularios*/
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.setHeader("Cache-Control","no-cache, no-store, must-revalidate");
httpResponse.setHeader("Pragma","no-cache");
httpResponse.setDateHeader("Expires", 0);
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
private String getUsuario(HttpServletRequest req) {
Usuario usuario = (Usuario) req.getSession().getAttribute("usuarioLogado");
if(usuario==null)
return "<deslogado>";
return usuario.getUsuario();
}
}
Good afternoon helped a lot, but my filter is already configured, the difficulty is to give a Re-turn after having dropped, and still have access to content.
– Celso Andre
You are invalidating the logout session?
– Nelson Aguiar
validation this ok, the problem is that I can access after undoing
– Celso Andre
Put the code in which you logout.
– Nelson Aguiar
Good morning Nelson, posted the Dofilter method., if you need anything let me know. Thank you very much.
– Celso Andre
Logout code looks like this: @Override protected void doPost(Httpservletrequest request, Httpservletresponse Response) throws Servletexception, Ioexception { request.getSession(). removeAttribute("userLog"); Response.sendRedirect("index.jsp"); }
– Celso Andre
So when you log out the correct one would be to invalidate the session via the method Session.invalidate(); tries to replace the line where you remove the attribute with Httpsession Session = request.getSession(false); Session.invalidate(); and see if it works.
– Nelson Aguiar