From 9347a3c52e15c9a9d26424e5978ae4e055003d18 Mon Sep 17 00:00:00 2001 From: DengMin <16679070171@qq.com> Date: Tue, 22 Mar 2022 18:35:04 +0800 Subject: [PATCH] 单设备登录 --- src/main/java/com/subsidy/common/configure/WebConfig.java | 17 ++++++++++++++++- src/main/java/com/subsidy/common/interceptor/AuthenticationInterceptor.java | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/subsidy/common/configure/WebConfig.java b/src/main/java/com/subsidy/common/configure/WebConfig.java index dc0d39f..07c78cd 100644 --- a/src/main/java/com/subsidy/common/configure/WebConfig.java +++ b/src/main/java/com/subsidy/common/configure/WebConfig.java @@ -1,8 +1,13 @@ package com.subsidy.common.configure; + import com.subsidy.common.interceptor.AuthenticationInterceptor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; @@ -59,5 +64,15 @@ public class WebConfig implements WebMvcConfigurer { .addPathPatterns("/**"); } - + @Bean + public CorsFilter corsFilter() { + CorsConfiguration config = new CorsConfiguration(); + config.setMaxAge(3600L); + config.addAllowedOrigin("*"); + config.addAllowedMethod("*"); + config.addAllowedHeader("*"); + UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); + configSource.registerCorsConfiguration("/**", config); + return new CorsFilter(configSource); + } } \ No newline at end of file diff --git a/src/main/java/com/subsidy/common/interceptor/AuthenticationInterceptor.java b/src/main/java/com/subsidy/common/interceptor/AuthenticationInterceptor.java index d18921b..63574c7 100644 --- a/src/main/java/com/subsidy/common/interceptor/AuthenticationInterceptor.java +++ b/src/main/java/com/subsidy/common/interceptor/AuthenticationInterceptor.java @@ -1,5 +1,6 @@ package com.subsidy.common.interceptor; +import com.alibaba.fastjson.JSON; import com.auth0.jwt.interfaces.Claim; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.subsidy.common.ResponseData; @@ -13,7 +14,9 @@ import com.subsidy.util.JwtUtil; import com.subsidy.util.Localstorage; import com.subsidy.util.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; @@ -43,6 +46,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor { private RedisUtil redisUtil; @Override + @CrossOrigin() public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { // 不需要进行拦截 if (!(handler instanceof HandlerMethod)) { @@ -83,6 +87,7 @@ public class AuthenticationInterceptor implements HandlerInterceptor { if(tk.equals(token)) { return true; } + throw new HttpException(1011); } else { throw new HttpException(1010); -- libgit2 0.25.0