微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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 举报,一经查实,本站将立刻删除。