【问题标题】:Okta-api -Issue using Spring-security SAML with CSRFOkta-api - 使用带有 CSRF 的 Spring-security SAML 的问题
【发布时间】:2018-03-24 12:59:58
【问题描述】:

我已经完成了文档中列出的步骤 -

https://developer.okta.com/blog/2017/03/16/spring-boot-saml#run-the-app-and-login-with-okta

一切正常,我看到生成 SAML 响应并从 OKTA 对应用程序进行重新编辑,但是当请求到达应用程序时,我收到此错误 -

类型=禁止,状态=403)。发现无效的 CSRF 令牌“null” 请求参数“_csrf”或标头“X-CSRF-TOKEN”。

我曾尝试禁用 csrf,但随后它会通过 SAML 重定向进入无限循环。

这里是 SecurityConfiguration.java-

package com.example;

import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;

import org.springframework.beans.factory.annotation.Value;
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;

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Value("${security.saml2.metadata-url}")
    String metadataUrl;

    @Value("${server.ssl.key-alias}")
    String keyAlias;

    @Value("${server.ssl.key-store-password}")
    String password;

    @Value("${server.port}")
    String port;

    @Value("${server.ssl.key-store}")
    String keyStoreFilePath;

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/saml*").permitAll()
                .anyRequest().authenticated()
                .and()
            .apply(saml())
                .serviceProvider()
                    .keyStore()
                        .storeFilePath("saml/keystore.jks")
                        .password(this.password)
                        .keyname(this.keyAlias)
                        .keyPassword(this.password)
                        .and()
                    .protocol("https")
                    .hostname(String.format("%s:%s", "10.200.10.10", this.port))
                    .basePath("/")
                    .and()
                .identityProvider()
                .metadataFilePath(this.metadataUrl);
    }
}

欢迎提出任何建议。

【问题讨论】:

    标签: saml okta okta-api


    【解决方案1】:

    我在您的代码与我的博客文章中看到的唯一区别是以下行:

    .hostname(String.format("%s:%s", "10.200.10.10", this.port))
    

    如果您将其更改为以下内容,一切正常吗?

    .hostname(String.format("%s:%s", "localhost", this.port))
    

    【讨论】:

    【解决方案2】:

    这个问题已经解决了。我做了几件事-

    在 OKTA 中添加了与单点登录 URL 相同的目标 URL-https://localhost:8443/saml/SSO

    另外,在 Spring 方面,我在 SecurityConfiguration 中禁用了 CSRF 保护-

    http.csrf().disable();

    但真正的问题是错误的目标网址。

    【讨论】:

    • OKTA 上的目标网址在哪里?是重定向 uri 吗?
    【解决方案3】:
    @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/**").authorizeRequests().antMatchers("/saml").permitAll()
                    .anyRequest().authenticated().and().csrf().csrfTokenRepository(getCsrfTokenRepository());
        }
    
        private CsrfTokenRepository getCsrfTokenRepository() {
            CookieCsrfTokenRepository tokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse();
            tokenRepository.setCookiePath("/");
            return tokenRepository;
        }
    

    添加 CSRF cookie。在前端,如果您使用 Angular,只需导入 HttpClientXsrfModule。这将获取 cookie 值并设置请求标头 X-XSRF-TOKEN 标头

    @Note : saml login 的配置还是一样的。以上代码展示了如何添加csrf token。

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 2020-03-05
      • 2018-07-12
      • 2016-12-05
      • 2018-02-26
      • 2017-04-06
      • 2015-02-27
      • 2013-03-02
      相关资源
      最近更新 更多