Spring Boot 2: Refresh properties on the fly not working - spring-boot

I have followed this official tutorial Getting Started Centralized Configuration using spring boot 2.0.0.RELEASE and spring cloud Finchley.M8
But refreshing properties on the fly (Without restart) is not working.
After Some debugging, I noticed that in method refresh() from ContextRefresher.class, it returns the changed keys correctly, but after reconstructing the bean annotated with #RefreshScope in the next use. It still sees the old value not the updated one.
Note: This was working perfectly with spring boot v 1.5.6 and spring cloud Edgware.RELEASE.
Any help please?
Thanks

It seems spring.cloud.config.uri in spring boot 2.0.1.RELEASE always looking for port 8888 and not accepting other values, so I put the below configuration (you can ignore it, as it is the default value for the client, and the server should run on port 8888)
spring:
cloud:
config:
uri: http://localhost:8888
I also tried to expose all other services in the client for testing as follows
management:
endpoints:
web:
exposure:
include: '*'
or use the following to allow only refresh
management:
endpoints:
web:
exposure:
include: refresh
Then called POST method not GET for refreshing
$ curl -X POST localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
Finally, it works.

Instead of Method "POST" , use "OPTIONS" method to call the "actuator/refresh" for spring boot 2.0 or higher.
For lower versions (<2.0), use the endpoint "context/refresh"
Make sure , you have management.endpoints.web.exposure.include=* defined in application.properties.

Use below in application.properties-
management.endpoint.refresh.enabled=true
management.endpoint.restart.enabled=true
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
management.endpoint.info.enabled=true
management.endpoints.web.exposure.include=info,health,refresh
Using yaml configuration file it was not working for me and when switched to properties file, it worked with above configuration.
Thanks

Related

spring boot actuator endpoints unavailable after (2.5) uprade

I have attempted to take advice offered for similar problems; to no avail. Can someone please explain a resolution for this problem, as if I were six?
I've upgraded my version of spring boot to 2.5. All tests in my suite still pass, except for this #Springboot test:
#Test
void readActuatorInfo_success() throws Exception {
mockMvc.perform(
get("/actuator/info")
.header("Authorization", "Basic dXNlcjpwYXNzd29yZA=="))
.andExpect(status().isOk());
}
It fails because the response is 404. Now, I know that springboot does not, by default, expose the endpoints, that they must be explicitly configured. For example, the following yaml should do it, but no. Any tips?
application:
endpoint:
info:
enabled: true
To be clear, this test passed for Springboot 2.3
From 2.5 onwards the Info endpoint is no longer exposed by default,
The /info actuator endpoint is no longer exposed over the web by default. Additionally, if Spring Security is on the classpath and your application doesn’t have a custom security configuration, the endpoint requires authenticated access by default.
Refer to the documentation on exposing and securing actuator endpoints to change these new defaults.
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5-Release-Notes#secure-info-endpoint
management:
endpoint:
web:
exposure: "health,info"

How to test application yml resource file settings that uses Spring Boot?

For example, I have settings like code below which Spring Boot Actuator uses.
management:
server:
port: 60001
security:
enabled: false
And I found out that if I will write this setting with mistakes. For example (code below):
management:
server:
port: 60001
security:
enabled: fase # wrong typed a `false` word
Spring Boot will not do anything, and the application will have build successfully.
Spring boot will validate the data as long as #ConfigurationProperties exist in core. The problem is that your #ConfigurationProperties may not exist before or already deprecated in your spring boot version.
Below is the comparison of management.server class with #ConfigurationProperties.
Try to change the port with string and spring boot with validate the data.
Look also at ManagementContextAutoConfiguration.class if your using spring boot 2 you will see that it loads the class ManagementServerProperties and WebEndpointProperties
If you still needed to validate your yaml you can refer to SnakeYAML.

change spring boot 2.0 context-path

I want to change context path for spring boot 2 for example i want to serve on http://localhost:8080/test/
i mean it not working for me with spring-boot-starter-webflux:2.0.0.RELEASE
it only working with spring-boot-starter-web::2.0.0.RELEASE
I have tried
server.servlet.context-path=/test
But nothing happened to me still serve on url http://localhost:8080/
If you use the servlet API then the property is now called
server.servlet.context-path=/myapp
As confirmed by Andy Wilkinson #andy-wilkinson of the Spring Boot team via Gitter
There’s no concept of context path in WebFlux so there’s no equivalent property
i.e WebFlux doesn't support context path configuration
In spring boot 2.3.x you can set spring.webflux.base-path property
spring.webflux.base-path=/path
In reference to Spring Boot 2.x. below configuration apply for application.yml
server:
port: 8080
servlet:
context-path: /test
For application.properties configuration are
server.port=8080
server.servlet.context-path= /test
For use cases where WebFlux application is behind load balancer/proxy you can use dedicated class - ForwardedHeaderTransformer that will extract path context from X-Forwarded-Prefix and will add it to ServerHttpRequest.
Doing so you won't need to modify global context-path (which does not make sense in WebFlux)
More about it here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-web-handler-api

spring boot actuator giving forbidden error when run on a different port other than application port

=
Getting forbidden error while accessing actuator running on different port other than application port using custom spring security configuration.
application.properties file
management.security.enabled=false
management.context-path=/manage
management.port=8085
server.context-path=/lnhcverifyhcp
server.port=8090
spring boot version - 1.3.3.RELEASE
Getting forbidden error on accessing actuator url's
I had the same issue, my mcAfee using that port. Actually 403 is from McAfee.
hit this url and see what you get - http://localhost:8081/
I'm assuming that you're using Spring Security with Spring Actuator. So, the configuration to escape the Actuator out the Security are:
endpoints:
health:
sensitive: false
management:
security:
enabled: false
Hope this help!
You can disable security on the management port by adding this to your application.properties file:
management.security.enabled=false
Or, set your own username and password:
See https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html

How do I configure the spring boot quickstart for keycloak?

I am trying to set up a basic example spring boot site which uses keycloak for security. I have done the following
cloned and ran (gradlew bootRun) the 'complete' example from https://spring.io/guides/gs/serving-web-content/ (https://github.com/spring-guides/gs-serving-web-content.git) to verify that it works
added the following to the project's gradle dependencies:
compile("org.keycloak:keycloak-spring-boot-adapter:2.2.1.Final")
compile("org.keycloak:keycloak-tomcat8-adapter:2.2.1.Final")
put the following in config/application.yml:
spring:
profiles: default
server.port: 8090
keycloak:
securityConstraints:
- securityCollections:
- name: application section
authRoles:
- user
patterns:
- /
realm: stl
realmKey: MIIBIjANBgkqh[etc...]
auth-server-url: http://localhost:8280/auth
ssl-required: none
resource: example-ui
credentials:
secret: a117[etc...]
With these steps, I believe I've followed all the directions in https://keycloak.gitbooks.io/securing-client-applications-guide/content/v/2.2/topics/oidc/java/spring-boot-adapter.html. But now when I attempt to browse to the application, I get an error. This is what's logged to the console: No login page was defined for FORM authentication in context []
What am I missing to complete configuration of this app? From prior experience with earlier versions of other keycloak adapters, I would expect to have to specify an auth method of KEYCLOAK somewhere, but I don't know where that would go in spring boot, if indeed it goes anywhere.
Same problem here but it's only when I upgraded from Spring boot 1.4.0 to 1.4.1.
They have passed version of tomcat to 8.5.4 to 8.5.5. And every request except GET are working, only GET don't work with the message "No login page was defined for FORM authentication in context []" like you said.
Downgrading the version of tomcat did the trick for me :
<tomcat.version>8.5.4</tomcat.version>
Can someone know what's going on ?

Resources