首页 > 解决方案 > OKTA登录成功后spring boot应用进入死循环

问题描述

我正在尝试将 SSO 集成到我的 Spring Boot 应用程序中。我尝试遵循https://developer.okta.com/blog/2017/03/16/spring-boot-saml中提到的教程并创建了一个示例应用程序,一切正常,我被重定向到 Okta 登录页面。但是,当我尝试在具有上下文路径的应用程序中使用相同的教程时,应用程序会导航到 OKTA 登录页面,但随后会进入无限循环。

我的 application.properties 文件:

server.port = 9090
server.ssl.enabled = true
server.ssl.key-alias = spring
server.ssl.key-store = classpath:keystore.jks
server.ssl.key-store-password = secret
security.saml2.metadata-url = https://XxxxxXXXXXXXX/sso/saml/metadata
application.baseUrl = https://localhost:9090/my_app
security.saml2.context-provider.lb.context-path=/my_app

我的 okta 配置是:

Single sign-on URL: https://localhost:9090/**my_app**/saml/SSO
Audience URI (SP Entity ID) : https://localhost:9090/**my_app**/saml/metadata

有人可以告诉我如何使用上下文路径配置它吗?

2020-10-16 09:18:14.202  INFO 9372 --- [nio-9090-exec-9] o.s.security.saml.log.SAMLDefaultLogger  : AuthNRequest;SUCCESS;0:0:0:0:0:0:0:1;https://localhost:9090/saml/metadata;http://www.okta.com/xxxxxxxxxxxxxxxxxx;;;
2020-10-16 09:18:14.996  INFO 9372 --- [nio-9090-exec-1] o.s.security.saml.log.SAMLDefaultLogger  : AuthNRequest;SUCCESS;0:0:0:0:0:0:0:1;https://localhost:9090/saml/metadata;http://www.okta.com/xxxxxxxxxxxxxxxxxx;;;
2020-10-16 09:18:15.779  INFO 9372 --- [nio-9090-exec-6] o.s.security.saml.log.SAMLDefaultLogger  : AuthNRequest;SUCCESS;0:0:0:0:0:0:0:1;https://localhost:9090/saml/metadata;http://www.okta.com/xxxxxxxxxxxxxxxxxx;;;
2020-10-16 09:18:16.512  INFO 9372 --- [nio-9090-exec-8] o.s.security.saml.log.SAMLDefaultLogger  : AuthNRequest;SUCCESS;0:0:0:0:0:0:0:1;https://localhost:9090/saml/metadata;http://www.okta.com/xxxxxxxxxxxxxxxxxx;;;
2020-10-16 09:18:17.558  INFO 9372 --- [nio-9090-exec-3] o.s.security.saml.log.SAMLDefaultLogger  : AuthNRequest;SUCCESS;0:0:0:0:0:0:0:1;https://localhost:9090/saml/metadata;http://www.okta.com/xxxxxxxxxxxxxxxxxx;;;

标签: spring-bootsingle-sign-onsamlokta

解决方案


我通过在安全配置文件中添加以下行解决了这个问题:

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

推荐阅读