Программисты JAVA общаются здесь
Anonymous
.authenticated() не работает должным образом
Сообщение
Anonymous » 07 дек 2024, 22:01
Здесь даю авторизацию для определенных ссылок и ссылок, которые должны работать только после аутентификации.
Код: Выделить всё
@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/v1/auth/login/create будет работать только тогда, когда пользователь вошел в систему.
Подробнее здесь:
https://stackoverflow.com/questions/792 ... s-expected
1733598107
Anonymous
Здесь даю авторизацию для определенных ссылок и ссылок, которые должны работать только после аутентификации. [code]@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(); } } [/code] когда я использую этот URL-адрес http://127.0.0.1:8080/api/v1/auth/login/create, он работает без аутентификации Я ожидаю, что http://127.0.0.1:8080/api/v1/auth/login/create будет работать только тогда, когда пользователь вошел в систему. Подробнее здесь: [url]https://stackoverflow.com/questions/79227339/authenticated-is-not-working-as-expected[/url]