Spring security 6.0 - oauth2Login with MS Azure and CSRF token - how to login there in Postman?
I'm beginner in Spring Security configuration and my purpose is set 2 things:
- CSRF token
- Authorization by Microsoft Azure
Firstly I've added oauth client in properties:
spring.security.oauth2.client.provider.azure.issuer-uri=https://login.microsoftonline.com/${id}/v2.0
spring.security.oauth2.client.registration.azure-dev.provider=azure
spring.security.oauth2.client.registration.azure-dev.client-id=${clientId}
spring.security.oauth2.client.registration.azure-dev.client-secret=${secret}
spring.security.oauth2.client.registration.azure-dev.scope=openid,email,profile
And all my GET endpoints started proxy my requests to Microsoft Login page -> after login all GET endpoints are available.
So I created security config:
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authz -> authz
.requestMatchers("/").permitAll()
.requestMatchers("/oauth2/authorization/azure-dev").permitAll()
.requestMatchers("/login").permitAll()
.requestMatchers(HttpMethod.POST, "/logout").permitAll()
.requestMatchers( "/api/v1/**").authenticated()
.requestMatchers(HttpMethod.POST, "/api/v1/**").authenticated()
.requestMatchers(HttpMethod.PUT, "/api/v1/**").authenticated()
)
.csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()))
.logout(l -> l
.logoutSuccessUrl("/")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")))
.oauth2Login(Customizer.withDefaults());
return http.build();
}
}
Option withHttpOnlyFalse
should add CSRF token to headers automatically, but I couldn't find it in GET requests, but it probably will be add only to POST/PUT/DELETE requests. Then I tried to send some requests from Postman, but it's secured under Microsoft Azure login, and response is always:
<html dir="ltr" class="" lang="en">
<head>
<title>Sign in to your account</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes">
<meta http-equiv="Pragma" content="no-cache">
(...)
So it's HTML to login page, but I can't open it, or handle (in browser after log in one time - I have access all the time to all requests). Again, I can't find any header/cookie which auth me in browser (to paste it in postman).
- Where is this oAuth2 key, after login to Microsoft in Browser? I want to copy it to postman (I even tried to get it from
OAuth2AuthorizedClient authorizedClient
withauthorizedClient.getAccessToken().getTokenValue()
, and there was value, but this token doesn't work in postman -> it still returns microsoft HTML page). - Is spring security configured correctly in your opinion?
- I'm wondering how to auth with this login page and csrf tokens from frontend, if I can't find any tokens in browser? Should I create some special endpoint (in Spring app)?
Comments
Post a Comment