1. 程式人生 > >在springboot專案中使用攔截器

在springboot專案中使用攔截器

最近練習了springboot專案,作為java開發人員,攔截器是必不可少的,我們用到的最多的用途就是進行使用者登入狀態的攔截,日誌的攔截等。在此記錄一下。

首先springboot專案集成了springmvc框架,我使用到的也是springmvc框架的攔截器。

建立一個URLInterceptor類實現HandlerInterceptor,程式碼如下:

/**
 * 攔截器攔截請求
 */

import com.springboot.first.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//重寫方法中內容根據具體專案來
public class URLInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI();
        if(uri.contains("wechat")||uri.contains("api")||uri.contains("wxuser")){
            return true;
        }
        User user = (User)request.getSession().getAttribute("user");
        if(!uri.contains("login")){
            if(user==null){
                response.sendRedirect("/user/tologin");
                return false;
            }
        }else{
            if(user!=null){
                response.sendRedirect("/main/index");
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null){
            if(! (modelAndView.getView() instanceof RedirectView)){
                String basePath = request.getContextPath();
                //modelAndView.addObject("basePath",basePath);
                request.setAttribute("basePath",basePath);
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

然後將URLInterceptor攔截器新增到springboot的配置中,程式碼如下:

import com.springboot.first.interceptor.URLInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Title:
 * @author: chenmingyue
 * @date: 2018/3/16 12:01
 * Description:配置URLInterceptor攔截器,以及攔截路徑
 */
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用於新增攔截規則
        // excludePathPatterns 使用者排除攔截
        registry.addInterceptor(new URLInterceptor()).addPathPatterns("/*/*");
        super.addInterceptors(registry);
    }
}

在ssm專案中也可通過xml配置URLInterceptor攔截器,具體配置在spring-mvc.xml

	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.springboot.first.interceptor.URLInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>