当前位置: 首页 > news >正文

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

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

}

相关文章:

  • C++ 设计模式——迭代器模式
  • ATL宁德新能源25届校招社招:Verify测评及四色测评考什么?真题分析
  • es集群详解
  • Eureka的生命周期管理:服务注册、续约与下线的完整流程解析
  • Visual Basic调试全景:解锁高效开发的秘密武器
  • 数据结构——队的基本操作
  • MATLAB 地面点构建三角网(83)
  • c++ 谷歌的招聘 题解
  • 基于PHP的文件包含介绍
  • 英伟达财报引爆AI投资狂潮?华尔街众说纷纭
  • 利用“2+1链动模式小程序AI智能名片S2B2C商城源码”优化企业参与外部社群策略
  • Web前端性能优化合集
  • node-sass@^4.13.0 run postinstall node scripts/build.js error:
  • IS-IS路由基础配置
  • html2canvas ios慎用和createImageBitmap ios慎用
  • 会议音频方案
  • Spark MLlib 特征工程系列—特征转换SQLTransformer
  • 搜维尔科技:使用MANUS VR手套控制特斯拉机器人叠衣服操作
  • 交通流量监测检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • Java基础——自学习使用(抽象类)
  • 三大上市猪企:前瞻应对饲料原材料价格波动
  • 拍摄《我们这一代》的肖全开展“江浙沪叙事”
  • 七部门联合发布《终端设备直连卫星服务管理规定》
  • 打造全域消费场景,上海大世界百个演艺娱乐新物种待孵化
  • 金科服务:大股东博裕资本提出无条件强制性现金要约收购,总代价约17.86亿港元
  • 西湖大学本科招生新增三省两市,首次面向上海招生