Программисты JAVA общаются здесь
-
Anonymous
Spring Security: Basic Auth in Postman выходит из строя, но кодируемые формируемыми работа
Сообщение
Anonymous »
Я работаю над приложением Spring Boot с Spring Security для аутентификации и сталкивался с проблемой при тестировании с почтальником. < /p>
- с xwww-form-urlencoded ) работает отлично. /> Базовая аут () в почтальстве приводит к «плохим учетным данным». Иногда почтальон возвращает страницу входа в систему HTML вместо json. < /Li>
< /ul>
Код: Выделить всё
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable()) // CSRF disabled for testing
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.POST, "/api/register").permitAll() // Allow registration
.requestMatchers(HttpMethod.GET, "/api/verify").permitAll() // Allow email verification
.requestMatchers("/api/admin/**").hasRole("ADMIN") // Restricted access
.requestMatchers("/api/user/**").hasRole("USER") // Restricted access
.anyRequest().authenticated() // Secure all other endpoints
)
.formLogin(form -> form
.loginProcessingUrl("/login") // ✅ Form-based login enabled
.defaultSuccessUrl("/api/profile", true)
.permitAll()
)
.httpBasic(basic -> basic
.realmName("API Authentication") // ✅ Basic Auth enabled
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) // Use sessions where needed
)
.build();
}
< /code>
Я пробовал: < /p>
Проверенные учетные данные (они работают в логине формы). < /p>
[*] проверен httpbasic ()
включена в SecurityConfig .
, но нет разницы.
Подробнее здесь:
https://stackoverflow.com/questions/793 ... ogin-works
1738142325
Anonymous
Я работаю над приложением Spring Boot с Spring Security для аутентификации и сталкивался с проблемой при тестировании с почтальником. < /p>
[list]
[*][code]POST /login[/code] с xwww-form-urlencoded ) работает отлично. /> Базовая аут ([code]Authorization: Basic ...[/code]) в почтальстве приводит к «плохим учетным данным». Иногда почтальон возвращает страницу входа в систему HTML вместо json. < /Li>
< /ul>
[code]@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable()) // CSRF disabled for testing
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.POST, "/api/register").permitAll() // Allow registration
.requestMatchers(HttpMethod.GET, "/api/verify").permitAll() // Allow email verification
.requestMatchers("/api/admin/**").hasRole("ADMIN") // Restricted access
.requestMatchers("/api/user/**").hasRole("USER") // Restricted access
.anyRequest().authenticated() // Secure all other endpoints
)
.formLogin(form -> form
.loginProcessingUrl("/login") // ✅ Form-based login enabled
.defaultSuccessUrl("/api/profile", true)
.permitAll()
)
.httpBasic(basic -> basic
.realmName("API Authentication") // ✅ Basic Auth enabled
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) // Use sessions where needed
)
.build();
}
< /code>
Я пробовал: < /p>
Проверенные учетные данные (они работают в логине формы). < /p>
[*] проверен httpbasic () [/code] включена в SecurityConfig .
, но нет разницы.
[/list]
Подробнее здесь: [url]https://stackoverflow.com/questions/79396200/spring-security-basic-auth-in-postman-fails-but-form-encoded-login-works[/url]