|
| 1 | +package com.ericsson.ei.frontend.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Value; |
| 4 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 5 | +import org.springframework.context.annotation.Bean; |
| 6 | +import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 8 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 9 | +import org.springframework.security.config.http.SessionCreationPolicy; |
| 10 | +import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator; |
| 11 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 12 | +import org.springframework.security.oauth2.core.OAuth2TokenValidator; |
| 13 | +import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult; |
| 14 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 15 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 16 | +import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; |
| 17 | +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter; |
| 18 | + |
| 19 | +@SuppressWarnings("deprecation") |
| 20 | +@Configuration |
| 21 | +@ConditionalOnProperty(name = "spring.cloud.azure.active-directory.enabled", havingValue = "true") |
| 22 | +public class SecurityConfigAzureAD extends WebSecurityConfigurerAdapter { |
| 23 | + |
| 24 | + @Value("${spring.cloud.azure.active-directory.credential.client-id}") |
| 25 | + private String clientId; |
| 26 | + |
| 27 | + @Value("${spring.cloud.azure.active-directory.profile.tenant-id}") |
| 28 | + private String tenantId; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void configure(HttpSecurity http) throws Exception { |
| 32 | + http |
| 33 | + .csrf().disable() |
| 34 | + .authorizeRequests() |
| 35 | + .antMatchers("/authentication/*","/status","/actuator/**").permitAll() |
| 36 | + .anyRequest().authenticated() |
| 37 | + .and() |
| 38 | + .oauth2Login() // Enable Azure MFA for H2M |
| 39 | + .and() |
| 40 | + .sessionManagement() |
| 41 | + .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) |
| 42 | + .and() |
| 43 | + .oauth2ResourceServer() |
| 44 | + .jwt() |
| 45 | + .jwtAuthenticationConverter(jwtAuthenticationConverter()) // Configure JWT converter |
| 46 | + .decoder(jwtDecoder()); // Configure JWT decoder to handle client credentials flow |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public JwtAuthenticationConverter jwtAuthenticationConverter() { |
| 51 | + JwtAuthenticationConverter converter = new JwtAuthenticationConverter(); |
| 52 | + // You can add additional configuration for authorities mapping here |
| 53 | + return converter; |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + public JwtDecoder jwtDecoder() { |
| 58 | + // Use Azure AD's JWKS endpoint |
| 59 | + String jwkSetUri = "https://login.microsoftonline.com/" + tenantId + "/discovery/v2.0/keys"; |
| 60 | + NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build(); |
| 61 | + |
| 62 | + // token validators (e.g., audience, issuer) to validate the token properly |
| 63 | + OAuth2TokenValidator<Jwt> audienceValidator = new OAuth2TokenValidator<Jwt>() { |
| 64 | + @Override |
| 65 | + public OAuth2TokenValidatorResult validate(Jwt token) { |
| 66 | + if (!token.getAudience().contains("api://" + clientId)) { |
| 67 | + return OAuth2TokenValidatorResult.failure(new OAuth2Error("invalid_audience", "Invalid audience", null)); |
| 68 | + } |
| 69 | + return OAuth2TokenValidatorResult.success(); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + OAuth2TokenValidator<Jwt> issuerValidator = new OAuth2TokenValidator<Jwt>() { |
| 74 | + @Override |
| 75 | + public OAuth2TokenValidatorResult validate(Jwt token) { |
| 76 | + String issuer = token.getIssuer().toString(); |
| 77 | + // Validate the issuer for both v1.0 and v2.0 endpoints |
| 78 | + if (!issuer.equals("https://login.microsoftonline.com/" + tenantId +"/v2.0") && !issuer.equals("https://sts.windows.net/" + tenantId + "/")) { |
| 79 | + return OAuth2TokenValidatorResult.failure(new OAuth2Error("invalid_issuer", "Invalid issuer", null)); |
| 80 | + } |
| 81 | + return OAuth2TokenValidatorResult.success(); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + // Chain validators together (audience and issuer) |
| 86 | + OAuth2TokenValidator<Jwt> tokenValidator = new DelegatingOAuth2TokenValidator<>(audienceValidator, issuerValidator); |
| 87 | + jwtDecoder.setJwtValidator(tokenValidator); // Apply validators to the decoder |
| 88 | + return jwtDecoder; |
| 89 | + } |
| 90 | +} |
0 commit comments