Guest User

Untitled

a guest
Jan 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package wad.accesscontrol;
  6.  
  7. import java.io.IOException;
  8. import javax.servlet.Filter;
  9. import javax.servlet.FilterChain;
  10. import javax.servlet.FilterConfig;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.ServletRequest;
  13. import javax.servlet.ServletResponse;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. /**
  18.  *
  19.  * @author jonne
  20.  */
  21. public class AccessControlFilter implements Filter {
  22.  
  23.     private FilterConfig filterConfig;
  24.    
  25.     @Override
  26.     public void init(FilterConfig fc) throws ServletException {
  27.         this.filterConfig = fc;
  28.        
  29.     }
  30.  
  31.     @Override
  32.     public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
  33.         HttpServletRequest httpRequest = (HttpServletRequest) sr;
  34.         HttpServletResponse httpResponse = (HttpServletResponse) sr1;
  35.         String url = httpRequest.getRequestURI();
  36.         if(url.contains("login")) {
  37.             String username = (String)httpRequest.getParameter("username");
  38.             String password = (String)httpRequest.getParameter("password");
  39.             if(username != null && password != null && username.equals("username") && password.equals("password")) {
  40.                 httpRequest.getSession().setAttribute("authenticated", (Boolean)true);
  41.                 httpResponse.sendRedirect(httpRequest.getContextPath()+"/app/secret");
  42.             }
  43.         }
  44.         else {
  45.             if(httpRequest.getSession().getAttribute("authenticated") == null) {
  46.                 httpResponse.sendRedirect(httpRequest.getContextPath()+"/denied.jsp");
  47.             }
  48.             else {
  49.                 fc.doFilter(httpRequest, httpResponse);
  50.             }
  51.         }
  52.            
  53.     }
  54.  
  55.     @Override
  56.     public void destroy() {
  57.        
  58.     }
  59.    
  60. }
Add Comment
Please, Sign In to add comment