跨域

拦截器:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * jsonp post提交处理拦截器
 */
public class PostRequestInterceptor implements HandlerInterceptor {

	private Logger logger = LoggerFactory.getLogger(PostRequestInterceptor.class);

	private String INDEX_PAGE;

	public PostRequestInterceptor(String indexPage) {
		this.INDEX_PAGE = indexPage;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String url = request.getRequestURI();

		//logger.info("Parameter : {}",request.getParameterMap());
		//logger.info("PostRequestInterceptor.preHandle execute url|  " + url  + " | ------------- end");
		//response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		//(request.getHeader("Origin").contains("127")) {
		String origin = request.getHeader("Origin");
//		if (StringUtil.isNotBlank(INDEX_PAGE)) {
//			origin = origin + "," + INDEX_PAGE;
//		}
		response.setHeader("Access-Control-Allow-Origin", origin);
		//}
		response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers,X-Requested-With,X_Requested_With");
		response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
		//	response.addHeader("Access-Control-Max-Age", "1000");
		if ("IE".equals(request.getParameter("type"))) {
			response.addHeader("XDomainRequestAllowed", "1");
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {


	}
}

 

 

添加拦截器:

import javax.annotation.PostConstruct;
import PostRequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

	@Autowired
	private RequestMappingHandlerAdapter handlerAdapter;

	@Value("${constant.indexPage}")
	private String indexPage;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new PostRequestInterceptor(indexPage)).addPathPatterns("/**");
		super.addInterceptors(registry);
	}

	/**
	 * 增加字符串转日期的功能
	 */
	@PostConstruct
	public void initEditableValidation() {
		ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
				.getWebBindingInitializer();
		if (initializer.getConversionService() != null) {
			GenericConversionService genericConversionService = (GenericConversionService) initializer
					.getConversionService();
			genericConversionService.addConverter(new WebDateConverter());
		}

	}

}

 

posted @ 2019-04-19 09:19  wanhua.wu  阅读(150)  评论(0编辑  收藏  举报