swagger 资源映射配置
This commit is contained in:
parent
1d7e2753d4
commit
2c65a03a4f
@ -1,31 +1,24 @@
|
|||||||
package com.rymcu.vertical.config;
|
package com.rymcu.vertical.config;
|
||||||
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||||||
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
||||||
import com.rymcu.vertical.core.result.GlobalResult;
|
|
||||||
import com.rymcu.vertical.jwt.aop.RestAuthTokenInterceptor;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
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.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,42 +68,23 @@ public class WebMvcConfigurer extends WebMvcConfigurationSupport {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
@Override
|
||||||
* 3. 混合密钥(secret)进行md5获得签名,与请求的签名进行比较
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
*/
|
/**
|
||||||
private boolean validateSign(HttpServletRequest request) {
|
* SpringBoot自动配置本身并不会把/swagger-ui.html
|
||||||
String requestSign = request.getParameter("sign");//获得请求签名,如sign=19e907700db7ad91318424a97c54ed57
|
* 这个路径映射到对应的目录META-INF/resources/下面
|
||||||
if (StringUtils.isEmpty(requestSign)) {
|
* 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
|
||||||
return false;
|
*/
|
||||||
}
|
registry.addResourceHandler("swagger-ui.html")
|
||||||
List<String> keys = new ArrayList<String>(request.getParameterMap().keySet());
|
.addResourceLocations("classpath:/META-INF/resources/");
|
||||||
keys.remove("sign");//排除sign参数
|
|
||||||
Collections.sort(keys);//排序
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
registry.addResourceHandler("/webjars/**")
|
||||||
for (String key : keys) {
|
.addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||||
sb.append(key).append("=").append(request.getParameter(key)).append("&");//拼接字符串
|
//将所有/static/** 访问都映射到classpath:/static/ 目录下
|
||||||
}
|
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
|
||||||
String linkString = sb.toString();
|
super.addResourceHandlers(registry);
|
||||||
linkString = StringUtils.substring(linkString, 0, linkString.length() - 1);//去除最后一个'&'
|
|
||||||
|
|
||||||
String secret = "Potato";//密钥,自己修改
|
|
||||||
String sign = DigestUtils.md5Hex(linkString + secret);//混合密钥md5
|
|
||||||
|
|
||||||
return StringUtils.equals(sign, requestSign);//比较
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user