I have the following code.If my url[0] is down/had any issue i.e !200 then i need to call another url
url[1].how can i code it effeciently.I am using spring boot and java 8.
url[0] = server1.8080/get/data
url[1] = server2.8080/get/data
ResponseEntity<MyPojo> response =restTemplate().exchange(url[0],HttpMethod.GET, request, MyPojo.class)
You could use Ribbon for client side load balancing with ribbon.
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html
There are a few steps to make it work:
add the ribbon dependency to your pom.xml
configure the servers in the application.properties or
application.yaml.
Configure the Ribbon Client
Please find a complete tutorial here:
https://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon
Related
I am creating endpoint that is dependent on another endpoint,i have created interface and did all of that stuff but when i requested https://example.com to give me info it did not responded and request timeout exception came up.the quarkus is not supporting https request i also have added certificates kindly let me know what i am doing wrong or what i need to do.
quarkus.http.ssl.certificate.file=META-INF/dev.crt
quarkus.http.ssl.certificate.key-file=META-INF/dev.com.key
com.package.xyz/mp-rest/url=https://example.com
You need to specify some properties in your application.properties file:
quarkus.http.ssl-port=8443
quarkus.http.insecure-requests=enabled
quarkus.http.ssl.certificate.key-store-file=keystore.jks
quarkus.http.ssl.certificate.key-store-password=password
Documentation source I used for this was the Quarkus cookbook available from RedHat: https://developers.redhat.com/books/quarkus-cookbook see section 3.8
I am new to feign client implementation , i have following code for current implementation .
FeignPaymentAbcService:
#FeignClient(name= "abc-service", url="abc url")
public interface FeignPaymentAbcService{
//methods
}
invoking call :
(feignPaymentAbcService.someFunctionality("some input")).getBody();
In future there is possibility of multiple feign services like feignPaymentAbcService ,feignPaymentxyz Service etc so according to service it should get feign client name and url in which service is running.
Basically want to make in dynamic way.
Can anybody suggest any approach ?
You can use the Feign along with Ribbon and Eureka server for dynamically getting the url along with the server( or list of urls depending on instances)
#FeignClient(name= "abc-service")
#RibbonClient(name = "abc-service")
but the name has to be there so that the particular service is identified from the app.properties file.
You need to add corresponding dependencies for eureka server & Ribbon Load balancer and you need to configure them in application.properties
You can lookup my sample code here
I wish to use Spring Security (version 5.1.2) to generate a CSRF token for my Angular 7 application. I have the following in my SecurityConfig file:
#Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
with the following RequestMapping in my controller:
#RestController
#RequestMapping("/authentication")
public class AuthenticationController {
#GetMapping("/csrf")
public void getCsrfToken(){...}
#PostMapping("/register")
public RegisterOutputDTO register(#RequestBody UserDTO input){...}
}
I gathered from various sources that the csrfTokenRepository would automatically generate a cookie with header XSRF-token on my first GET call (which is what /authentication/csrf is for), but I am not getting a cookie back from the server. Hence on my next POST call I am getting a 403 response. What could I possibly be missing?
As indicated on the comments to my question, I found the answer to my problem. A cookie can not be sent cross-domain.
My frontend was deployed on localhost:3000 and my backend on localhost:9080, which are considered different domains apparently. If I go to localhost:9080 (I get a white page, but that doesn't matter) and I then go to the application tab in Chrome, I find that the XSRF cookie I was looking for is stored like I was expecting all along. The cookie was available from the GET call I executed from my front-end. The problem is that the cookie needs to be available for localhost:3000 so that Angular can make use of the cookie.
There are multiple ways you can solve this issue in your local environment.
Use a proxy
You can use a proxy to map certain url paths to your backend. This is the solution I went with.
I run my Angular application with a webpack dev server. Webpack provides an easy way to proxy certain urls to your backend. For example:
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 3000,
proxy: {
'/api': 'http://localhost:9080'
}
}
The Angular application runs on localhost:3000. Any calls to localhost:3000/api/*. will be forwarded to localhost:9080/api/*. So in my case I no longer perform a GET call on localhost:9080/api/authentication/csrf, but I call localhost:3000/api/authentication/csrf which will then get forwarded to my backend. (I added /api to the path in my rest controller, for those wondering.)
Deploy both applications on the same port
Using the frontend-maven-plugin you can build the frontend to its dist folder and then let maven package the dist folder along with the backend for deploy. I haven't tried this, but there are various resources that show this should be easy to do with Spring boot. So both frontend and backend would be available through localhost:9080 for example.
Use Spring Profile to disable csrf locally
You can make use of Spring #Profile annotations to create a different configuration for local environment and the rest (test, acceptance, production). Csrf can simply be disabled for development. I do not prefer this option since I like to keep DEV and other environments the same as much as possible. It's also not a real answer to the problem statement.
Special thanks to the answer of user #dspies which helped me find the problem.
I try to implement restart feature in my web application.
I've added following dependencies:
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.cloud:spring-cloud-starter:1.2.4.RELEASE")
In start logs I found that post /restart was registered.
I decided to request this url:
But result is fail. I understand that this url should be protected but I have custom authorization/authentication mechanism and I don't have rights to change it.
Is there way to disable protection for this url? A better way - to have service which I can inject inside my controller and invoke. Is there something inside the spring to solve my problem?
Reason is that spring cloud enabled the security for endpoints by default.You need disable security for management (because /restart endpoint is an additional endpoint for management), in properties:
management.security.enabled=false
to remap endpoints from ../restart to /foo/restart, you need to add additional property:
management.context-path=/foo
To implement your custom end point, you just have to implement interface Endpoint and override its methods.
to disable default restart endpoint:
endpoints.restart.enabled=false
try this:
endpoints.restart.enabled = true
management.security.enabled=false
I am working on configuring a website where the client wants us to configure URL's like example.com/newyork and example.com/england. We are using Java SpringBoot environment.
Please advise how should I approach this problem? Is this something that can be configured in spring boot/spring-mvc or is an apache configuration.
You have to use #RequestMapping("/myUrl") on your controller where myUrl should be replaced with the URL you want.
This should work.