USe application.properties for OAuth - spring-boot

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.

Related

Use Micrometer with OpenFeign in spring-boot application

The OpenApi documentation says that it supports micrometer. How does the integration works? I could not find anything except this little documentation.
I have a FeignClient in a spring boot application
#FeignClient(name = "SomeService", url = "xxx", configuration = FeignConfiguration.class)
public interface SomeService {
#GET
#Path("/something")
Something getSomething();
}
with the configuration
public class FeignConfiguration {
#Bean
public Capability capability() {
return new MicrometerCapability();
}
}
and the micrometer integration as a dependency
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-micrometer</artifactId>
<version>10.12</version>
</dependency>
The code makes a call but I could not find any new metrics via the actuator overview, expecting some general information about my HTTP requests. What part is missing?
Update
I added the support for this to spring-cloud-openfeign. After the next release (2020.0.2), if micrometer is set-up, the only thing you need to do is putting feign-micrometer onto your classpath.
Old answer
I'm not sure if you do but I recommend to use spring-cloud-openfeign which autoconfigures Feign components for you. Unfortunately, it seems it does not autoconfigure Capability (that's one reason why your solution does not work) so you need to do it manually, please see the docs how to do it.
I was able to make this work combining the examples in the OpenFeign and Spring Cloud OpenFeign docs:
#Import(FeignClientsConfiguration.class)
class FooController {
private final FooClient fooClient;
public FooController(Decoder decoder, Encoder encoder, Contract contract, MeterRegistry meterRegistry) {
this.fooClient = Feign.builder()
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.addCapability(new MicrometerCapability(meterRegistry))
.target(FooClient.class, "https://PROD-SVC");
}
}
What I did:
Used spring-cloud-openfeign
Added feign-micrometer (see feign-bom)
Created the client in the way you can see above
Importing FeignClientsConfiguration and passing MeterRegistry to MicrometerCapability are vital
After these, and calling the client, I had new metrics:
feign.Client
feign.Feign
feign.codec.Decoder
feign.codec.Decoder.response_size

How to configure DispatcherType's for SecurityFilterAutoConfiguration?

I have a Spring Boot (2.1.5) application which uses the SecurityFilterAutoConfiguration feature. During registration of DelegatingFilterProxyRegistrationBean only REQUEST, ASYNC, ERROR DispatcherTypes are set. But I need FORWARD and INCLUDE as well.
The property security.filter-dispatcher-types from Spring Boot 1.x no longer works.
I can work around the problem by "overwriting" the DelegatingFilterProxyRegistrationBean as follows:
#Bean
#ConditionalOnBean(name = DEFAULT_FILTER_NAME)
#Primary
public DelegatingFilterProxyRegistrationBean customSecurityFilterChainRegistration(SecurityProperties securityProperties) {
DelegatingFilterProxyRegistrationBean registration = new DelegatingFilterProxyRegistrationBean(DEFAULT_FILTER_NAME);
registration.setOrder(securityProperties.getFilter().getOrder());
registration.setDispatcherTypes(allOf(DispatcherType.class));
return registration;
}
But that doesn't seem like a very elegant solution to me.
Is there a way to configure this for Spring Boot 2.1.x explicit?
You have to use spring.security.filter.dispatcher-types, see Spring Boot 2.0 Configuration Changelog.

Spring cloud gateway cannot find Fluent Java Routes API

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.

Implement multi-tenanted application with Keycloak and springboot

When we use 'KeycloakSpringBootConfigResolver' for reading the keycloak configuration from Spring Boot properties file instead of keycloak.json.
Now there are guidelines to implement a multi-tenant application using keycloak by overriding 'KeycloakConfigResolver' as specified in http://www.keycloak.org/docs/2.3/securing_apps_guide/topics/oidc/java/multi-tenancy.html.
The steps defined here can only be used with keycloak.json.
How can we adapt this to a Spring Boot application such that keycloak properties are read from the Spring Boot properties file and multi-tenancy is achieved.
You can access the keycloak config you secified in your application.yaml (or application.properties) if you inject org.keycloak.representations.adapters.config.AdapterConfig into your component.
#Component
public class MyKeycloakConfigResolver implements KeycloakConfigResolver {
private final AdapterConfig keycloakConfig;
public MyKeycloakConfigResolver(org.keycloak.representations.adapters.config.AdapterConfig keycloakConfig) {
this.keycloakConfig = keycloakConfig;
}
#Override
public KeycloakDeployment resolve(OIDCHttpFacade.Request request) {
// make a defensive copy before changing the config
AdapterConfig currentConfig = new AdapterConfig();
BeanUtils.copyProperties(keycloakConfig, currentConfig);
// changes stuff here for example compute the realm
return KeycloakDeploymentBuilder.build(currentConfig);
}
}
After several trials, the only feasible option for spring boot is to have
Multiple instances of the spring boot application running with different spring 'profiles'.
Each application instance can have its own keycloak properties (as it is under different profiles) including the realm.
The challenge is to have an upgrade path for all instances for version upgrades/bug fixes, but I guess there are multiple strategies already implemented (not part of this discussion)
there is a ticket regarding this problem: https://issues.jboss.org/browse/KEYCLOAK-4139?_sscc=t
Comments for that ticket also talk about possible workarounds intervening in servlet setup of the service used (Tomcat/Undertow/Jetty), which you could try.
Note that the documentation you linked in your first comment is super outdated!

Apache CXF Spring Java Config for JAXWS Endpoint

I'm trying to configure Spring with Apache CXF using java config (no XML config) and wanted to know how to register JAXWS endpoints using spring java config. For example, what would be the 'java config' equivalent for the XML config below?
<jaxws:endpoint id="reportService" implementor="#reportServ" address="/reportService"/>
Kind regards,
Zahanghir
The 'Java-config' equivalent of your XML configuration is something like :
#Configuration
public class CXFConfiguration {
#Autowired
private ReportService reportServ;
#Bean
public Endpoint endpoint() {
Endpoint endpoint = new EndpointImpl(reportServ);
endpoint.publish("/reportService");
return endpoint;
}
}
I hope this can help you ^^.
Unfortunately, from what I can tell KevinHol's answer doesn't actually work. A working answer can be found at the sister thread (Apache CXF + Spring Java config (no XML)).

Resources