Ниже мой код для конфигурации безопасности. Пожалуйста, помогите мне с тем, чего мне не хватает в этом коде. Как настроить фильтр таким образом, чтобы аутентификация JWT выполнялась для шаблона URL, отличного от / login и / register
Ядро безопасности Spring: 4.2.3, загрузка пружины: 1.5.4
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.POST, "/login").permitAll()
.antMatchers(HttpMethod.POST, "/users/register").permitAll()
.anyRequest().authenticated()
.and()
// We filter the api/login requests
.addFilterBefore(new LoginFilter("/login", authenticationManager()),
UsernamePasswordAuthenticationFilter.class)
// And filter other requests to check the presence of JWT in header
.addFilterBefore(new NoLoginAuthenticationFilter("/users/register"), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JWTAuthenticationFilter("/**", authenticationManager()),
UsernamePasswordAuthenticationFilter.class);
}
Вы хотите игнорировать определенные URL-адреса. Для этого переопределите метод configure, который принимает объект WebSecurity и игнорирует шаблон.
Попробуйте добавить переопределение метода ниже в свой класс конфигурации.
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/users/register/**");
}
@Mahesh_Loya У меня аналогичная проблема, но, к сожалению, web.ignoring () не решает ее, потому что вызывает ошибку CORS. Есть ли способ исключить шаблоны URL без использования web.ignoring ()?
@Override public void configure (HttpSecurity http) выдает исключение {http.cors (). ConfigurationSource (corsConfigurationSource ()) ....}
@Bean CorsConfigurationSource corsConfigurationSource () {Конфигурация CorsConfiguration = новая CorsConfiguration (); configuration.setAllowedOrigins (Arrays.asList ("")); configuration.setAllowedMethods (Arrays.asList (" GET "," POST "," OPTIONS "," DELETE "," PUT "," PATCH ")); configuration.setAllowedHeaders (Arrays.asList (" X-Requested -With "," Origin "," Content-Type "," Accept "," Authorization "," otherheader ")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (); source.registerCorsConfiguration (" / *", конфигурация); источник возврата;}
он работает после изменения кода, как вам предложено. Спасибо за помощь!!.