Bläddra i källkod

config(security): 优化安全配置并添加全局跨域支持

- 在 ResourceServerConfig 中禁用 CSRF 并启用跨域支持
- 新增 WebConfig 类,用于全局跨域配置
- 调整授权请求的配置,使代码更加清晰
zhouwang 1 vecka sedan
förälder
incheckning
27abfe869f

+ 10 - 10
src/main/java/com/lqkj/framework/security/ResourceServerConfig.java

@@ -33,7 +33,8 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
33 33
     }
34 34
     @Override
35 35
     public void configure(HttpSecurity http) throws Exception {
36
-        http.formLogin()
36
+        http.cors().and().csrf().disable()
37
+                .formLogin()
37 38
                 .loginProcessingUrl("/login")
38 39
                 .successHandler(customAuthenticationSuccessHandler)
39 40
                 .failureHandler(customAuthenctiationFailureHandler)
@@ -43,20 +44,19 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
43 44
                 .and()
44 45
                 .authorizeRequests()
45 46
                 .antMatchers("/oauth/**",
46
-                "/v1/captchaImage",
47
-                "/business/cmsNews/front/**",
48
-                "/business/cmsCategory/front/**",
49
-                "/login",
50
-                "/**/*.css",
51
-                "/**/*.js",
52
-                "/profile/**").permitAll() //不需要身份认证即可访问
47
+                        "/v1/captchaImage",
48
+                        "/business/cmsNews/front/**",
49
+                        "/business/cmsCategory/front/**",
50
+                        "/login",
51
+                        "/**/*.css",
52
+                        "/**/*.js",
53
+                        "/profile/**").permitAll() //不需要身份认证即可访问
53 54
                 .antMatchers("/swagger-resources/**").anonymous()
54 55
                 .antMatchers("/webjars/**").anonymous()
55 56
                 .antMatchers("/*/api-docs").anonymous()
56 57
                 .anyRequest().authenticated() //其他请求路径都需要身份认证
57 58
                 .and().headers().frameOptions().disable()//支持前端vue中iframe中访问
58
-                .and().cors()
59
-                .and().csrf().disable();
59
+                .and().cors(); // 启用跨域支持
60 60
     }
61 61
 
62 62
 

+ 22 - 0
src/main/java/com/lqkj/framework/security/WebConfig.java

@@ -0,0 +1,22 @@
1
+package com.lqkj.framework.security;
2
+
3
+import org.springframework.context.annotation.Bean;
4
+   import org.springframework.context.annotation.Configuration;
5
+   import org.springframework.web.cors.CorsConfiguration;
6
+   import org.springframework.web.cors.CorsConfigurationSource;
7
+   import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
8
+
9
+   @Configuration
10
+   public class WebConfig {
11
+
12
+       @Bean
13
+       public CorsConfigurationSource corsConfigurationSource() {
14
+           CorsConfiguration configuration = new CorsConfiguration();
15
+           configuration.addAllowedOrigin("*"); // 允许所有来源,也可以指定特定的域名
16
+           configuration.addAllowedMethod("*"); // 允许所有HTTP方法
17
+           configuration.addAllowedHeader("*"); // 允许所有请求头
18
+           UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
19
+           source.registerCorsConfiguration("/**", configuration);
20
+           return source;
21
+       }
22
+   }