1. 程式人生 > >springboot學習之攔截器配置

springboot學習之攔截器配置

package com.wanghp.demo.configurer;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
public class DemoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("----------使用介面實現的方式實現(在請求發生之前執行)---------");

        return true;// 只有返回true才會繼續向下執行,返回false取消當前請求
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("----------使用介面實現的方式實現(在請求發生之後執行)---------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("----------使用介面實現的方式實現(在請求發生之後執行),但是在DispatcherServlet 渲染了對應的檢視之後執行(主要是用於進行資源清理工作)---------");
    }


}
b、建立配置類,並註冊攔截器
package com.wanghp.demo.configurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {


     public void addInterceptors(InterceptorRegistry registry){
         // 多個攔截器組成一個攔截器鏈
         // addPathPatterns 用於新增攔截規則
         // excludePathPatterns 使用者排除攔截
         registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/**");
         //registry.addInterceptor(new DemoInterceptor1()).addPathPatterns("/**");
         super.addInterceptors(registry);
     }

}

c、建立測試Controller,並在瀏覽器進行訪問測試。
package com.wanghp.demo.spring.controller;

import com.wanghp.demo.spring.commontest.readconfig.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/8
 */
@RestController
@RequestMapping("/testController")
public class TestController {
    @GetMapping("/testController")
    public String test(){
        return "test";
    }

}
d、測試結果如下
訪問:http://localhost:8080/demo/testController/testController 結果:
方式二:通過繼承HandlerInterceptorAdaptor類來實現。
a、建立DemoInterceptor1,然後繼承HandlerInterceptorAdaptor
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * description: 使用繼承的方式實現攔截器,選擇性的重寫相關的類即可
 * created by wanghaiping
 * created date 2017/12/10
 */
public class DemoInterceptor1 extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("----------使用繼承類的方式實現(在請求發生之前執行)---------");
        return true;// 只有返回true才會繼續向下執行,返回false取消當前請求
    }




}


b、建立配置類,並註冊攔截器
package com.wanghp.demo.configurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/10
 */
@Configuration
public class DemoWebMvcConfigurer extends WebMvcConfigurerAdapter {


     public void addInterceptors(InterceptorRegistry registry){
         // 多個攔截器組成一個攔截器鏈
         // addPathPatterns 用於新增攔截規則
         // excludePathPatterns 使用者排除攔截
         //registry.addInterceptor(new DemoInterceptor()).addPathPatterns("/**");
         registry.addInterceptor(new DemoInterceptor1()).addPathPatterns("/**");
         super.addInterceptors(registry);
     }

}

c、建立測試Controller,並在瀏覽器進行訪問測試。
package com.wanghp.demo.spring.controller;

import com.wanghp.demo.spring.commontest.readconfig.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description:
 * created by wanghaiping
 * created date 2017/12/8
 */
@RestController
@RequestMapping("/testController")
public class TestController {
    @GetMapping("/testController")
    public String test(){
        return "test";
    }

}
d、測試結果如下
訪問:http://localhost:8080/demo/testController/testController 結果: