I have a Contact Entity in my gateway(gw-app) app and I would like to create a entry every time a new user is registered in the UAA app. I have been trying to use the inter service communication described in Jhipster documentation for microservices.
- Fist problem I have here is I don't have this interface
@AuthorizedFeignClient
in the UAA app. - Second, I never got a success creation from uaa to gw-app using
@FeignClient
.
Beside having the communication/configuration issues with the feign client I have some concerns about how this will work when there is no session stablished(new user registering) and then I have another use case where I have an existing session from the user-management screen (when an admin is creating a new user)
UAA config
{
"generator-jhipster": {
"promptValues": {
"packageName": "com.uaa.auth",
"nativeLanguage": "es"
},
"jhipsterVersion": "4.13.3",
"baseName": "UAA",
"packageName": "com.agriket.auth",
"packageFolder": "com/uaa/auth",
"serverPort": "9999",
"authenticationType": "uaa",
"cacheProvider": "hazelcast",
"enableHibernateCache": true,
"websocket": false,
"databaseType": "sql",
"devDatabaseType": "mysql",
"prodDatabaseType": "mysql",
"searchEngine": "elasticsearch",
"messageBroker": false,
"serviceDiscoveryType": "eureka",
"buildTool": "gradle",
"enableSocialSignIn": false,
"enableSwaggerCodegen": false,
"jwtSecretKey": "8e4167f67e9f8d85cc35b70181a828c691374e58",
"enableTranslation": true,
"applicationType": "uaa",
"testFrameworks": [],
"jhiPrefix": "jhi",
"nativeLanguage": "es",
"languages": [
"es",
"en"
],
"clientPackageManager": "yarn",
"skipClient": true
}
}
Gateway app config
{
"generator-jhipster": {
"promptValues": {
"packageName": "com.app.gw",
"nativeLanguage": "es"
},
"jhipsterVersion": "4.13.3",
"baseName": "gwApp",
"packageName": "com.agriket.chat",
"packageFolder": "com/app/gw",
"serverPort": "9085",
"authenticationType": "uaa",
"uaaBaseName": "UAA",
"cacheProvider": "hazelcast",
"enableHibernateCache": true,
"websocket": "spring-websocket",
"databaseType": "sql",
"devDatabaseType": "mysql",
"prodDatabaseType": "mysql",
"searchEngine": "elasticsearch",
"messageBroker": false,
"serviceDiscoveryType": "eureka",
"buildTool": "gradle",
"enableSocialSignIn": false,
"enableSwaggerCodegen": false,
"clientFramework": "angularX",
"useSass": false,
"clientPackageManager": "yarn",
"applicationType": "gateway",
"testFrameworks": [],
"jhiPrefix": "jhi",
"enableTranslation": true,
"nativeLanguage": "es",
"languages": [
"es",
"en"
]
}
}
Client Code
package com.uaa.auth.service.restClient;
import com.uaa.auth.service.restClient.Contact;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
@FeignClient(name = "gwApp")
@RequestMapping("/api")
public interface ContactClient {
@PostMapping("/contacts")
Contact createContact(@RequestBody Contact contact);
@GetMapping("/contacts/{id}")
Contact getContact(@PathVariable(name = "id") Long id);
}
Contact
public class Contact {
private Long id;
private String login;
private String firstName;
private String lastName;
public Contact(User user) {
this.id = user.getId();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
}
public Long getId() {
return id;
}
public String getLogin() {
return login;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
sorry for the late answer. First about your concerns on the session thing. Session is a term most commonly used in stateful auth dealing with cookies. In our JWT world, you can think of a session is just the duration until the access token expires.
However, in your use case your gateway app should "log in as a service", or more precisely: authenticate as an internal OAuth2 client using the client credentials flow. This can be done using this config addon in your UAA:
and using
@AuthorizedFeignCleint
. If you wonder, yes, in this case, UAA does make a call to itself, what is a bit weird, but it works. (At least, this is the correct way as of OAuth).So you will have a session for this sensible write action. You were confused about how that could work if there is no authenticated user. With client credentials grant there is no user needed at all, as you secure service-to-service and not user-to-service calls.
I had a similar problem and solved it by simply copying the Client package from the microservice to the UAA server and doing some minor changes to the application-dev.yml. See link
To include Feign in your project use the starter with group
org.springframework.cloud
and artifact idspring-cloud-starter-openfeign
.I suppose Feign client is best suited for inter service communication between microservices, not between gateway and microservice. Hope this will help you.
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html