117 lines
4.7 KiB
Java
117 lines
4.7 KiB
Java
package com.rymcu.vertical.config;
|
||
|
||
|
||
import com.alibaba.fastjson.JSON;
|
||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||
import com.rymcu.vertical.core.result.GlobalResult;
|
||
import com.rymcu.vertical.jwt.aop.RestAuthTokenInterceptor;
|
||
import org.apache.commons.codec.digest.DigestUtils;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.context.annotation.Bean;
|
||
import org.springframework.context.annotation.Configuration;
|
||
import org.springframework.http.MediaType;
|
||
import org.springframework.http.converter.HttpMessageConverter;
|
||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import java.io.IOException;
|
||
import java.nio.charset.Charset;
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.Collections;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* Spring MVC 配置
|
||
*/
|
||
@Configuration
|
||
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
|
||
|
||
private final Logger logger = LoggerFactory.getLogger(WebMvcConfigurer.class);
|
||
// @Value("${env}")
|
||
// private String env;//当前激活的配置文件
|
||
|
||
@Override
|
||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
||
FastJsonConfig config = new FastJsonConfig();
|
||
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
|
||
SerializerFeature.WriteNullStringAsEmpty);//String null -> ""
|
||
//SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
|
||
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect); //关闭循环引用
|
||
converter.setFastJsonConfig(config);
|
||
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
|
||
converter.setDefaultCharset(Charset.forName("UTF-8"));
|
||
converters.add(0, converter);
|
||
}
|
||
|
||
//解决跨域问题
|
||
@Override
|
||
public void addCorsMappings(CorsRegistry registry) {
|
||
registry.addMapping("/**")
|
||
.allowedOrigins("*")
|
||
.allowCredentials(true)
|
||
.allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH");
|
||
}
|
||
|
||
@Bean
|
||
public RestAuthTokenInterceptor restAuthTokenInterceptor() {
|
||
return new RestAuthTokenInterceptor();
|
||
}
|
||
|
||
//添加拦截器
|
||
@Override
|
||
public void addInterceptors(InterceptorRegistry registry) {
|
||
// TODO 先不拦截接口,进行测试
|
||
registry.addInterceptor(restAuthTokenInterceptor()).addPathPatterns("/api/**")
|
||
.excludePathPatterns("/api/v1/console/**","/api/v1/article/articles/**","/api/v1/article/detail/**","/api/v1/topic/**");
|
||
|
||
}
|
||
|
||
private void responseResult(HttpServletResponse response, GlobalResult result) {
|
||
response.setCharacterEncoding("UTF-8");
|
||
response.setHeader("Content-type", "application/json;charset=UTF-8");
|
||
response.setStatus(200);
|
||
try {
|
||
response.getWriter().write(JSON.toJSONString(result));
|
||
} catch (IOException ex) {
|
||
logger.error(ex.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 一个简单的签名认证,规则:
|
||
* 1. 将请求参数按ascii码排序
|
||
* 2. 拼接为a=value&b=value...这样的字符串(不包含sign)
|
||
* 3. 混合密钥(secret)进行md5获得签名,与请求的签名进行比较
|
||
*/
|
||
private boolean validateSign(HttpServletRequest request) {
|
||
String requestSign = request.getParameter("sign");//获得请求签名,如sign=19e907700db7ad91318424a97c54ed57
|
||
if (StringUtils.isEmpty(requestSign)) {
|
||
return false;
|
||
}
|
||
List<String> keys = new ArrayList<String>(request.getParameterMap().keySet());
|
||
keys.remove("sign");//排除sign参数
|
||
Collections.sort(keys);//排序
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
for (String key : keys) {
|
||
sb.append(key).append("=").append(request.getParameter(key)).append("&");//拼接字符串
|
||
}
|
||
String linkString = sb.toString();
|
||
linkString = StringUtils.substring(linkString, 0, linkString.length() - 1);//去除最后一个'&'
|
||
|
||
String secret = "Potato";//密钥,自己修改
|
||
String sign = DigestUtils.md5Hex(linkString + secret);//混合密钥md5
|
||
|
||
return StringUtils.equals(sign, requestSign);//比较
|
||
}
|
||
}
|