.Authenticated () не работает, как и ожидалось, какая причина?JAVA

Программисты JAVA общаются здесь
Anonymous
.Authenticated () не работает, как и ожидалось, какая причина?

Сообщение Anonymous »

Здесь я даю разрешение на определенные ссылки и ссылки, которые должны работать только после аутентификации. < /p>

Код: Выделить всё

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {

private final UserService userService;
private final JwtAuthenticationFilter jwtAuthenticationFilter;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

http.csrf(AbstractHttpConfigurer::disable)
.cors(cors -> cors.configurationSource(request -> {
var corsConfig = new CorsConfiguration();
corsConfig.setAllowedOriginPatterns(List.of("*"));
corsConfig.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTION"));
corsConfig.setAllowedHeaders(List.of("*"));
corsConfig.setAllowCredentials(true);
return corsConfig;
}))
.authorizeHttpRequests(authorize ->
authorize.requestMatchers("/api/v1/auth/**")
.permitAll()
.requestMatchers("/api/v1/auth/login/**")
.permitAll().anyRequest()
.authenticated())
.httpBasic(Customizer.withDefaults())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

return http.build();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userService.userDetailsService());
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}
Когда я использую этот URL http://127.0.0.1:8080/api/v1/auth/login/create. Это работает без аутентификации
Мое ожидание - это то, что http://127.0.0.1:8080/api/autheatm Когда пользователь вошел в систему.

Подробнее здесь: https://stackoverflow.com/questions/792 ... -the-cause

Вернуться в «JAVA»