Spring LDAP implementation failover capability - java

I am using LDAP with spring mvc 3. I want to configure it in a way where if my primary LDAP URL fails to connect then it connects to secondary LDAP URL. Can it be managed in spring configuration?

Set the urls property (pass it an array of ldap server urls) instead of the url property on your LDAPContextSource bean in your spring config. This will allow it to fail over if it can't talk to the first server.

Related

Spring Boot: use database config from WildFly's standalone.xml

I'm currently developing an REST app whith Spring boot. For development, I hardcode my database configuration in application.properties. However, this app is going to be deployed on different WildFly servers, each of them defining their DB config (user credentials) in standalone.xml.
As I'm a newbe to Spring/Java, here is my question: How can I use the DB config from standalone for my spring app?
I already did some research. What I got is to define Services with an #PersistenceContext annotated entity manager. But how can I use it without defining new services, just by using plain JpaRepositories?
In Wildfly you will have a datasource configured with at JNDI name.
Instead of configuring url, username and password you have to configure this JNDI name:
spring.datasource.jndi-name=java:jboss/datasources/myGreatDS
That's all.

Tomcat prefix to every Spring url with Spring Security

Currently I'm facing issue with redirecting application which is behind proxy server:
To see application I need to go to:
http://myserver.net/PREFIX/configuration
And I need to add PREFIX to every url which will be returned by spring. Currently after successful logging with spring security when I return
.successForwardUrl("/configuration")
It's redirecting me to url address which is not existing:
http://myserver.net/configuration
Is there any possibility to add Tomcat prefix to dispatcher servlet? To make somehow default path for the application:
http://myserver.net/PREFIX/
I was trying to use:
server.servlet.contextPath=PREFIX
But problem is that when I use context path, I need to go
http://myserver.net/PREFIX/PREFIX/configuration
To be redirected properly to spring controller. Maybe there is possibility to set up context path but only for response?

Accessing User defined service in spring boot

I am working on spring boot application which uses REST webservices. Till now , all the database paaswords i used to store in my application.properties class and am using HICKARI CP also to manage the connection pooling.
But now , i have created the user services in Cloud foundry and have included the script of CF in my build.gradle file too.
But i do not have any idea as to how shall i access these services within my code and how can i autowire thise datasources. Please advice any solution.
Use Spring cloud service connector. This is sub project of Springs cloud.
http://cloud.spring.io/spring-cloud-connectors/spring-cloud-spring-service-connector.html#_relational_database_db2_mysql_oracle_postgresql_sql_server
you can create a mysql service inside your cloud foundry space. And the bind the same to your app. This can be done as defining services inside your manifest.yml or using bind-service command in cf cli.
You can add spring cloud service connector dependencies in your gradle build and add datasource configuration bean.
#Bean
public DataSource dataSource() {
return connectionFactory().dataSource("your-mysql-service-name");
}
This creates a datasource of the mysql service in your application, and registers as a bean in spring context. That can be autowired where ever needed.
Alternatively, cloud foundry exposes the details about every service that application is binded to, in VCAP_SERVICES env variable. Once your application is binded to service, you can find your mysql service username, password, connection url, db name etc in this variable. You can read this variable and parse the values and build a datasource of your own.
Using service connectors are preferred approach as the infrastructure code is handled by springs.

Hit external IPs from microservices using Spring Cloud

I'm trying to hit external services from one of my microservices. I'm using Spring Cloud, Eureka for the registry and Spring boot as main framework.
Map<String, String> urlVariables = new HashMap<>();
urlVariables.put("ip_address", IP);
urlVariables.put("port", PORT);
ResponseObject state =
restTemplate.getForObject("http://{ip_address}:{port}/state/", ResponseObject.class, urlVariables);
From what I see, Spring Cloud injects Ribbon as the HTTP client for the Rest Template, and when I try to hit this IP (e.g: 193.172.x.x) it produces the following error:
java.lang.IllegalStateException: No instances available for 193.172.x.x
at org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory.createRequest(RibbonClientHttpRequestFactory.java:64)
at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:540)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:247)
It looks like Ribbon is trying to find a microservice instance with that name instead of looking outside. Is there any way to configure Ribbon to look for external IPs, or is it only for internal use?
You are injecting a #LoadBalanced version of RestTemplate. You have to ensure that your RestTemplate is a plain vanilla one. You can just create it with new RestTemplate(). If it's a bean just add a qualifier to ensure you're injecting a proper version of RestTemplate.
On thing you could try is to use the service id in your code and configure the real instances:
ResponseObject state =
restTemplate.getForObject("http://myExternalService/state/", ResponseObject.class, urlVariables);
Than you configure a static list of endpoints for your service
myExternalService.ribbon.listOfServers=http://ip:port
This way you do not use service discovery for this service.
http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka

Spring Eureka server does not find context-path in client url

When a client application is registered into Spring Eureka server the client id appears at dashboard, but the link url only contains the hostname and port without the context-path of client.
If I create the Spring Boot client application without setting a context-path, I mean default root context, Eureka server can access all actuators available in there.
Is there any way to inform Eureka server about it? I tried to set health and info properties, but it did not work.
If your Eureka client is setup via Spring's #EnableEurekaClient, then the client will default the health check and status check to /health and /info respectively. (These paths may be the default values beyond the Spring #EnableEurekaClient setup, but I am unfamiliar with those at this point in time).
You can override these defaults by setting the following properties:
eureka.instance.statusPageUrlPath
eureka.instance.healthCheckUrlPath
The Spring Cloud Documentation contains this information, plus much more.
application.yml
server:
servlet:
context-path: /your-path

Categories

Resources