当前位置: 首页 > 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();
        //异常处理器

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

}

相关文章:

  • 卸载重装redis
  • Linux自旋锁和读写锁
  • 用java实现的一个本地文件队列,支持并发消费与顺序消费
  • Spring Cloud Consul精选面试题及答案
  • 大模型如何赚钱,杀手级应用是什么、创业机会在哪里?
  • C3面:ASP.NET MVC 中还有哪些注释属性用来验证?
  • 代码随想录算法训练营第十一天|150. 逆波兰表达式求值 、239. 滑动窗口最大值、347.前 K 个高频元素
  • ue5远程渲染和本地渲染的区别,及云渲染的联系
  • 服务器被渗透的表现及检测方法
  • 从匿名内部类到Lambda表达式:Java编程的优雅进化
  • 字符串函数
  • 第3章-01-Python语言基础一篇通
  • Web-ssrfme
  • PowerShell 一键配置IP
  • 如何使用ssm实现基于java斗车交易系统设计与实现+vue
  • 【数据结构】八大排序
  • 每日一练【最大连续1的个数 III】
  • 前端网格布局display: grid;
  • LLM 直接偏好优化(DPO)的一些研究
  • JimuReport 积木报表 v1.8.0 版本发布,开源可视化报表
  • 国内锂矿“双雄”开局业绩PK:从巨亏中崛起,或深陷泥淖谋求多元转型
  • 国铁集团:5月1日全国铁路预计发送旅客2250万人次
  • 夜读丨春天要去动物园
  • 山西太原一居民小区发生爆炸,应急管理部派工作组赴现场
  • 美国参议院投票通过戴维·珀杜出任美国驻华大使
  • 日趋活跃!2024年我国数据生产总量同比增长25%