<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maksim Stepachev</title>
    <description>The latest articles on DEV Community by Maksim Stepachev (@ingvard).</description>
    <link>https://dev.to/ingvard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1515601%2F4b16ed42-ed0d-4df6-8fd3-bb2246dbd553.png</url>
      <title>DEV Community: Maksim Stepachev</title>
      <link>https://dev.to/ingvard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ingvard"/>
    <language>en</language>
    <item>
      <title>Windows Live Single Sign-On (SSO) SpringBoot OAuth without spring-cloud-azure-starter-active-directory | Windows Live SSO</title>
      <dc:creator>Maksim Stepachev</dc:creator>
      <pubDate>Fri, 24 May 2024 10:57:33 +0000</pubDate>
      <link>https://dev.to/ingvard/windows-live-single-sign-on-sso-springboot-oauth-without-spring-cloud-azure-starter-active-directory-windows-live-sso-519h</link>
      <guid>https://dev.to/ingvard/windows-live-single-sign-on-sso-springboot-oauth-without-spring-cloud-azure-starter-active-directory-windows-live-sso-519h</guid>
      <description>&lt;p&gt;Hey there. This is my developer's notes about configuring Spring Boot OAuth without additional libraries.&lt;/p&gt;

&lt;p&gt;There are so many examples of SSO configurations for Microsoft Identity providers on the internet. Unfortunately, all of them require an extra dependency like &lt;code&gt;spring-cloud-azure-starter-active-directory&lt;/code&gt;, for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/spring-boot-starter-for-azure-active-directory-developer-guide"&gt;Spring Boot Starter for Microsoft Entra developer's guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory"&gt;Add sign-in with Microsoft Entra account to a Spring web app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It surprised me because at that moment I already had several integrations via &lt;code&gt;org.springframework.boot:spring-boot-starter-oauth2-client&lt;/code&gt; without any additional libraries.&lt;br&gt;
The spring has already the well-prepared configurations for auth providers which are stored in the &lt;code&gt;CommonOAuth2Provider&lt;/code&gt;. These providers include &lt;code&gt;GOOGLE&lt;/code&gt;, &lt;code&gt;GITHUB&lt;/code&gt;, &lt;code&gt;FACEBOOK&lt;/code&gt;, &lt;code&gt;OKTA&lt;/code&gt;. However, &lt;code&gt;MICROSOFT&lt;/code&gt; is not included.&lt;/p&gt;

&lt;p&gt;I used this &lt;code&gt;CommonOAuth2Provider&lt;/code&gt; as an example and created the following configuration.&lt;/p&gt;

&lt;p&gt;Register a new provider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.security.oauth2.client.provider.microsoft.issuer-uri: https://login.microsoftonline.com/&amp;lt;Tenant ID&amp;gt;/v2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure this provider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.security.oauth2.client.registration.microsoft.clientId=&amp;lt;client_id&amp;gt;
spring.security.oauth2.client.registration.microsoft.clientSecret=&amp;lt;client_secret&amp;gt;
spring.security.oauth2.client.registration.microsoft.redirectUri= https://&amp;lt;your_redirect_url&amp;gt;/&amp;lt;your_path&amp;gt;/microsoft
spring.security.oauth2.client.registration.microsoft.scope=openid,profile,email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To complete this configuration you need to have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Tenant ID&lt;/code&gt;, &lt;code&gt;client_id&lt;/code&gt;, &lt;code&gt;client_secret&lt;/code&gt; - that is created by this &lt;a href="https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory"&gt;official guide&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;redirectUri&lt;/code&gt; - that is configured on the spring side by &lt;a href="https://www.baeldung.com/spring-security-5-oauth2-login#5-custom-redirection-endpoint"&gt;this instruction&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It works, but it only allows me to sign in as a user who already exists in my Azure tenant's AD. If I try to use my Skype account, it leads to the following error message, even though I have chosen:&lt;code&gt;Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;AADSTS50020: User account '&lt;a href="mailto:username@google.com"&gt;username@google.com&lt;/a&gt;' from identity provider '&lt;a href="https://sts.windows.net/852cfa84-de9a-40a5-a885-2517e9aa919e/"&gt;https://sts.windows.net/852cfa84-de9a-40a5-a885-2517e9aa919e/&lt;/a&gt;' does not exist in tenant 'yourdomen.org' and cannot access the application 'd390cb06-2da2-483d-a0fd-0ecbef3fb8e2'(Your application) in that tenant. The account needs to be added as an external user in the tenant first. Sign out and sign in again with a different Azure Active Directory user account.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have found a few solutions to this, such as creating an application of the B2C type or switching to automatically adding external users to the active directory. This is a strange solution, but I am still interested in why some Wordpress integrations work &lt;a href="https://plugins.miniorange.com/windows-live-oauth-and-openid-connect-single-sign-on-sso-login"&gt;without this problem&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I tried to find how they configure their plugins for it and as result created the following configuration for us.&lt;/p&gt;

&lt;p&gt;The registration of the provider from scratch (don't use &lt;code&gt;issuer-uri&lt;/code&gt; from the previous example here):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.security.oauth2.client.provider.microsoft.authorization_uri=https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize
spring.security.oauth2.client.provider.microsoft.token_uri=https://login.microsoftonline.com/consumers/oauth2/v2.0/token
spring.security.oauth2.client.provider.microsoft.jwk_set_uri=https://login.microsoftonline.com/consumers/discovery/v2.0/keys
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure this provider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.security.oauth2.client.registration.github.clientId=&amp;lt;client_id&amp;gt;
spring.security.oauth2.client.registration.github.clientSecret=&amp;lt;client_secret&amp;gt;
spring.security.oauth2.client.registration.github.redirectUri=https://&amp;lt;your_redirect_url&amp;gt;/&amp;lt;your_path&amp;gt;/microsoft
spring.security.oauth2.client.registration.microsoft.authorization_grant_type=authorization_code
spring.security.oauth2.client.registration.microsoft.scope=openid,profile,email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This set of configurations helps me sign in with my personal account without additional configuration of the active directory.&lt;/p&gt;

&lt;p&gt;I hope this developer's note will be useful to someone and save you time searching.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>livesso</category>
      <category>oauth</category>
      <category>microsoft</category>
    </item>
  </channel>
</rss>
