Авторизация и Аутенификация JWT в микросервисной архитектуре

У меня есть сервис auth,в котором лежат два JWT-фильтра для аутентификация и авторизации, принципиал и другие конфигурации, есть второй сервис lib-commons, в котором лежат общие вещи для всех сервисов, включая SecurityConfiguration, чтобы каждый микросервис его использовал, эта конфигурация:

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
@Qualifier("customAuthenticationProvider")
private AuthenticationProvider authenticationProvider;

@Autowired
private CustomAuthEntryPoint customAuthEntryPoint;

//
// Beans
//

@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("OPTIONS");
    config.addAllowedMethod("GET");
    config.addAllowedMethod("POST");
    config.addAllowedMethod("PUT");
    config.addAllowedMethod("DELETE");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
}

//
// Overrides
//

@Override
protected void configure(AuthenticationManagerBuilder auth) {
    auth.authenticationProvider(authenticationProvider);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .cors()
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(customAuthEntryPoint)
            .and()
            .csrf().disable()
            .httpBasic().and()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers(HttpMethod.POST, "/auth/register").permitAll()
            .antMatchers(HttpMethod.POST, "/auth/login").permitAll()
            .antMatchers(HttpMethod.GET, "/v2/api-docs",
                    "/configuration/ui",
                    "/swagger-resources/**",
                    "/configuration/security",
                    "/swagger-ui.html",
                    "/webjars/**",
                    "/csrf").permitAll()
            .anyRequest().authenticated()
            .and()
            .addFilter(new JWTAuthenticationFilter(authenticationManager()))
            .addFilter(new JWTAuthorizationFilter(authenticationManager()))
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}

В этой конфигурации я добавляю два фильтра для JWT, которые лежат в auth-сервисе, но это создает циклическую зависимость в gradle, так как в auth я добавляю проект lib-commons, а в lib-commons наоборот. Идея переместить JWT-фильтры в lib-commons, мне кажется, немного неправильной. Как мне лучше стоит сделать аутенфикацию и авторизацию JWT для микросервисов?


Ответы (0 шт):