Okta-api -Issue使用带有CSRF的Spring-security SAML

时间:2017-10-12 14:26:22

标签: saml okta okta-api

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

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

一切正常,我看到SAML响应生成并从OKTA发送到应用程序,但是当请求到达应用程序时,我收到此错误 -

  

type = Forbidden,status = 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);
    }
}

任何建议都表示赞赏。

3 个答案:

答案 0 :(得分:0)

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

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

如果将其更改为以下内容,是否有效?

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

答案 1 :(得分:0)

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

在OKTA中添加的目标网址与网址https://localhost:8443/saml/SSO

上的单一广告相同

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

http.csrf()禁用();

但问题确实是错误的目标网址。

答案 2 :(得分:0)

@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标头

@注意:saml登录的配置仍然相同。上面的代码显示了如何添加csrf令牌。