Created
August 4, 2021 01:23
-
-
Save erdemaydin/f9d217236f524602e312c6c0f5d63eed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomerFilter implements Filter { | |
| private static final List<String> EXCLUDE_URI_LIST = Arrays.asList("/product", "/order", "/user"); | |
| private static final Short INVALID_CUSTOMER_STATUS = Short.valueOf("-1"); | |
| @Override | |
| public void init(FilterConfig filterConfig) throws ServletException { } | |
| @Override | |
| public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | |
| HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; | |
| HttpServletResponse httpResponse = (HttpServletResponse) servletResponse; | |
| String requestURI = httpRequest.getRequestURI(); | |
| if (EXCLUDE_URI_LIST.stream().anyMatch(requestURI::startsWith)) { | |
| httpResponse.sendRedirect("/error.html"); | |
| httpResponse.setHeader("HELLO", "WORLD"); | |
| httpResponse.setStatus(HttpStatus.NOT_ACCEPTABLE.value()); | |
| filterChain.doFilter(httpRequest, httpResponse); | |
| } | |
| HttpSession httpSession = httpRequest.getSession(); | |
| Customer customer = (Customer) httpSession.getAttribute("customer"); | |
| if (customer == null || INVALID_CUSTOMER_STATUS.equals(customer.getStatus())) { | |
| httpResponse.sendRedirect("/unauthorzed.html"); | |
| httpResponse.setStatus(HttpStatus.UNAUTHORIZED.value()); | |
| filterChain.doFilter(httpRequest, httpResponse); | |
| } | |
| String customerId = String.valueOf(customer.getId()); | |
| Cookie cookie = new Cookie("customer", customerId); | |
| httpResponse.addCookie(cookie); | |
| filterChain.doFilter(httpRequest, httpResponse); | |
| } | |
| @Override | |
| public void destroy() { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment