Commit 1e40e60dbf96e3e142c450d0d9de1df525d585cb
1 parent
133a10ec
支持CORS跨域请求
Showing
3 changed files
with
37 additions
and
0 deletions
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java
| 1 | 1 | package com.ruoyi.framework.config; |
| 2 | 2 | |
| 3 | 3 | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.context.annotation.Bean; | |
| 4 | 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 | 9 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 6 | 10 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 7 | 11 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| ... | ... | @@ -39,4 +43,24 @@ public class ResourcesConfig implements WebMvcConfigurer |
| 39 | 43 | { |
| 40 | 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 | } |
| 43 | 67 | \ No newline at end of file | ... | ... |
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
| ... | ... | @@ -12,6 +12,8 @@ import org.springframework.security.config.http.SessionCreationPolicy; |
| 12 | 12 | import org.springframework.security.core.userdetails.UserDetailsService; |
| 13 | 13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 14 | 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 | 17 | import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; |
| 16 | 18 | import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; |
| 17 | 19 | import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; |
| ... | ... | @@ -47,6 +49,12 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
| 47 | 49 | */ |
| 48 | 50 | @Autowired |
| 49 | 51 | private JwtAuthenticationTokenFilter authenticationTokenFilter; |
| 52 | + | |
| 53 | + /** | |
| 54 | + * 跨域过滤器 | |
| 55 | + */ | |
| 56 | + @Autowired | |
| 57 | + private CorsFilter corsFilter; | |
| 50 | 58 | |
| 51 | 59 | /** |
| 52 | 60 | * 解决 无法直接注入 AuthenticationManager |
| ... | ... | @@ -112,6 +120,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter |
| 112 | 120 | httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); |
| 113 | 121 | // 添加JWT filter |
| 114 | 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 | ... | ... |
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java
| ... | ... | @@ -178,6 +178,8 @@ public class GenController extends BaseController |
| 178 | 178 | private void genCode(HttpServletResponse response, byte[] data) throws IOException |
| 179 | 179 | { |
| 180 | 180 | response.reset(); |
| 181 | + response.addHeader("Access-Control-Allow-Origin", "*"); | |
| 182 | + response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); | |
| 181 | 183 | response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); |
| 182 | 184 | response.addHeader("Content-Length", "" + data.length); |
| 183 | 185 | response.setContentType("application/octet-stream; charset=UTF-8"); | ... | ... |