Add Social Login Your Spring Boot 2.0 App

Add Social Login Your Spring Boot 2.0 App

The primary benefits of social login are ease of use and security. It’s easier for your users to log into your app if they can use credentials they’ve already created on a a service they trust.

Gelom4

Awesome blog post! The instructions were clear and very straightforward. I got it work in the end, however the application.yml resource file was included in the .ignore file. :wink: Anyway very helpful. Big thanks!

Andrew Hughes

Thanks for pointing that out (r/ the application.yml). It’s been fixed. Glad you got it to work!

mtbadi39

Hi Andrew, its a clear and very helpful post, thanks for sharing.
just a question : why Twitter not found as a provider in okta dashboard ?

Matt Raible

Twitter is not currently supported because they use OAuth 1, not OIDC like modern social authentication providers. See https://developer.twitter.c… for more information.

Ela Fader

1. It was announced Google+ is being shut down. It would be nice to mention how those this affect authentication and fact you have to enable google+ api to use Google as id provider.
2. I had problems adding redirect url at google api. Provided value was not saved. I hacked it by providing two urls (of which only first one was saved).
3. The tutorial note “If your social accounts use an email address different than what you’re using for your Okta preview account, you may need to add a user to the Okta application.” suggests you need to create users first. It did not happen to me (for google), octa user was created by logging in. It would be worth mentioning what to disable this kind of registration.

Brian Demers

Hey @elafader!
Sorry for the delay :wink:

I think the only Google+ specific part of this post is the “Sign in with Google” button, we will get that updated!

The different email account issue, could come up when you assign a specific user to an Okta Application. Social user with matching email addresses will be matched with the corresponding Okta account. But if you have an Okta account from self@corp.com and a Google account of self@gmail.com these will not match.

When enabling social auth i’d recommend adding the “Everyone” group to your applications, otherwise your users will end up being authenticated but still not able to access your application (likely leaving them confused)

For more info on Okta and the Google+ deprecation check out: https://support.okta.com/he…

Ravi Sharma

I am getting error when running the app Error handling failed (class org.springframework.boot.context.event.ApplicationFailedEvent cannot be cast to class org.springframework.security.authentication.event.AuthenticationSuccessEvent (org.springframework.boot.context.event.ApplicationFailedEvent and org.springframework.security.authentication.event.AuthenticationSuccessEvent are in unnamed module of loader ‘app’))
2021-04-16 17:26:59.095 ERROR 94339 — [ restartedMain] o.s.boot.SpringApplication : Application run failed

Appreciate your input to solve this.

Matt Raible

Hello Ravi,

I updated this post today to use the latest version of Spring Boot. See the changelog at the bottom for details. Can you please try again?

Ravi Sharma

Hi Matt,

thanks a ton :slight_smile: . now i am not facing that issue. i am able to navigate through social login flow for gmail. my user profile is getting created although i am getting the error invalid_nonce. could you please clue for resolving this.

Appreciate your input!

Matt Raible

I’m not sure why this is happening. We did have a recent change where new apps don’t have the Everyone group assigned to them, so you might want to check that. If that’s OK, I’d try posting your question to our developer forums. More people will see it there and hopefully someone will be able to help you.

https://devforum.okta.com/

Ravi Sharma

Thanks Matt, appreciate your help.

radhe

Code is working file with java 11 but facing below issue in java 8.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘hostedLoginCodeFlowExampleApplication.OAuth2SecurityConfigurerAdapter’: Unsatisfied dependency expressed through method ‘setContentNegotationStrategy’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration’: Unsatisfied dependency expressed through method ‘setConfigurers’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration$OAuth2ClientWebMvcSecurityConfiguration’: Unsatisfied dependency expressed through method ‘setClientRegistrationRepository’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘clientRegistrationRepository’ defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository]: Factory method ‘clientRegistrationRepository’ threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve Configuration with the provided Issuer of "http://dev-adfsfa.okta.com/…"

Can you tell me how to run this with java 8

Brian Demers

Hey @disqus_YKH0PlBd5C Java 8 is supported (we test against 8 and 11 currently).

This error looks like a configuration issue. Did your “issuer” value change between tests? or maybe there is a typo? One easy way to test to make sure your issue is valid is to append “/.well-known/oauth-authorization-server” and you should see the discovery json.

Also double check your issuer is in the format of: https://${host}/oauth2/${id} (where “id” is usually the value “default”). Of course you can cut/paste this value from your Okta Admin Console as well.

invzbl3

Thanks for this tutorial, it’s very helpful.

I’ve reproduced the error on last step by running Maven project using Java 11:


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in com.okta.spring.example.controllers.LoginController required a bean of type ‘com.okta.spring.boot.oauth.config.OktaOAuth2Properties’ that could not be found.


Action:

Consider defining a bean of type ‘com.okta.spring.boot.oauth.config.OktaOAuth2Properties’ in your configuration.

And fix was:

rename application.template.yml to application.yml

Matt Raible

The good news is these instructions are in this post!

> The first thing you’ll want to do is to copy the src/main/resources/application.template.yml to src/main/resources/application.yml

:slightly_smiling_face:

invzbl3

Got it, missed this moment. Thanks again :+1: