当前位置: 首页 > 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;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", buildConfig());return new CorsFilter(source);}@Overridepublic 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();//异常处理器//配置自定义的过滤器}}


http://www.mrgr.cn/news/8645.html

相关文章:

  • 【学术会议征稿】2024年计算机与信息安全国际会议(WCCIS 2024)
  • 并行程序设计基础——组通信(2)
  • 【精选】基于大数据的___银行信用卡用户的数仓系统的设计与实现(全网独一无二,最新定制)
  • 中学教师资格笔试题库及答案
  • linux定期统计某个目录内每天的文件增量大小
  • ROUTE_STATUS
  • 微信小程序:开发工具修改js编译后还是旧的js逻辑
  • React Hooks 的高级用法
  • 数据库系统 第21节 ACID 属性
  • BZOJ 五月胡乱补题
  • pikachu SSRF通关(服务器端请求伪造)
  • Kubernetes
  • 企业选择raksmart大带宽服务器的原因
  • java springboot 实现文件上传下载(文件服务器,文件统一处理,图片,word,pdf,视频,等)
  • 华为手机相册的照片丢失或误删怎么恢复?
  • 哈里和梅根的批评者似乎并不理解“不可征服运动会”的全球影响力
  • 什么是微服务?
  • 针对防火墙IPSec业务不通或业务丢包问题,防火墙如何做流量统计、远程抓包、报文示踪
  • 图解Kafka | 28张图彻底搞懂消费者
  • Redis保姆级安装教程