Spring Cloud Gateway: why route uri property ignored? - spring

I have only this route conf
spring.cloud.gateway.routes[0].id=x-service
spring.cloud.gateway.routes[0].uri=http://localhost:5555/x-service/v1/private/files
spring.cloud.gateway.routes[0].predicates[0]=Path=/v1/private/files
but app redirects to /v1/private/files.
How to fix this?

AFAIK spring-cloud-gateway Route builder takes into account only hostname and port passed to UriSpec.uri(String uri), so only http://localhost:5555 matters at this point.
So you need to rewrite your path, using RewritePath gateway filter, like this:
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/v1/private/files, /x-service/v1/private/files
First argument is the original path, second - a replacer.
RegEx also can be used there.
Take a look at spring-cloud-gateway reference docs for details.
Also I suggest reading this article.

Related

look for assistance with the url rewrite module in IIS

I'm trying to set up a reverse proxy for single sign on to an application. the URL to reach my app and the defined authentication method is "http://servername.domain/app/main?authsource=sso_rp"
My current config just sends me to the application login prompt. I've tested my authentication source and confirmed its working. My question is, does my URL rewrite config look like it should? there are no conditions or variables defined. i know I'm missing something simple but this isn't documented on the vendor end so seeking your help.

Handle different URL patterns in Zuul filters

I have two types of URL patterns as below.
/gateway/secure/api/user/getUser
/gateway/nonsecure/api/user/getUser
(Context root of zuul gateway application is gateway)
Using zuul filters I'm trying to implement two different logics based on secure and nonsecure URL patterns. I've written a pre-filter and seems it's not even executing that filter for above URL patterns. I could see gateway is throwing 404. When I try to access microservice without secure or nonsecure its working as expected. Below are the property changes I've done so far.
Context root of downstream microservice is api/user
zuul.prefix=/secure
zuul.routes.user.path=/api/user/**
zuul.routes.user.service-id=user
zuul.prefix=/nonsecure
zuul.routes.user.path=/api/user/**
zuul.routes.user.service-id=user
I've already tried by giving zuul.prefix and it seems prefix is setting as globally and cant apply only for specific routes. How can I achieve this? Can anyone please advice.Thanks
As you noted, the zuul.prefix property affects all mappings and can only be defined once. So, drop the zuul.prefix property and add the corresponding prefix to each zuul.routes.*.path properties:
zuul.routes.user.path=/secure/api/user/**
zuul.routes.user.service-id=user
zuul.routes.user.path=/nonsecure/api/user/**
zuul.routes.user.service-id=user
Note that, according to the documentation:
zuul.stripPrefix only applies to the prefix set in zuul.prefix. It does not have any effect on prefixes defined within a given route’s path.

REST API URL with Directory and File name as Path Variables

I have a requirement to send the folder details in the URL for POST method, some thing like this
http:///{directory}/{filename}
I am using Spring API to create the service. Using #PathVariables to two variables in the URI.
Problem : Directory can have "/" slashes in it.
Now how can I create my API, please help me.
POST http://example.com/api/files/path/to/my/file/filename
Stick a controller on /api/files. Scrape the URL starting after /files. Use that to locate the file.
P.S. This has the potential to be a Very Bad Idea. Make sure you secure the controller to only expose those parts of your filesystem you don't mind random internet strangers to be able to operate on.

REST Web API URL to update a resource property

To update the resource i have exposed following reset web api url -
http://server.com/api/v1/{companyid}/resources/{resourceid}
and request body contains the resource to be updated.
I have also exposed a seperate API to update a property of same resource. From business rule perspective this is special property and cannot be updated/retrieved along with normal resource api.
So using following url to expose separate api as below -
http://server.com/api/v1/{companyid}/resources/{resourceid}/property?propertyKey=propertyValue
this does not sound good. Is there better approach?
Answer from the comments for others
PUT api/v1/{companyid}/resources/{resourceid}/{property} with the Body containing the value of the property is one way.
PUT api/v1/{companyid}/resources/{resourceid}/{property}/{propertyvalue} is another way if you want the value entirely in the URL.
Of course, http://server.com/api/v1/{companyid}/resources/{resourceid}/property?propertyKey=propertyValue is also probably fine.
As #David-Brabant mentioned don't version your API's in the URL

Getting a route to an external laravel instance

I have two separate Laravel instances/sites running on a server and want to be able to generate a url to a named route on one from code within the other.
For example the following named route exists in the first instance:
Route::get('users/my_account', array('uses' => 'UsersController#myAccount', 'as' => 'my_account'))
In the second instance I want to generate a url to the route above. Can anyone think of a clean way to do this, without explicitly knowing the url (i.e. only knowing the name of the route 'my_account')?
Basically I want to expose the RouteCollection of one site to the other...
That's a pretty interesting question. There's no natively supported way of doing that and, from what I know, it won't ever.
You could try loading the routes file of the first application, parsing it's configurations (you will need that for reverse routing), construct a Router instance and use it, but I'm sure it won't be simple at all.
If you have a really, I mean really, good reason to use reverse routes, you can try building a small API on the first application. That API should receive the parameters necessary, those used in url($params), and return the full url (with domain and everything). Although, this will introduce some serious performance issues.
IMHO, stick to hard coded my_account, leave a comment on the first application route and/or controller explaining that it's used on another project and move on :)
I may call this a dirty trick. Supposing you have the following structure in your file system, where both paths are Laravel apps:
/path/to/apps/app1
/path/to/apps/app2
And you want to load the routes file from app1 into app2. You can do it as follow:
include "../app1/apps/routes.php";
$url = URL::route('register');
Voilà! But, although it worked for me there are some points to consider.
Include that file, will surely overwrites your current route collection for that process.
If that last is true, then you can have problem generating other URLs, maybe in your views.
The domain name will be of the current Laravel instance. It means that your URLs generated into app2 will hold the domain name of app1. I believe this is not what you want. But you can always generate non-absolute URLs with URL::route('register', null, false).

Resources