interceptors.general.GeneralInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for interceptors.general.GeneralInterceptor.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interceptors.general;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 *
 * @author caoc1
 */
public class GeneralInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (request.getRequestURI().contains("all") || request.getRequestURI().contains("index")
                || request.getRequestURI().contains("Login") || request.getRequestURI().contains("Logout")
                || request.getRequestURI().contains("end")) {
            return true;
        }
        if (request.getSession().getAttribute("loggedUser").toString().equals("admin")) {
            if (request.getRequestURI().contains("admin")) {
                return true;
            }
        } else {
            if (request.getRequestURI().contains("user") || request.getRequestURI().contains("post")) {
                return true;
            }
        }
        request.getSession().invalidate();
        response.sendRedirect("loginPage.html");
        return false;
    }
}