SecurityConfig в SpringBoot 3JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 SecurityConfig в SpringBoot 3

Сообщение Anonymous »

Я перехожу с SpringBoot 2 на SpringBoot3. Я собираюсь реализовать аутентификацию на основе случайно сгенерированных токенов. Я создал конечные точки для входа и регистрации.
У меня есть список общедоступных URL-адресов (например, /login, /register и т. д.) и фильтр для аутентификации. Моя проблема в том, что когда я вызываю один из этих общедоступных URL-адресов, запрос также фильтруется (но не должен быть), и я получаю статус 401. Для остальных конечных точек все работает нормально.
Мой SecurityConfis.class:

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

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {

@Autowired
private CustomAuthenticationFilter customAuthenticationFilter;

/**
* Końcówki nie wymagające autoryzacji
*/
public static final RequestMatcher PUBLIC_URLS = new OrRequestMatcher(
new AntPathRequestMatcher("/api/auth/login"),
new AntPathRequestMatcher("/api/auth/register")
);

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

var protectedUrls = new NegatedRequestMatcher(PUBLIC_URLS);

http
.securityMatcher(protectedUrls)
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
.csrf(csrf -> csrf.disable())
.sessionManagement(sessionManagement ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.exceptionHandling(exceptionHandling ->
exceptionHandling.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.FORBIDDEN)))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/api/data-transfer/**").hasAnyAuthority("ADMIN")
.requestMatchers("/api/discount-code/**").hasAnyAuthority("USER")
.anyRequest().authenticated())
.addFilterAfter(customAuthenticationFilter, BasicAuthenticationFilter.class);

return http.build();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
[...]
}
}
CustomAuthenticationFilter.class

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

@Component
public class CustomAuthenticationFilter extends OncePerRequestFilter {

private static final String BEARER_PREFIX = "Bearer ";

@Autowired
private UserCrudService userCrudService;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
[...]
}
}
Как запретить вызов CustomAuthenticationFilter при доступе к /api/auth/login или /api/auth/register??

Подробнее здесь: https://stackoverflow.com/questions/792 ... ringboot-3
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

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