普通项目解决跨域问题和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();
        //异常处理器

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

}
相关推荐
秋窗72 天前
调用百度翻译API遇到的跨域问题解决方案
nginx·api·跨域
闲来无事垂钓6 天前
Node.js学习记录(二)
node.js·接口·express·路由·跨域·cors·jsonp
ZhaiMou15 天前
前端跨域问题详解与解决方案指南
前端·ajax·面试·node.js·html5·js·跨域
玛卡`三少1 个月前
《Web项目跨域请求后端Api设置Cookie失败问题?》
前端·跨域
爬山虎还上班1 个月前
Http自定义Header导致的跨域问题
http·跨域
菊の物语2 个月前
vite解决前端跨域步骤
前端·vite·跨域
墨尊2 个月前
Oat++ 后端实现跨域
跨域·c++ 后端·oatpp
Java追光着2 个月前
谷粒商城实战笔记-跨域问题
笔记·跨域·谷粒商城
凉风听雪2 个月前
Node.js配置CORS跨域(解决服务器api接口跨域问题)
node.js·express·跨域·cors