首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用和CSRF的Okta-api -Issue

使用和CSRF的Okta-api -Issue
EN

Stack Overflow用户
提问于 2017-10-12 14:26:22
回答 3查看 1.8K关注 0票数 1

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

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‘或标头’X令牌‘上发现无效的CSRF令牌'null’。

我试过禁用csrf,但随后它会以无限循环的SAML重定向。

这是SecurityConfigur.java

代码语言:javascript
复制
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);
    }
}

如有任何建议,将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2017-10-12 15:21:52

我在您的代码与我的博客文章中看到的唯一不同之处是下面一行:

代码语言:javascript
复制
.hostname(String.format("%s:%s", "10.200.10.10", this.port))

如果您将其更改为以下内容,事情会正常吗?

代码语言:javascript
复制
.hostname(String.format("%s:%s", "localhost", this.port))
票数 0
EN

Stack Overflow用户

发布于 2017-10-12 19:17:56

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

在OKTA中添加了目标url,与URL- https://localhost:8443/saml/SSO上的单个符号相同。

另外,在春季方面,我已经在安全配置中禁用了CSRF保护-

http.csrf().disable();

但实际上,这个问题是错误的目的地url。

票数 0
EN

Stack Overflow用户

发布于 2019-08-13 06:13:20

代码语言:javascript
复制
@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曲奇。在前端,如果您使用的是角,只需导入HttpClientXsrfModule。这将获取cookie值并设置请求报头XSRF令牌报头。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46712155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档