From 2c65a03a4fe3019ed985b3677e2921edfb71584d Mon Sep 17 00:00:00 2001 From: ronger Date: Mon, 2 Dec 2019 09:15:37 +0800 Subject: [PATCH] =?UTF-8?q?swagger=20=E8=B5=84=E6=BA=90=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vertical/config/WebMvcConfigurer.java | 62 ++++++------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java b/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java index 593d5d0..4987214 100644 --- a/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java +++ b/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java @@ -1,31 +1,24 @@ 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.util.ResourceUtils; import org.springframework.web.servlet.config.annotation.CorsRegistry; 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 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; /** @@ -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) - * 3. 混合密钥(secret)进行md5获得签名,与请求的签名进行比较 - */ - private boolean validateSign(HttpServletRequest request) { - String requestSign = request.getParameter("sign");//获得请求签名,如sign=19e907700db7ad91318424a97c54ed57 - if (StringUtils.isEmpty(requestSign)) { - return false; - } - List keys = new ArrayList(request.getParameterMap().keySet()); - keys.remove("sign");//排除sign参数 - Collections.sort(keys);//排序 + * 访问静态资源 + * */ + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + /** + * SpringBoot自动配置本身并不会把/swagger-ui.html + * 这个路径映射到对应的目录META-INF/resources/下面 + * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布; + */ + registry.addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/"); - 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);//比较 + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); + //将所有/static/** 访问都映射到classpath:/static/ 目录下 + registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/"); + super.addResourceHandlers(registry); } }