~/spring/spring-saml-okta-solve-saml-message-intended-destination-not-match.md

Fix: SAML Message Intended Destination Did Not Match Recipient

1 min readApril 20, 2022by Gorav Singal#spring#okta#saml
// TL;DR

Resolve the SAML destination mismatch caused by http vs https difference by configuring a reverse proxy or setting the server.use-forward-headers property.

Fix: SAML Message Intended Destination Did Not Match Recipient

Introduction

I was trying to integrate Okta with Spring, and when I deploy the code. I got following error:

org.opensaml.common.binding.decoding.BaseSAMLMessageDecoder] SAML message intended destination endpoint did not match the recipient endpoint

And, the error mentioned the two URLs only differe by https. One was with http, and other was with https.

Setup

I was working with docker containers for my spring app, and was deploying it on kubernetes, behind a Ingress load balancer. My spring app pod was running on http, and setup SSL (https) on Ingress load balancer.

Spring Beans before Solution

I’m not mentioning all beans defined. Just mentioning two beans that needed a fix.

<bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter">
    <constructor-arg>
        <bean class="org.springframework.security.saml.metadata.MetadataGenerator">
            <property name="entityId" value="${saml.audience.url}"/>
            <property name="extendedMetadata">
                <bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
                    <property name="idpDiscoveryEnabled" value="false"/>
                </bean>
            </property>
        </bean>
    </constructor-arg>
</bean>

<bean id="contextProvider" class="org.springframework.security.saml.context.SAMLContextProviderImpl"/>

Where value of saml.audience.url was:

saml.audience.url=https://<MyApp>.com/api/saml/audience

Solution

I needed to correct above two beans.

<bean id="metadataGeneratorFilter" class="org.springframework.security.saml.metadata.MetadataGeneratorFilter">
    <constructor-arg>
        <bean class="org.springframework.security.saml.metadata.MetadataGenerator">
            <property name="entityId" value="${saml.audience.url}"/>
            <property name="entityBaseURL" value="${saml.entity.base.url}"/>
            <property name="extendedMetadata">
                <bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
                    <property name="idpDiscoveryEnabled" value="false"/>
                </bean>
            </property>
        </bean>
    </constructor-arg>
</bean>

<bean id="contextProvider" class="org.springframework.security.saml.context.SAMLContextProviderLB">
    <property name="scheme" value="https"/>
    <property name="serverName" value="${saml.server.name}"/>
    <property name="serverPort" value="443"/>
    <property name="includeServerPortInRequestURL" value="false"/>
    <property name="contextPath" value="${saml.context.path}"/>
</bean>

Notice two things:

  • contextProvider bean of class SAMLContextProviderLB
  • entityBaseURL in metadataGeneratorFilter bean

Lets have a look at their values:

saml.metadata.url: "https://XYZ.okta.com/app/<IDP_ID>/sso/saml/metadata"
saml.audience.url: "https://<MyApp>.com/api/saml/audience"
saml.entity.base.url: "https://<MyAPp>.com/api"
saml.server.name: "<MyApp>.com"
saml.context.path: "/api"

Note, its very important to set saml.context.path with a slash in beginning.

Now, build your app and run. It ran smoothly without any issue.

Hope it helps. Thanks for reading.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Spring - Learn Multiple Ways to use PackageScan Annotation

Spring - Learn Multiple Ways to use PackageScan Annotation

Introduction In this post, we will see multiple ways to use annotation…

Spring Boot - Fixing Autowire Bean Not found

Spring Boot - Fixing Autowire Bean Not found

Introduction In a Spring boot app, we tend to use annotation, so that Spring…

Java - Union and Intersection of two lists

Java - Union and Intersection of two lists

Suppose you have two lists, and you want Union and Intersection of those two…

Linkage Error Loader Constraint Violation - JUnit test case development issue

Linkage Error Loader Constraint Violation - JUnit test case development issue

Its good to write unit tests cases, and this part is mostly forgotten by…

How to mock a constructor - Junit test case development issues

How to mock a constructor - Junit test case development issues

While writing JUnit test cases, we encounter cases like we want to initialize a…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…