普通项目解决跨域问题和springSecurity解决跨域问题

普通项目解决跨域问题

添加一个配置文件

复制代码
package com.lzy.config;

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.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

   private CorsConfiguration buildConfig() {
      CorsConfiguration corsConfiguration = new CorsConfiguration();
      corsConfiguration.addAllowedOrigin("*");
      corsConfiguration.addAllowedHeader("*");
      corsConfiguration.addAllowedMethod("*");
      corsConfiguration.addExposedHeader("Authorization");
      return corsConfiguration;
   }
   
   @Bean
   public CorsFilter corsFilter() {
      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      source.registerCorsConfiguration("/**", buildConfig());
      return new CorsFilter(source);
   }
   
   @Override
   public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**")
            .allowedOrigins("*")
//          .allowCredentials(true)
            .allowedMethods("GET", "POST", "DELETE", "PUT")
            .maxAge(3600);
   }
}

springSecurity解决跨域问题

不光要添加上面的文件,还需要写一个springSecurity的配置类

复制代码
package com.lzy.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启方法级别的权限注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private static final String[] URL_WHITELIST = {
            "/login",
            "/logout",
            "/captcha",
            "/favicon.ico", // 防止 favicon 请求被拦截
    };

    protected void configure(HttpSecurity http) throws Exception {
        //跨域配置
        http.cors().and().csrf().disable()
                //登录配置
                .formLogin()
//            .successHandler().failureHandler()
                //禁用session
                .and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                //配置拦截规则
                .and().authorizeRequests()
                //白名单
                .antMatchers(URL_WHITELIST).permitAll()
                //其他请求都需要认证
                .anyRequest().authenticated();
        //异常处理器

        //配置自定义的过滤器
    }

}
相关推荐
身如柳絮随风扬2 天前
深度解析跨域问题:真实场景、解决方案与进阶方案
跨域
We་ct5 天前
深度剖析浏览器跨域问题
开发语言·前端·浏览器·跨域·cors·同源·浏览器跨域
胡志辉的博客19 天前
本地明明好好的,怎么一上线就跨域了?把同源策略、前后端分工和 CORS 一次讲明白
前端·javascript·vue.js·reactjs·nextjs·跨域
软弹24 天前
快速了解前端中的跨域问题
前端·javascript·vue.js·react.js·node.js·跨域
csdn_aspnet1 个月前
在 .NET Core 8 中实现 CORS
.netcore·跨域·cors·.net8
╰つ栺尖篴夢ゞ1 个月前
Web之深入解析Cookie的安全防御与跨域实践
前端·安全·存储·cookie·跨域
九皇叔叔1 个月前
006-SpringSecurity-Demo 跨域(CORS)配置
java·springboot3·springsecurity·跨域·cors
名字很费劲1 个月前
thinkphp8怎么解决跨域错误
跨域·thinkphp8
丁丁丁梦涛2 个月前
oss自定义域名+cdn跨域问题解决
cdn·oss·跨域·自定义域名
大飞哥~BigFei4 个月前
新版chrome浏览器安全限制及解决办法
java·前端·chrome·安全·跨域