Skip to main content Explore View all products (200+) Microsoft Foundry Azure Copilot GitHub Copilot Azure Kubernetes Service (AKS) Azure Cosmos DB Azure Database for PostgreSQL Azure Arc Microsoft Fabric Linux virtual machines in Azure Foundry Models Foundry Agent Service Foundry IQ Foundry Tools Foundry Control Plane Observability in Foundry Control Plane Azure OpenAI in Foundry Models Azure Speech in Foundry Tools Azure Machine Learning View all databases Azure Cosmos DB Azure DocumentDB Azure SQL Azure Database for PostgreSQL Azure Managed Redis Microsoft Fabric Azure Databricks Linux virtual machines in Azure Windows Server on Azure Azure Functions Azure Virtual Machine Scale Sets Azure API Management Azure Container Apps Azure Kubernetes Service (AKS) Azure Kubernetes Fleet Manager Azure Container Registry Azure Red Hat OpenShift Azure Container Instances Azure Container Storage Azure Arc Azure Local Microsoft Defender for Cloud Azure Monitor Microsoft Sentinel Azure Migrate View all solutions (40+) Cloud solutions for small and medium businesses Cloud migration and modernization center Data analytics for AI Azure Databases AI apps and agents Microsoft Marketplace Microsoft Sovereign Cloud AI apps and agents Responsible AI with Azure AI Infrastructure Data analytics for AI Machine learning operations (MLOps) Low-code application development on Azure Integration Services Serverless computing DevOps Migration and modernization center .NET apps migration Databases on Azure Linux on Azure Oracle on Azure SAP on the Microsoft Cloud Adaptive cloud High-performance computing (HPC) Infrastructure as a service (IaaS) Resiliency Azure Essentials Azure Accelerate FinOps on Azure Microsoft Marketplace Azure pricing overview Create an Azure account Free Azure services Flexible purchase options Pricing calculator FinOps on Azure Maximize ROI from AI Azure savings plans Azure reservations Azure Hybrid Benefit Virtual Machines Azure SQL Microsoft Foundry Microsoft Fabric Azure Kubernetes Service (AKS) Microsoft Defender for Cloud Software Development Companies Microsoft Marketplace Find a partner Get started with Azure Customer stories Analyst reports, white papers, and e-books Videos Learn more about cloud computing Documentation Explore Azure portal Developer resources Quickstart templates Resources for startups Developer community Students Azure for partners Blog Events and Webinars Learn Support Contact Sales Get started with Azure Sign in
  • 2 min read

Use Azure Active Directory with Spring Security 5.0 for OAuth 2.0

We are excited to announce that Spring Starter for Azure Active Directory (AD) is now integrated with Spring Security 5.0. It offers you an easy way to build OAuth2.

We are excited to announce that Spring Starter for Azure Active Directory (AD) is now integrated with Spring Security 5.0. It offers you an easy way to build OAuth2.0 authentication and authorization flow for your Java apps in the cloud, supporting both implicit and authorization code grant types. With only a few lines of configuration, you can build apps that perform authentication with Azure Active Directory OAuth2 and manage authorization with Azure Active Directory groups.

Spring Initializr

Get started

To start, open the Azure portal and register a new application in Azure Active Directory (AD). Next, grant permissions to the newly created application. Use Azure Active Directory’s group and member to set up the access rules. Add the Spring Security Azure AD library to your project. Depending on the kind of application that you’re building, choose from the following two authentication types to build up OAuth2.0 authentication and authorization flow. Learn more about Spring Starter for Azure Active Directory on GitHub.

Back-end authentication

Once the library of Spring Security Azure AD is added to the project, it will automatically map the Azure AD groups and Spring Security authorization logics. It allows developers to build the OAuth2.0 flow in the back end. To enable that, you only need to add the following configurations to specify the usage of OAuth2 User Service. Then you can use the annotation @PreAuthorize(“hasRole(‘GROUP_NAME’)”) for role-based authorization. To learn more, please review our example on GitHub.

@Autowired
private OAuth2UserService<oidcuserrequest, oidcuser=""> oidcUserService;

@Override
protected void configure(HttpSecurity http) throws Exception {
     http
             .authorizeRequests()
             .anyRequest().authenticated()
             .and()
             .oauth2Login()
             .userInfoEndpoint()
             .oidcUserService(oidcUserService);
}</oidcuserrequest,>

Front-end authentication

For a Single Page Application (SPA) scenario, use Azure AD library for JavaScript to handle Azure AD authentication in the front end, and autowire the AADAuthenticationFilter in your Spring Boot project. Then you can use the annotation @PreAuthorize(“hasRole(‘GROUP_NAME’)”) for role-based authorization. Learn more by reviewing the Azure Active Directory Spring Boot sample.

Next steps

Check out our project on GitHub and learn about Spring integrations with Azure services.

Feedback

Please share your feedback and ask questions to help us improve by commenting below or contacting us on GitHub.

English (United States)
Your Privacy Choices Opt-Out Icon Your Privacy Choices
Consumer Health Privacy Sitemap Contact Microsoft Privacy Manage cookies Terms of use Trademarks Safety & eco Recycling About our ads