当前位置: 首页 > 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/11542.html

相关文章:

  • 进程的创建,结束,回收基础API
  • Spring Boot : ORM 框架 JPA 与连接池 Hikari
  • 大模型学习必备指南:深入解析技术原理与应用,从入门到精通一应俱全
  • 软考攻略/超详细/系统集成项目管理工程师/基础知识分享04
  • Kafka事件(消息、数据、日志)的存储
  • 4. kafka消息监控客户端工具
  • S3协议分片上传(minio)
  • kubebuiler开发operator理论术语
  • Postman【使用总结】--SpringBoot的Controller规范【重修】
  • C#入门(16)for循环
  • 使用AWS的EC2服务如何降低成本
  • 摄像头设备问题如何检测
  • (24)(24.4) MultiWii/DJI/HDZero OSD (version 4.2 and later)(二)
  • 【软考】网络安全性威胁
  • WordPress全站从http升级至https配置
  • 图解 Elasticsearch 的 Fielddata Cache 使用与优化
  • sql server导入mysql,使用python多线程
  • 为啥我的焊接阈值没有单位了
  • 端到端 - UniAD: Planning-oriented Autonomous Driving - 以规划为导向的自动驾驶(CVPR 2023)
  • day36