working in the same machine with ReactApp at port 3000, Spring boot war deployed on Tomcat9 at port 8090 but CORS blocked from another machine - spring-boot

In a single machine named mbc the React app at port 3000 running using pm2, the backend Spring boot application demo.war file deployed on Tomcat 9 at port 8099. In the same machine with firefox browser it is working as expected.
But I am facing the problem when from another machine with the firefox browser http://mbc:3000 the UI opens and the fetch call to the backend Spring boot App the sent option call
ends in the CORS blocked. The OPTIONS request returns with CORS blocked. I am not using any Security such as Spring Security. The application is simple proof of concept application using Spring boot and JPA. Kindly guide me how to sort it out. Is this indicates network issue or any other settings to be done at backend server
(1) at each controller #CrossOrigin annotation
(2) at the spring boot application file
#Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
};
(3) I am not using spring security
(4) In my windows machine at the etc file in the host file added the entry for ipaddress mbc. Here the mbc is the name of the host.

Related

Issues setting up API on Elastic Beanstalk with HTTPS requests

So, I've been trying for time to get the following working:
Hosting front-end application on S3 Bucket (with HTTPS support)
Hosting Spring Boot API with Elastic Beanstalk (with HTTPS support)
Make requests from front-end application to API (with HTTPS support)
My current setup for EBS is, the API is sitting behind a load balancer with Route 53 directing incoming requests to the load balancer, which then sends an internal HTTP request to EBS.
It was quite painful for me to get to this point (This is all pretty new to me) and after getting this set up, I'm receiving a CORS error when trying to make a request to the API from my front-end. I'm not seeing anything in the logs and my Spring Boot CORS is set up to accept all, so this leads me to believe that the Load Balancer is causing the CORS error.
I'm wondering if I'm on the right track at all? Could the load balancer be causing this issue? Can it be resolved?
Add this code in your main class below main function
#Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
.allowedHeaders("*");
}
};
}

Accessing tomcat management page from Spring tool set 4

I have developed a Camel Rout using Spring. I used the STS4 IDE to develop the same.
When I started the application using Run As-> Spring Boot App, the application starts and also I can see from the logs that the route is started.
My route is a basic app, the exposes a rest endpoint and logs a Hello World
#Override
public void configure() throws Exception {
restConfiguration()
.enableCORS(true)
.apiContextPath("/api-doc")
.apiProperty("api.title", "Test REST API")
.apiProperty("api.version", "v1")
.apiContextRouteId("doc-api")
.component("servlet")
.bindingMode(RestBindingMode.json);
rest("/api/")
.id("api-route")
.consumes("application/json")
.get("/test")
.to("direct:log");
from("direct:log")
.id("log")
.log(LoggingLevel.INFO,"Test successful");
}
What I am expecting is that if I do localhost:8080/api/test, i will see some logs "Test Susccessful" in the Tomcat logs. I am using tomcat 9, Instead what I get WhiteLable error. I thought there is an issue with my code so I tried localhost:8080 and i was expecting the Tomcat Management server to open. Even that is not working.
I am not starting Tomcat separately. What I am doing is Run As-> Spring Boot App and I guess it is calling the embedded tomcat.
Credit for this answer goes to #Bedla. I had to add camel in the URL path http://localhost:8080/camel/api/test

Connect secured Client with Spring Boot Admin Security

Can someone guide me how can I connect my secured client with the spring boot admin application?
Scenario: I have a spring boot application (client) with spring security and spring-actuator enabled. The actuator endpoints are secured and for this I have the following (working) configuration in my web security config:
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
auth.inMemoryAuthentication().withUser("Test-Actuator").password("Test-Baby").roles("ADMIN", "ACTUATOR");
}
Now I want to integrate the Spring Boot Admin Application in our environment. I set up a simple application with spring-boot-admin-server and spring-boot-admin-server-ui enabled. In the application.properties file I provide the following properties:
spring.boot.admin.username=test
spring.boot.admin.password=test
these are for the server admin, right?
spring.boot.admin.client.metadata.user.name=Test-Actuator
spring.boot.admin.client.metadata.user.password=Test-Baby
these are for the client, right?
Now when I run the applications I get the following error from the client: Failed to register application as Application [...] at spring-boot-admin ([...]) 401 null
I call the spring boot admin page and get a HttpBasic Authentication Pop Up. No credentials from above work for this Pop Up.
Note: If I disable the security in the client application.properties file it works but (like expected) the client doesn't work anymore.

Spring boot addResourceHandlers not working on linux

In my spring boot application, I am adding resource handlers via the below method. Handler is added to the location on windows setup, however are not being done so when deployed on my linux machine.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//linux
registry.addResourceHandler("/content/**").addResourceLocations("file:/app/content/").setCachePeriod(0);
//windows
//registry.addResourceHandler("/content/**").addResourceLocations("file:/D:\files\").setCachePeriod(0);
}
NOTE: The target folder on the linux machine has read write permissions as well. Also, have tried with file:// and file:///
Any other settings that I might be missing. Access to the files on http://myhost/content/file.jpg result in 404 on linux
Resolved. Issue was with the apache proxy conf.
Initially I was serving the static files via apache. Later was doing so from within the spring boot application.
However,
ProxyPassMatch ^/content !
this setting was still there the conf which was not forwarding the requests to the interceptor.

Send a message with SockJS to Spring Websocket handler over RabbitMQ

I'm developing message-broker communication between 2 applications: Grails client and Spring Boot micro service.
To make my client-side updated in long-polling manner I use WebSockets.
I've successfully configured Grails and Spring Boot to use web sockets over RabbitMQ broker.
Grails client gets all publications from Spring Boot as expected.
But I faced a problem to send message from my JS code on Grails side to Spring Boot handler on server side.
I follows all default configuration from: https://github.com/zyro23/grails-spring-websocket/blob/010ea1fb3557a63b6ce0d87a0b055f6cbc7df319/README.md
The same config I used to write on Spring Boot side:
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableStompBrokerRelay("/topic", "/queue")
.setRelayHost(brokerRelayHost)
.setSystemLogin(brokerRelayUsername)
.setSystemPasscode(brokerRelayPassword)
config.setApplicationDestinationPrefixes("/app");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp").withSockJS()
}
My client code call:
client.send("/app/hello", {}, JSON.stringify("world"));
But annotation #MessageMapping("/hello") doesn't work on my Spring boot handler methods.
Another one strange thing that when I enable Grails handlers with the same annotation they works good and receives all messages.
I've monitored RabbitMQ admin console and seems like in case with Spring Boot handlers client never send message to broker.
Did anybody find the same issue with cross-application web socket message sending?
Thanks in advance!

Resources