使用 HTTPS 时 Spring Boot 返回 401 Unauthorized 但它适用于 HTTP

问题描述

我有一个由 react 和 spring boot 开发的程序。 react 是 url.com 上的午餐,spring boot API 是 api.url.com 上的 当我使用 HTTP 协议时,我的 API 可以正常工作。但是当我启用 SSL 时它返回 401(未经授权)。 似乎 cookie 不是通过 HTTPS 发送的。

我使用本教程来配置我的 ssl: https://www.thomasvitale.com/https-spring-boot-ssl-certificate/

这也是我的安全配置。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .cors()
            .and()
            .requiresChannel() 
            .anyRequest()
            .requiresSecure() // SSL and block HTTP
            .and()
            .csrf().disable()
            .exceptionHandling()
                .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
            .and()
            .authorizeRequests()
                .antMatchers(resources).permitAll()
                .anyRequest()
                .authenticated()
                .accessDecisionManager(accessDecisionManager())
                
            .and()
            .exceptionHandling(e -> e
                    .accessDeniedPage("/error")
                    .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
                    )
            .oauth2Login()
                .clientRegistrationRepository(clientRegistrationRepository())
                .authorizedClientService(authorizedClientService())
                .successHandler(customAuthenticationSuccessHandler())
                .failureHandler(customAuthenticationFailureHandler())
                ;
        
           
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)