How to configure embedded jetty base resource for servlet in Spring Boot - spring-boot

How do I configure embedded jetty base resource for servlet in Spring Boot?

Spring Boot documentation contains pretty good piece of information regarding embedded containers. In order to have Jetty as your Embedded container all what you need to do is just to update boot dependencies. All other configuration should pretty standard for Servlet/Spring MVC application.
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

Related

Spring Cloud microservice with keycloak

I have a spring cloud microservices project which uses Spring Cloud Config Server for managing configurations and Eureka Server for service discovery.
My application was doing great until I wanted to add a new microservice with keycloak. This new microservice is simply a rest API for my Vue frontend application and user managament is expected to be handled by Keycloak.
The new service runs OK and registers itself to Eureka until I add keycloak dependencies to the project. Application does not crash or throw any errors, startsup fine and registers itself to Eureka but on the Spring Boot Admin server panel I see that the application is down.
Here is my .properties file for the new service.
eureka.instance.preferIpAddress=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.leaseRenewalIntervalInSeconds=3
eureka.client.eureka-connection-idle-timeout-seconds=3
eureka.client.fetchRegistry=true
spring.boot.admin.client.url=http://localhost:6060
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
server.port=8082
keycloak.auth-server-url=http://localhost:8080/auth
keycloak.realm=microservices
keycloak.resource=microservices-app
keycloak.public-client=true
keycloak.security-constraints[0].authRoles[0]=user
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/*
keycloak.cors=true
Here are my dependencies for the new service.
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
<version>4.8.3.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>4.8.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring cloud version is Hoxton.SR1
Spring boot version is 2.2.2.RELEASE
I have tried adding Spring Security and making java configurations using KeycloakAuthenticationProvider but didn't help.
Throughout this project, I had many weird bugs caused by Spring Cloud version or Spring Cloud-Spring Boot Admin interaction so changing version or adding one little parameter to the configuration files usually did the trick, I am suspecting this Keycloak issue will be solved the same way.
Spring Boot Admin uses actuator endpoints, try to make them unprotected:
keycloak.security-constraints[0].authRoles[0]=user
keycloak.security-constraints[0].securityCollections[0].patterns[0]=/*
keycloak.security-constraints[0].securityCollections[1].patterns[0]=/actuator
keycloak.security-constraints[0].securityCollections[1].patterns[1]=/actuator/*

Why is SBA JMX Bean Management reporting an error?

Versions:
Spring Boot 2.1.0
Spring Boot Admin 2.1.1
I have everything working with my Spring Boot app reporting to Spring Boot Admin. There is just 1 piece not working and that is JMX Bean Management. From the docs:
To interact with JMX-beans in the admin UI you have to include Jolokia
in your application. As Jolokia is servlet based there is no support
for reactive applications. In case you are using the
spring-boot-admin-starter-client it will be pulled in for you, if not
add Jolokia to your dependencies.
I understand it does not work with Reactive WebFlux but I am using Servlet Based example using Undertow as my servlet container. Configuration in pom.xml:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
And as i stated everything is working except 1 thing and that is JMX control. I get the following error and I can't find any reason or logging as to why? Any help would be appreciated!
Start your program with following parameters:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.port=1099
And (if using docker/kubernetes) expose port 1099

Debug log4j Configuration in a spring boot webapplication

I am developing a multimodule webapp with spring boot. My webapplication always takes the log4j config from the dependend submodule, which is not desired. Is there a way to show debug info, which logback or log4j file is used?
You need to exclude default by adding these to pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

Spring boot 1.4 externalize log4j xml

Before spring boot 1.4 migration, i was able to keep the log4j.xml outside of the jar using the application.properties config [logging.config=conf/log4j.xml]. The config folder and application jar resides under same folder.
After spring boot 1.4 migration, the log4j.xml is not get picked and it throws filenotfound exception. Does spring 1.4 loads the config and log xml differently from previous version?
spring-boot is probably trying to use its default logger, logback. You need to exclude logback or spring-boot-starter-logging.
Spring documentation on how-to-logging:
Spring Boot supports Log4j 2 for logging configuration if it is on the
classpath. If you are using the starters for assembling dependencies
that means you have to exclude Logback and then include log4j 2
instead.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

How to restrict a programmer to use a dependency in the pom.xml for spring boot?

For example
Undertow(Spring boot - using Undertow container)
Jetty(Spring boot - using Jetty container)
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
I wanted to restrict a programmer to use undertow or jetty dependency in their code.

Resources