Можно ли зарегистрировать фильтр маршрута в приложении.JAVA

Программисты JAVA общаются здесь
Anonymous
Можно ли зарегистрировать фильтр маршрута в приложении.

Сообщение Anonymous »

Я определяю пользовательский фильтр в проекте Spring Gateway, теперь я обнаружил, что должен зарегистрировать пользовательский фильтр в коде, как это: < /p>

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

@Configuration
public class GatewayConfig {

private final JwtAuthenticationFilter filter;

public GatewayConfig(JwtAuthenticationFilter filter) {
this.filter = filter;
}

@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("auth", r -> r.path("/auth/**").filters(f -> f.filter(filter)).uri("lb://auth"))
.route("music", r -> r.path("/music/**").filters(f -> f.filter(filter)).uri("lb://dolphin-music")).build();
}
}
< /code>
Если я не зарегистрирую пользовательский фильтр, фильтр не вступит в силу и будет работать, как и ожидалось. Но я уже настраиваю маршрут в Application.properties 

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

# dolphin music
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=dolphin-music-service
# forward by ip:port way
spring.cloud.gateway.routes[0].uri=http://10.107.64.246:11014
# forward by service name way
# spring.cloud.gateway.routes[0].uri=lb://
spring.cloud.gateway.routes[0].predicates[0]=Path=/music/**
< /code>
Я думаю, что мы не должны определять конфигурацию маршрута как в коде, так и в файле конфигурации, что мне делать только для регистрации фильтра? Есть ли лучший способ справиться с ситуацией? Должен ли я определить маршрут в коде? Я думаю, что config in file будет более гибким.btw, это jwtauthenticationfilter 
:

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

@Component
public class JwtAuthenticationFilter implements GatewayFilter {

@Value("${dolphin.gateway.jwt.verify:true}")
private boolean jwtVerfiy;

final static List apiEndpoints = List.of("/register", "/login");

final static Predicate isApiSecured = r -> apiEndpoints.stream()
.noneMatch(uri -> r.getURI().getPath().contains(uri));

@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {

return chain.filter(exchange);
}
}
Я попытался добавить myfilterfactory

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

@Component
public class MyFilterFactory extends AbstractGatewayFilterFactory {

@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;

public MyFilterFactory() {
super(Config.class);
}

@Override
public GatewayFilter apply(Config config) {
return jwtAuthenticationFilter;
}

public static class Config {
}

}
и определить на заводе по маршруту в Application.properties нравится это:

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

spring.cloud.gateway.routes[0].filters[0]=MyFilterFactory
Проблема заключается в том, что запрос не вводил jwtauthenticationfilter я определен.

Подробнее здесь: https://stackoverflow.com/questions/692 ... -config-in

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