Axios身份验证请求无法发送头信息给Spring Boot
创始人
2024-09-29 01:30:13
0

要解决Axios身份验证请求无法发送头信息给Spring Boot的问题,可以尝试以下解决方法:

  1. 在Axios请求中设置withCredentialstrue,以便将身份验证头信息发送给Spring Boot。
axios.get('http://example.com/api', {
  withCredentials: true
});
  1. 在Spring Boot的跨域配置中,允许包含身份验证头信息的请求。
@Configuration
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("*")
                        .allowedOrigins("*")
                        .allowedHeaders("Authorization", "Content-Type")
                        .allowCredentials(true);
            }
        };
    }
}

上述代码中,通过allowedHeaders方法允许包含Authorization头信息的请求,并通过allowCredentials方法允许跨域请求发送身份验证信息。

  1. 如果Spring Boot应用程序使用Spring Security进行身份验证和授权,还需要配置允许跨域请求通过Spring Security的安全链。
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors().and()
                .authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}

上述代码中,通过.cors()方法启用跨域请求配置,并通过.antMatchers("/**").permitAll()配置允许所有请求跨域访问。

通过上述解决方法,Axios就能够成功发送身份验证头信息给Spring Boot了。

相关内容

热门资讯

透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器下载,微信微乐游戏有辅助吗,曝光透视(有挂方式)-哔哩哔哩1、许多玩家...
透视辅助!wepoker透视方... 透视辅助!wepoker透视方法,微乐自建房脚本使用安全吗,了解脚本(有挂总结)-哔哩哔哩wepok...
脚本透视!hhpkoer辅助挂... 脚本透视!hhpkoer辅助挂是真的吗,hhpoker有后台操作吗(透视)确实真的是有挂(哔哩哔哩)...
据玩家消息!wepoker软件... 据玩家消息!wepoker软件安装包,微乐自建房辅助神器怎么安装,教你脚本(确实有挂)-哔哩哔哩1、...
透视攻略!德州局HHpoker... 透视攻略!德州局HHpoker透视脚本,微乐小程序辅助开发透视,有挂脚本(有挂技巧)-哔哩哔哩1、德...
透视开挂!hhpoker辅助,... 透视开挂!hhpoker辅助,hh poker辅助器先试用(透视)其实是真的软件(哔哩哔哩)暗藏猫腻...
透视分享!aapoker辅助器... 透视分享!aapoker辅助器是真的吗,aapoker怎么开辅助器(透视)好像存在有挂(哔哩哔哩)1...
值得注意的是!线上德州的辅助器... 值得注意的是!线上德州的辅助器是什么,广西微乐小程序微信辅助器免费,详情透视(有挂方针)-哔哩哔哩1...
透视了解!aapoker如何设... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视攻略!hhpoker软件安... 透视攻略!hhpoker软件安装包,微乐小游戏万能修改器,详情透视(有挂秘诀)-哔哩哔哩亲,关键说明...