diff --git a/README.md b/README.md index af6062c..c2b10a7 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,11 @@ > 前端地址:[https://github.com/wang926454/VueStudy/tree/master/VueStudy08-JWT](https://github.com/wang926454/VueStudy/tree/master/VueStudy08-JWT) -#### Issues +#### 疑问查看 1. [#14 重复请求会不会生成多个token](https://github.com/dolyw/ShiroJwt/issues/14) 2. [#19 跨域sso问题](https://github.com/dolyw/ShiroJwt/issues/19) -3. [#22 如果是微服务的话,是不是每个微服务都的写一套这样的shiro?](https://github.com/dolyw/ShiroJwt/issues/22) +3. [#29 Token刷新并发处理](https://github.com/dolyw/ShiroJwt/issues/29) diff --git a/src/main/java/com/wang/config/OriginFilter.java b/src/main/java/com/wang/config/OriginFilter.java new file mode 100644 index 0000000..731fbc8 --- /dev/null +++ b/src/main/java/com/wang/config/OriginFilter.java @@ -0,0 +1,37 @@ +package com.wang.config; + +import org.apache.shiro.web.util.WebUtils; +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * 全局跨域放开 + * + * @author wliduo[i@dolyw.com] + * @date 2019/11/26 14:29 + */ +@Component +public class OriginFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) throws ServletException { } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest httpServletRequest = WebUtils.toHttp(request); + HttpServletResponse httpServletResponse = WebUtils.toHttp(response); + httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE"); + httpServletResponse.setHeader("Access-Control-Max-Age", "3600"); + httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin")); + httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers")); + filterChain.doFilter(request, response); + } + + @Override + public void destroy() { } + +} diff --git a/src/main/java/com/wang/config/WebMvcConfig.java b/src/main/java/com/wang/config/WebMvcConfig.java deleted file mode 100644 index 53c9483..0000000 --- a/src/main/java/com/wang/config/WebMvcConfig.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wang.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -/** - * 登录接口跨域放开 - * - * @author wliduo[i@dolyw.com] - * @date 2019/11/26 14:29 - */ -@Configuration -public class WebMvcConfig extends WebMvcConfigurerAdapter { - @Override - public void addCorsMappings(CorsRegistry registry) { - registry.addMapping("/user/login").allowedOrigins("*") - .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS") - .allowCredentials(false).maxAge(3600); - } -} \ No newline at end of file diff --git a/src/main/java/com/wang/config/shiro/jwt/JwtFilter.java b/src/main/java/com/wang/config/shiro/jwt/JwtFilter.java index efc2523..630b634 100644 --- a/src/main/java/com/wang/config/shiro/jwt/JwtFilter.java +++ b/src/main/java/com/wang/config/shiro/jwt/JwtFilter.java @@ -133,7 +133,8 @@ protected boolean executeLogin(ServletRequest request, ServletResponse response) */ @Override protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception { - HttpServletRequest httpServletRequest = WebUtils.toHttp(request); + // 跨域已经在OriginFilter处全局配置 + /*HttpServletRequest httpServletRequest = WebUtils.toHttp(request); HttpServletResponse httpServletResponse = WebUtils.toHttp(response); httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin")); httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE"); @@ -142,7 +143,7 @@ protected boolean preHandle(ServletRequest request, ServletResponse response) th if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) { httpServletResponse.setStatus(HttpStatus.OK.value()); return false; - } + }*/ return super.preHandle(request, response); }