正在显示
3 个修改的文件
包含
37 行增加
和
0 行删除
| 1 | package com.ruoyi.framework.config; | 1 | package com.ruoyi.framework.config; |
| 2 | 2 | ||
| 3 | import org.springframework.beans.factory.annotation.Autowired; | 3 | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.context.annotation.Bean; | ||
| 4 | import org.springframework.context.annotation.Configuration; | 5 | import org.springframework.context.annotation.Configuration; |
| 6 | +import org.springframework.web.cors.CorsConfiguration; | ||
| 7 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
| 8 | +import org.springframework.web.filter.CorsFilter; | ||
| 5 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | 9 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 6 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | 10 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | 11 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| @@ -39,4 +43,24 @@ public class ResourcesConfig implements WebMvcConfigurer | @@ -39,4 +43,24 @@ public class ResourcesConfig implements WebMvcConfigurer | ||
| 39 | { | 43 | { |
| 40 | registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**"); | 44 | registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**"); |
| 41 | } | 45 | } |
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 跨域配置 | ||
| 49 | + */ | ||
| 50 | + @Bean | ||
| 51 | + public CorsFilter corsFilter() | ||
| 52 | + { | ||
| 53 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
| 54 | + CorsConfiguration config = new CorsConfiguration(); | ||
| 55 | + config.setAllowCredentials(true); | ||
| 56 | + // 设置访问源地址 | ||
| 57 | + config.addAllowedOrigin("*"); | ||
| 58 | + // 设置访问源请求头 | ||
| 59 | + config.addAllowedHeader("*"); | ||
| 60 | + // 设置访问源请求方法 | ||
| 61 | + config.addAllowedMethod("*"); | ||
| 62 | + // 对接口配置跨域设置 | ||
| 63 | + source.registerCorsConfiguration("/**", config); | ||
| 64 | + return new CorsFilter(source); | ||
| 65 | + } | ||
| 42 | } | 66 | } |
| @@ -12,6 +12,8 @@ import org.springframework.security.config.http.SessionCreationPolicy; | @@ -12,6 +12,8 @@ import org.springframework.security.config.http.SessionCreationPolicy; | ||
| 12 | import org.springframework.security.core.userdetails.UserDetailsService; | 12 | import org.springframework.security.core.userdetails.UserDetailsService; |
| 13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | 13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 14 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | 14 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 15 | +import org.springframework.security.web.authentication.logout.LogoutFilter; | ||
| 16 | +import org.springframework.web.filter.CorsFilter; | ||
| 15 | import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; | 17 | import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; |
| 16 | import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; | 18 | import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; |
| 17 | import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; | 19 | import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; |
| @@ -49,6 +51,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter | @@ -49,6 +51,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter | ||
| 49 | private JwtAuthenticationTokenFilter authenticationTokenFilter; | 51 | private JwtAuthenticationTokenFilter authenticationTokenFilter; |
| 50 | 52 | ||
| 51 | /** | 53 | /** |
| 54 | + * 跨域过滤器 | ||
| 55 | + */ | ||
| 56 | + @Autowired | ||
| 57 | + private CorsFilter corsFilter; | ||
| 58 | + | ||
| 59 | + /** | ||
| 52 | * 解决 无法直接注入 AuthenticationManager | 60 | * 解决 无法直接注入 AuthenticationManager |
| 53 | * | 61 | * |
| 54 | * @return | 62 | * @return |
| @@ -112,6 +120,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter | @@ -112,6 +120,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter | ||
| 112 | httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); | 120 | httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); |
| 113 | // 添加JWT filter | 121 | // 添加JWT filter |
| 114 | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); | 122 | httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| 123 | + // 添加CORS filter | ||
| 124 | + httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class); | ||
| 125 | + httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class); | ||
| 115 | } | 126 | } |
| 116 | 127 | ||
| 117 | 128 |
| @@ -178,6 +178,8 @@ public class GenController extends BaseController | @@ -178,6 +178,8 @@ public class GenController extends BaseController | ||
| 178 | private void genCode(HttpServletResponse response, byte[] data) throws IOException | 178 | private void genCode(HttpServletResponse response, byte[] data) throws IOException |
| 179 | { | 179 | { |
| 180 | response.reset(); | 180 | response.reset(); |
| 181 | + response.addHeader("Access-Control-Allow-Origin", "*"); | ||
| 182 | + response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); | ||
| 181 | response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); | 183 | response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); |
| 182 | response.addHeader("Content-Length", "" + data.length); | 184 | response.addHeader("Content-Length", "" + data.length); |
| 183 | response.setContentType("application/octet-stream; charset=UTF-8"); | 185 | response.setContentType("application/octet-stream; charset=UTF-8"); |
-
请 注册 或 登录 后发表评论