I am trying my hands on Spring-cloud-gateway. While going through the documentation I found that we can configure routes not only in yml/ properties file, but also using Fluent Routes API. Here is the snippet from the documentation.
#Bean
public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) {
return Routes.locator()
.route("test")
.predicate(host("**.abc.org").and(path("/image/png")))
.addResponseHeader("X-TestHeader", "foobar")
.uri("http://httpbin.org:80")
.route("test2")
.predicate(path("/image/webp"))
.add(addResponseHeader("X-AnotherHeader", "baz"))
.uri("http://httpbin.org:80")
.route("test3")
.order(-1)
.predicate(host("**.throttle.org").and(path("/get")))
.add(throttle.apply(tuple().of("capacity", 1,
"refillTokens", 1,
"refillPeriod", 10,
"refillUnit", "SECONDS")))
.uri("http://httpbin.org:80")
.build();
}
But I am not able to find this class Routes. Not sure If I have missed anything. I am using spring boot 2.0.0.M7 and I have spring-cloud-starter-gateway depependecy included.
Any idea ?
Routes is no longer available. Add a RouteLocatorBuilder parameter to customRouteLocator. I'll fix the docs.
Related
Im using spring boot 2.3.2.RELEASE with spring-cloud-config-server 2.2.4.RELEASE. Im trying to implement the spring-doc-openapi (1.4.3) in a existing project. If i add #EnableConfigServer in one the configuration class file, the swagger-ui.html endpoint returns a weird json:
"name":"swagger-ui",
"profiles":[
"index.html"
],
"label":null,
"version":null,
"state":null,
"propertySources":[
]
}
and not the the swagger ui as expected. Im not sure if its a bug, but would appreciate any kind of help.
Not sure if its relevant to add the springdoc dependency on spring cloud config server, unless you need to explore some APIs on the config server it self.
Here is the link of a fully working example using springdoc with config server:
https://github.com/springdoc/springdoc-openapi-demos/tree/master/sample-springdoc-openapi-microservices
And this is the link of a blog which explains the natural usage with microservies and spring cloud modules:
https://piotrminkowski.com/2020/02/20/microservices-api-documentation-with-springdoc-openapi/
Answer from #brianbro seems not to be working anymore...
Verified on: springdoc-openapi v1.6.6 and org.springframework.cloud:spring-cloud-config-server:v2.2.4.RELEASE
Here is how I solved it:
List item spring.cloud.config.server.prefix=config-server - please note that any request to config server will require to add prefix!
Add following bean (sample implementation in Kotlin)
#Bean fun configServerApi(): GroupedOpenApi =
GroupedOpenApi.builder()
.group("Config server")
.pathsToMatch(
"/config-server/**"
)
.build()
Now you should be able to reach swagger ui :)
I am new to Spring Boot and my current project is a REST API developed in Spring Webflux. The goal is to have an endpoint which has an optional JWT Token, allowing you ti create things anonymously or not. But all the starter guides to Spring Security are really complicated and use Spring MVC, as far as I can tell.
Now my idea was to create a HandlerFilterFunction looking like
class AuthenticationFilter : HandlerFilterFunction<ServerResponse, ServerResponse> {
override fun filter(request: ServerRequest, next: HandlerFunction<ServerResponse>): Mono<ServerResponse> {
val authHeader = request.headers().header("Authorization").firstOrNull()
// get user from database
request.attributes()["user"] = user
return next.handle(request)
}
}
and adding it to the router {...} bean.
Is this a good idea, or should I go another router? If so, can somebody point me towards a JWT tutorial for Spring Webflux.
The Spring Security docs point to a JWT-Based WebFlux Resource Server sample in the codebase.
It's not Kotlin-based, so I also posted a sample of my own just now; hopefully, it helps get you started.
As for your question, yes, you can create a filter of your own, though Spring Security ships with a BearerTokenAuthenticationFilter that already does what your filter would likely do. The first linked sample adds this filter manually while the second sample lets Spring Boot add it.
I am trying to expose cxf web services from my Spring Boot Application. I am trying to use the cxf-spring-boot-starter-jaxws v3.2.6. The documentation for the starter is a bit out of date. The examples shows the following for exposing and endpoint.
#Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
endpoint.publish("/Hello");
return endpoint;
}
But now the EndpointImpl takes 3 arguments and I am trying to understand how to construct one.
My mistake, I was using the EndpointImpl from the wrong package. I was trying to use org.apache.cxf.endpoint.EndpointImpl and not org.apacke.cxf.jaxws.EndpointImpl
It is possible to get hystrix metrics information in a similar way as done in the spring-boot 1.5?
In 1.5 metric endpoint I have something like this:
"gauge.servo.hystrix.hystrixcommand.[service-name].[method].errorcount": 0,
"gauge.servo.hystrix.hystrixcommand.[service-name].[method].requestcount": 0,
"gauge.servo.hystrix.hystrixcommand.[service-name].[method].latencytotal_mean": 0,
But now with actuator/metric endpoint that uses Micrometer, I can't find any reference to the terms "hystrix", "netflix", etc.
I alredy configured my application with #EnableCircuitBreaker and hystrix:metrics:enabled: true.
There is some way tho get this information without using the hystrix.stream endpoint as I was able before? Or this should be working and am I doing something wrong?
Following this tutorial you have to add the following bean to your application to register the Hystrix Metrics Binder:
#Configuration
public class MetricsConfig {
#Bean
HystrixMetricsBinder registerHystrixMetricsBinder() {
return new HystrixMetricsBinder();
}
}
As explainend in this issue, hystrix metrics should now show up like this:
hystrix.errors{event="short_circuited"}
How to use propeties in application.properties in SpringBoot 2.0.0.M7 App?
I have foloowed thedocumentation, but I dont know if I need to use OAuth2ClientProperties excplicitly
One more thing, the documentation doesn't according wih the autocompletion about
syntax parameter
my application.properies :
spring.security.oauth2.client.provider.verimi.authorization-uri=https://verimi.com/dipp/api/oauth/authorize
spring.security.oauth2.client.provid
spring.security.oauth2.client.registration.verimi.scope=login
spring.security.oauth2.client.registration.verimi.authorization-grant-typeer.verimi.tokenUri=https://verimi.com/dipp/api/oauth/token
spring.security.oauth2.client.registration.verimi.client-id=dipp
spring.security.oauth2.client.registration.verimi.clientSecret=G|41|0an18ZIs_w
spring.security.oauth2.client.registration.verimi.provider=verimi=authorization_code
OAuthConfig :
#Configuration
#EnableOAuth2Client
class OAuth2Config {
// What do I need to add ?
#Bean
fun oauth2RestTemplate(oauth2ClientContext: OAuth2ClientContext,
details: OAuth2ProtectedResourceDetails): :/* <--Error here : not bean found*/ OAuth2RestTemplate = OAuth2RestTemplate(details, oauth2ClientContext)
}
THX
Verimi does not use plain OAuth2 but OpenID Connect which uses OAuth2 as authorization protocol. A few days ago I managed to make the official Verimi Spring Boot sample work. If this might help you I have pushed it in Github.