Advertisement
vim_fans

Untitled

Sep 12th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. package Filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.annotation.WebFilter;
  12. import javax.servlet.http.HttpServletRequest;
  13.  
  14.  
  15. @WebFilter(urlPatterns = "*.do")
  16. public class LoginRequiredFilter implements Filter {
  17.  
  18. // private static final Logger logger =
  19. // LogManager.getLogger(LoginRequiredFilter.class);
  20. // private static final Logger logger =
  21. // LogManager.getLogger("Filter.LoginRequiredFilter");
  22. // private static final Logger logger = LogManager.getLogger();
  23.  
  24. public void destroy() {
  25.  
  26. }
  27.  
  28. @Override
  29. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
  30. throws IOException, ServletException {
  31.  
  32. HttpServletRequest request = (HttpServletRequest) servletRequest;
  33. request.setCharacterEncoding("UTF-8");
  34. servletResponse.setContentType("text/plain");
  35. servletResponse.setCharacterEncoding("UTF-8");
  36. String path = request.getRequestURI();
  37. if (path.matches("^/admin.*$")) {
  38. if (path.equals("/adminsignup.do")) {
  39.  
  40. chain.doFilter(servletRequest, servletResponse);
  41. } else {
  42. if (request.getSession().getAttribute("admin") != null) {
  43.  
  44. chain.doFilter(servletRequest, servletResponse);
  45.  
  46. } else {
  47. request.getRequestDispatcher("/adminlogin.do").forward(servletRequest, servletResponse);
  48. }
  49. }
  50. }
  51. else if (path.equals("/signup.do")) {
  52.  
  53. chain.doFilter(servletRequest, servletResponse);
  54. } else {
  55. if (request.getSession().getAttribute("name") != null) {
  56. chain.doFilter(servletRequest, servletResponse);
  57.  
  58. } else {
  59. request.getRequestDispatcher("/login.do").forward(servletRequest, servletResponse);
  60. }
  61. }
  62.  
  63. }
  64.  
  65. @Override
  66. public void init(FilterConfig arg0) throws ServletException {
  67.  
  68. }
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. package Filter;
  91.  
  92. import java.io.IOException;
  93.  
  94. import javax.servlet.Filter;
  95. import javax.servlet.FilterChain;
  96. import javax.servlet.FilterConfig;
  97. import javax.servlet.ServletException;
  98. import javax.servlet.ServletRequest;
  99. import javax.servlet.ServletResponse;
  100. import javax.servlet.annotation.WebFilter;
  101. import javax.servlet.http.HttpServletRequest;
  102. import javax.servlet.http.HttpServletResponse;
  103.  
  104. @WebFilter(urlPatterns = "/views/*")
  105. public class AuthFilter implements Filter {
  106.  
  107. @Override
  108. public void destroy() {
  109.  
  110. }
  111.  
  112. @Override
  113. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
  114. throws IOException, ServletException {
  115. HttpServletRequest request = (HttpServletRequest) servletRequest;
  116. HttpServletResponse response = (HttpServletResponse) servletResponse;
  117. request.setCharacterEncoding("UTF-8");
  118. servletResponse.setContentType("text/plain");
  119. servletResponse.setCharacterEncoding("UTF-8");
  120. String path = request.getRequestURI();
  121. if (path.matches("^\\/views\\/back-end\\/.*")) {
  122. if (request.getSession().getAttribute("admin") != null) {
  123.  
  124. chain.doFilter(servletRequest, servletResponse);
  125.  
  126. } else {
  127. response.sendRedirect("/adminlogin.do");
  128. }
  129.  
  130. } else {
  131. if (request.getSession().getAttribute("name") != null) {
  132.  
  133. chain.doFilter(servletRequest, servletResponse);
  134.  
  135. } else {
  136. response.sendRedirect("/login.do");
  137. }
  138. }
  139.  
  140. }
  141.  
  142. @Override
  143. public void init(FilterConfig arg0) throws ServletException {
  144.  
  145. }
  146.  
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement