Spring Security之旅————————————restful风格接口简述,过滤器配置(2)

restful接口的完成度简介 , 分为四级

Spring Security之旅————————————restful风格接口简述,过滤器配置(2)_第1张图片

Spring Security之旅————————————restful风格接口简述,过滤器配置(2)_第2张图片

Spring Security之旅————————————restful风格接口简述,过滤器配置(2)_第3张图片

利用三种方式拦截接口请求 

Filter

package com.sola.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.stereotype.Component;

@Component
public class TimeFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("TIME FILTER DESTROY-------------------");
	}

	@Override
	public void doFilter(ServletRequest reguest, ServletResponse response, FilterChain filterchain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("my filter start");
		
		//在这就可以操作一下请求参数了
		
		//这个就是过滤器链,让两个进入下一道请求
		filterchain.doFilter(reguest, response);
		
		System.out.println("my filter ok~");
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("TIME FILTER DESTROY-------------------");
	}

}

有时候也会用到第三方的过滤器,有的可能没法加@Component注解

也可以用配置来实现 把上面的@Component注解删了 ,在这加配置

package com.sola.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sola.filter.TimeFilter;

@Configuration
public class MainConfig {

	@Bean
	public TimeFilter timefilter(){
		return new TimeFilter();
	}
}

再进阶一下下,可以用springboot提供的过滤器管理类管理

https://blog.csdn.net/doctor_who2004/article/details/56055505?utm_source=tuicool&utm_medium=referral

package com.sola.config;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sola.filter.TimeFilter;

@Configuration
public class MainConfig {

	@Bean
	public FilterRegistrationBean timeFilter(){
		
		FilterRegistrationBean filterregistrationbean = new FilterRegistrationBean<>();
		
		TimeFilter timeFilter = new TimeFilter();
		
		filterregistrationbean.setFilter(timeFilter);
		
		List url = new ArrayList<>();
		
		url.add("/*");
		
		filterregistrationbean.setUrlPatterns(url);
		
		return filterregistrationbean;
	}
}

还可以加入多个过滤器并排序(貌似这样不太好,但是还是先这样,因为是一个集合类,应该都可以加入在里面)

这样就可以过滤一些IP或者什么请求

package com.sola.config;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sola.filter.FristFilter;
import com.sola.filter.TimeFilter;

@Configuration
public class MainConfig {

	@Bean
	public FilterRegistrationBean timeFilter(){
		
		FilterRegistrationBean filterregistrationbean = new FilterRegistrationBean<>();
		
		TimeFilter timeFilter = new TimeFilter();
		
		filterregistrationbean.setFilter(timeFilter);
		
		List url = new ArrayList<>();
		
		url.add("/*");
		
		filterregistrationbean.setUrlPatterns(url);
		
		filterregistrationbean.setOrder(2);
		
		return filterregistrationbean;
	}
	
	@Bean
	public FilterRegistrationBean fristFilter(){
		
		FilterRegistrationBean filterregistrationbean = new FilterRegistrationBean<>();
		
		FristFilter fristfilter = new FristFilter();
		
		filterregistrationbean.setFilter(fristfilter);
		
		List url = new ArrayList<>();
		
		url.add("/*");
		
		filterregistrationbean.setUrlPatterns(url);
		
		filterregistrationbean.setOrder(1);
		
		return filterregistrationbean;
	}
}

结构

Spring Security之旅————————————restful风格接口简述,过滤器配置(2)_第4张图片

Interceptor

package com.sola.interceptor;

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

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class TimeInterceptor implements HandlerInterceptor {
	
	//里面一共有三个方法
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// preHandle 在真正的去调用某一个Controller中的接口之前调用
		
		System.err.println("------------preHandle-------------");
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}
	
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// postHandle处理中调用  但是假如 接口中出现了异常则不调用
		
		System.err.println("------------postHandle-------------");
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// afterCompletion处理完成后 调用
		
		System.err.println("------------afterCompletion-------------");
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}

}

上面加入了@Component 但是 没有效果,光加入这一个注解是不器作用的 还需要在配置类里配置

package com.sola.config;

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

import com.sola.interceptor.TimeInterceptor;

@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter{
	
	@Autowired
	private TimeInterceptor timeinterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		
		registry.addInterceptor(timeinterceptor);
		
		super.addInterceptors(registry);
	}
}

要配置类要继承WebMvcConfigurerAdapter 然后再在registry加入编写的interceptor拦截器就OK了,不过这样也发现了 没有aop切面拦截器方便

你可能感兴趣的:(Security)