how to make keycloak work with embedded jetty? - java

Hello every one I have just been trying out key-cloak for IAM and it seems a great tool to me but one thing that I can't seem to figure out is how to integrate it with my current JAX-rs web services that run on an embedded jetty container. As far as I have searched no key-cloak client adapters exist for embedded jetty server and the jetty 9.x adapters only seem to work for jetty standalone (non-embedded) server. Does this mean I have to implement my own client adapter by implementing ContaineRequestFilter class and analyzing the request headers and utilizing the token introspection endpoint for resource server authorization? Also is this how all other client adapters work or is it some other way?

There are two interesting projects on github, which both use Spring Boot:
https://github.com/Baeldung/spring-security-oauth/tree/master/oauth-sso/sso-authorization-server
Documented here: https://www.baeldung.com/keycloak-embedded-in-spring-boot-app
If you prefer Jetty as embedded webserver, change the pom.xml:
Exclude tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Add Jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>provided</scope>
</dependency>
Also this project is interesting: https://github.com/thomasdarimont/embedded-spring-boot-keycloak-server

Related

Which Jakarta-light-web-server is to implement jax-ws and JMS specification?

I have an application in which the following modules are:
1- Jax-ws
2- JMS and its implementation by Activemq with the following dependency:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
3- the version of JDK is 1.8
4- Its application server is Wildfly
I want to change and upgrade my application to JDK 17(LTS), due to this migration the javax package is replaced with jakarta package.
My question is:
1- What except Wildfly is which supports Jakarta-ws and jakarta-jms? because Wildfly is so heavy for my solution.
2- Before for JMS configuration, the following dependency were:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.4.0</version>
</dependency>
after migration to JDK-17, which dependency should to add to implement jakarta-jms by activemq
Check out OpenLiberty, it is fully open source and supports Java 17 and Jakarta EE 8 and 9. Moreover its fully composable, so you can pick and choose what features (APIs) you need in the server.
WildFly can be easily reduced to your usage since you can easily disable what you don't need. You may want to take a look at Galleon as a way to provision your server tailored to your need.

Spring boot application stuck when adding jnpclient maven dependency

I need to load a JNDI resource located in a JBoss server. For this, I am using a jnp-client library:
<dependency>
<groupId>jboss</groupId>
<artifactId>jnp-client</artifactId>
<version>4.2.2.GA</version>
</dependency>
The problem is that as soon as I include this dependency in my pom.xml, the Spring application gets stuck at the start time without any message. Not a single line of logs, like if it is trying to load something forever. The main is not even invoked. Removing this dependency the application runs but I get an expected:
ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
Which is normal because I have removed the dependency that includes this class.
Any clue?
I have tried all version of jnp-client or jbossall-client that also includes this NamingContextFactory
EDIT:
It is the mix of these two dependencies that unfortunately are inherited from required dependencies:
<dependency>
<groupId>org.jboss.naming</groupId>
<artifactId>jnp-client</artifactId>
</dependency>
and spring-webmvc inside
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
If I run with
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>
It works fine.
Now, if I exclude the jnp-client I cannot access the JNDI resource, but if I remove the spring-security-web I cannot access the REST endopoints this service exposes.
EDIT2:
So I thought it could be two classes interfering with each other and I started the java process with -verbose. This is what I found.
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$165/0x00000001002d8440
source:
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader
[389.020s][info][class,load]
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$166/0x00000001002d8840
source:
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader
[558.011s][info][class,load]
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$167/0x00000001002d8c40
source:
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader
[558.012s][info][class,load]
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$168/0x00000001002d9040
source:
org.springframework.boot.context.config.ConfigFileApplicationListener$Loader
From time to time the application prints, only in verbose, this ConfigFileApplicationListener$Loader
EDIT 3:
It seems that the problem is TimedSocketFactory inside the jnp-client. Somehow, just by having this class inside the jnp-client dependency, the Spring application is blocked and never starts. Removing this from the jnp-client allows the application to start. Of course, it fails later because the JNDI cannot connect to the server.

Change the version of the embedded tomcat using spring starter

The requirement is to use spring boot version 2.2.6.RELEASE however the tomcat version should be 9.0.37.
I tried to do it by excluding the tomcat starter from the spring-boot-starter-web depdendency like so :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
And added the spring-boot-starter-tomcat separately which has the 9.0.37 tomcat version :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
However even after doing so the version is not override and the following gets used :
Mar 11 2020 09:31:38 UTC
Apache Tomcat/9.0.33
9.0.33.0
Do we need to do anything else to override the tomcat version?
Is it possible to override it by excluding started?
Update:
The parent pom is a corporate parent pom and not the spring-boot-starter-parent. As per one of the answers we can simply override the tomcat.version property however my effective pom doesn't show it.
If you're not inheriting from spring-boot-starter-parent I can only guess that you import spring-boot-dependencies in your dependencies management somewhere.
The documentation covers what you need to do to override the tomcat version. Each tomcat artifact should be listed with the overridden version, before you imports spring-boot-dependencies. The list of artifacts can be found in spring-boot-dependencies.
Using a different version of the starter is wrong (you can't mix two Spring Boot versions in the same app) and will have no effect since dependency management is in control anyway. In other words, you'll get spring-boot-starter-web version 2.3.2.RELEASE indeed but all the artefacts that it brings will be managed by the dependency management (the tomcat version defined by version 2.2.6.RELEASE).
In that particular case of yours, upgrading to 2.2.9.RELEASE could also be an option as it provides dependency management for the tomcat version that you need.
Well, this has been already answered.
For you, as you are using maven, you need to override the properties set in parent Spring pom.
<properties>
......
<tomcat.version>your_version</tomcat.version>
......
<properties>
For gradle, it is simple as
ext['tomcat.version'] = '8.5.34'
in your main build.gradle
Source: How to change embedded tomcat's version in existing spring boot app?

What are the Maven coordinates for the JCache API jar?

I have an executable JAR (not a web app being deployed to a standard container like Tomcat, etc.) that needs to connect to a DB and cache a lot of stuff.
If at all possible, I'd like to use the JCache API and inject (via Guice) the Hazelcast implementation. I will code 100% against the JCache API, so that if I ever change my mind and choose another implementation, the swap out should be (relatively) painless.
One problem: where can I get the latest stable JCache JAR?!? (What are the Maven coordinates?)
On Maven Central, all I can find are some dev JARs from 2005! So I ask: where is the JCache?
The jar are available here : javax.cache/cache-api
If you want to use Ehcache implementations, copy/past the dependencies below:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.1</version>
<scope>runtime</scope>
</dependency>
Find a complete example here.

Paho MQTT vs MQTT paho spring integration

While I was fixing some issues in my client within an application using paho and spring framework, I discovered the spring MQTT integration. I'm not sure if this fix my problems, and I not even sure what is this.
Could someone explain what is the difference of using this together?
Do spring framework bring errors into paho that the integration solve?
or is save enough use this two technologies separate?
The dependencies used by me are:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
and :
<dependency>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<groupId>org.eclipse.paho</groupId>
<version>1.0.2</version>
</dependency>
The integration of the dependency below it will be quite painful. I want to know if there is any advantage on doing this.
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
Thank you all!
Spring Integration is sub framework in Spring eco system which allows to configure Enterprise Integration Patterns easily for Enterprise Application Integration.
It is good if you want to do processing of data with Source, Filter, Accumulator, Processor, Sink kind of a architecture with highly cohesive and loosely coupled components.
It is ideal to use where you have multiple systems speaking different protocols together which needs to integrated to with other systems without much fuss.

Categories