How to generate access token for testing with Azure AD - spring-boot

We have a spring boot REST API which will be used by mobile client. Azure AD is used for auth. We need to generate access token for initial testing spring boot REST API. What is the recommended option for this?
Basically we need to to generate access token using client credentials and call the REST API end point using the access token. Then on the REST API, validate access token and print the app roles assigned in Azure AD.

To get the access token from Azure AD to authenticate and authorize users from Azure AD.
First, you need to register both the application mobile client and spring boot applications in your Azure AD. Refer register your application in the Azure AD.
To generate access token using client credentials flow, there would be no user involvement, so your server application needs to create an appRole, and then grant the app Role as an application permission to the client application.
The administrator must grant the correct application permissions via a consent process to access the application.
Configure the application's details registered in Azure AD to your spring boot application.properties file with application id, tenant id, client-secret and scope.
Refer guide to configure client application to access a web API using spring boot step by step.

Related

Spring Boot Azure AD B2C User Management

I have a Spring Boot App (REST API only) which I want to connect to Azure AD B2C for the User Management. I created the Active Directory B2C, registered my app, and set up a Sign in and Aign up User Flow. I read a lot about how to connect that to Spring Security for authorization. Also, I saw those two endpoints that I can use:
https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>/oauth2/v2.0/authorize
https://<tenant-name>.b2clogin.com/<tenant-name>.onmicrosoft.com/<policy-name>/oauth2/v2.0/token
But how can I actually CREATE a new User? Is there an endpoint for the sign up, that I can call with an email and password and that creates a User Account in my Azure AD B2C Tenant?

How can we configure a Spring boot application to perform AWS ADFS login?

I have a Spring boot application in which users can log in and their credentials would then be used to perform an AWS ADFS login.
A preconfigured role will be assumed and we'll get an access key, secret key, and session token in return. These will then be used to create the S3 client to perform file upload/download operations.
My issue is how can we perform the AWS ADFS login from the java spring boot code? Is there a library that supports it out of the box?
Also, I understand the session obtained this way is short-lived. Is there a way to refresh the session so that these tokens do not expire? Or should the whole AWS authentication, s3 client creation be done every time an upload/download request comes in?

generate azure ad jwt token and call the thrid party api using those token using spring boot

I registered the app in an azure ad, and I have application id(client id) and directory id (tenant id), and secret key.
using this need to generate the jwt token in the azure ad and need to validate that token.
once the token generate need to call the third-party API's using that token in spring boot application.
please provide some example
To achieve the above requirement. You can take Reference of this Post to call the third-party Api using the JWT token using spring boot application.
In the above, there is tutorial they have built a Spring Boot Application that supports Token based Authentication with JWT. Please refer the section How to configure Spring Security to work with JWT that might be solution of requirement.

How does spring security knows which user has been authenticated in azure ad

I am using spring boot with angular for azure ad authentication. In angular i have used Microsoft adal library for authentication. From that I am getting an access token, and passing as header with request to spring boot app. But When i am retrieving SecurityContext object, i am getting anonymous user.
Now how does spring security knows about this user login. Do i need to explicitly do any code for this to get done?
Configure properly azure-spring-boot (ad client id and secret) and then in WebSecurityConfigurerAdapter filter your requests with AADAuthenticationFilter. Check out a sample here.

Spring Boot REST service – End User Authentication vs APP (REST client) Authentication

I have gone through many posts and articles but didn't find a straightforward solution for the case below which I have to implement.
Platform: Spring Boot 2.x.x (Spring Security 5.x.x) with embed Tomcat
Solution: REST service that consume many client apps and many end users.
I have to implement a REST end point /api/search which accessible for many client application. As an example, web application APP-X (Angular), web application APP-Y(Jquery/Bootstrap) and mobile application APP-Z (IOS). All three clients are separate entities (both technical perspective and business perspective).
So I have to authenticate above application using onetime token. Therefore I planned to go for Spring OAuth2 by enabling #EnableAuthorizationServer and #EnableResourceServer. For each app client I’ll generate a token and they can use it when they connect with my REST service. Is this approach correct?
Apart from the app clients system has capability to register and login functionality for end users. Also my end point (/api/search) can access both anonymous users and users who registered under ROLE_REGUSER role. And through the security context, I need to access the user details as usual user authentication.
This is the place I got stuck. How can I handle the following points together using Spring Security 5.x.x (Spring Boot 2.x.x).
I. Both client apps and end users authentications.
II. Allow access for anonymous users and registered users for same end point.
I have attached small diagram to elaborate the above scenario.
Thanks
I found a solution when I upgraded my spring security version to 5.2. In version 5.2, they have depreciated #EnableAuthorizationServer and #EnableResourceServer. So I had to move with an external authorization provider who supports auth2. I chose AWS Cognito, and fulfill the above requirement, using the user pool option.
In AWS Cognito
I created a user pool.
Then created two app clients in the same user pool.
One app client configured as support to the client credentials flow.
The second app client configured as support to the user authentication flow.
In client applications
Retrieve access token directly from AWS Cognito using client credentials and used to secure all API calls.
If a user login at any stage, retrieve access token directly from AWS Cognito using the authorization code and replace any existing access token.
The advantage is, the resources server can validate any access token that generated related to the same user pool.
In resources server (backend API/Spring Boot)
Validate access token.

Resources