Spring Security antMatcher expression not works with path variable - java

I need to provide certain role for accessing a URL in the following format:
/connector/{programId}/order/{anything here}
Where programId is an integer value so I'd tried the following and it doesn't work at all.
.antMatchers('/connector/{programId:\\d+}/order/**').access("hasRole('CONNECTOR')")
But when I used ** instead of the programId part it's working well. But how can I make it work with pathVariable (which is always an integer).

You should use RegexRequestMatcher instead of AntPathRequestMatcher.
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")

I am using:
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")
But server response 405 error

Related

Path variable having special characters like # in Spring boot

I'm using Spring Boot 2.4.6. For delete APIs getting 405 method not found. My endpoint is like: beauty/v1/sites/addressTemplates/:templateId
Path variable: ##$%#
Can someone please suggest what can be done to make this behavior as not complaining for 405? Please direct me to other questions in case I'm missing something.
I guess that your issue has nothing to do with Spring. Maybe you are trying to compose the whole URL by using reserved characters.
In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. Source.
Which means that an URL which looks like:
beauty/v1/sites/addressTemplates/##$%#
is not exactly what you imagine it to be because # is interpreted in a special way. What you have to do is to percent encode the "special" path variable so it will look like this at the end:
beauty/v1/sites/addressTemplates/%23%40%24%25%23
Then Spring will not complain anymore and will resolve properly the endpoint.

Pinterest API: pass multiple values in entity_fields parameter

In using this Pinterest Analytics API:
/ads/v3/reports/async/{advertiser}/delivery_metrics/
I am successful when the entity_fields parameter is a single value like this:
https://api.pinterest.com/ads/v3/reports/async/<advertiserId>/delivery_metrics/?start_date=2021-04-04&end_date=2021-04-05&level=PIN_PROMOTION&entity_fields=AD_GROUP_ID
however I also need the CAMPAIGN_ID, but when I try to include more than one value in the entity_fields parameter I get bad request. I have tried several forms, and all the following fail:
entity_fields=CAMPAIGN_ID&entity_fields=AD_GROUP_ID
entity_fields=CAMPAIGN_ID AD_GROUP_ID
entity_fields="CAMPAIGN_ID AD_GROUP_ID"
Here is an example of the error, although the exact syntax of the error depends on how I tried to parameterize entity_fields:
{"code":1,"data":null,"endpoint_name":"ads_v3_create_advertiser_delivery_metrics_report","error":{"message":"Parameter 'entity_fields' (value CAMPAIGN_ID AD_GROUP_ID) is not one of PRODUCT_GROUP_ID, PIN_PROMOTION_ID, PIN_PROMOTION_NAME, PIN_PROMOTION_STATUS, AD_GROUP_ID, AD_GROUP_NAME, AD_GROUP_STATUS, CAMPAIGN_ID, CAMPAIGN_NAME, CAMPAIGN_STATUS, CAMPAIGN_MANAGED_STATUS. "},"message":"Invalid parameters.","message_detail":"Parameter 'entity_fields' (value CAMPAIGN_ID AD_GROUP_ID) is not one of PRODUCT_GROUP_ID, PIN_PROMOTION_ID, PIN_PROMOTION_NAME, PIN_PROMOTION_STATUS, AD_GROUP_ID, AD_GROUP_NAME, AD_GROUP_STATUS, CAMPAIGN_ID, CAMPAIGN_NAME, CAMPAIGN_STATUS, CAMPAIGN_MANAGED_STATUS. ","status":"failure"}
How does the Pinterest api server expect the entity_fields parameter to be formatted? Thanks in advance for any insight here.
Sorry to answer my own question, but I heard back from pinterest support. To pass multiple field names in the entity_fields parameter, separate by comma:
https://api.pinterest.com/ads/v3/reports/async/<advertiserId>/delivery_metrics/?start_date=2021-04-04&end_date=2021-04-05&level=PIN_PROMOTION&entity_fields=AD_GROUP_ID,CAMPAIGN_ID

Azure Custom Vision Parameter this.client.endpoint() is required and cannot be null

I'm trying to get a basic Custom Vision image classifier working by following the tutorial in the Azure documentation found here: https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/quickstarts/image-classification?pivots=programming-language-java
I have followed this tutorial word for word and have spent a couple days trying to work this out. The issue I am having is when running the code I am getting the following stack trace:
Parameter this.client.endpoint() is required and cannot be null.
java.lang.IllegalArgumentException: Parameter this.client.endpoint() is required and cannot be null.
at com.microsoft.azure.cognitiveservices.vision.customvision.training.implementation.TrainingsImpl.createProjectWithServiceResponseAsync(TrainingsImpl.java:2313)
at com.microsoft.azure.cognitiveservices.vision.customvision.training.implementation.TrainingsImpl$TrainingsCreateProjectParameters.execute(TrainingsImpl.java:2401)
at com.microsoft.azure.cognitiveservices.vision.customvision.samples.CustomVisionSamples.ImageClassification_Sample(CustomVisionSamples.java:67)
at com.microsoft.azure.cognitiveservices.vision.customvision.samples.CustomVisionSamples.runSample(CustomVisionSamples.java:46)
at com.microsoft.azure.cognitiveservices.vision.customvision.samples.CustomVisionSamples.main(CustomVisionSamples.java:374)
The endpoint is defined here and passed as a parameter
final String Endpoint = System.getenv("AZURE_CUSTOMVISION_ENDPOINT");
CustomVisionTrainingClient trainClient = CustomVisionTrainingManager.authenticate("https://{Endpoint}/customvision/v3.0/training/", CustomVisionTrainingClientKey).withEndpoint(Endpoint);
CustomVisionPredictionClient predictClient = CustomVisionPredictionManager.authenticate("https://{Endpoint}/customvision/v3.0/prediction/", predictionApiKey).withEndpoint(Endpoint);
While the tutorial doesn't explicitly state to do so I have tried to set the system environment variable AZURE_CUSTOMVISION_ENDPOINT manually with PowerShell and it doesn't work with or without this set.
Am I missing something? Any help would be greatly appreciated!
Fixed my own issue. The environment variables had not set correctly and were null pointers. I replaced the System.getenv call to the string in plain text and this is working correctly.

Thymeleaf add custom data attribute with message resource value

I have a requirement where I need to insert the value to custom data tag using thymeleaf. The code for doing it using
data-th-attr="${data-custom=#messages.msg('test')}"
as well as
th:attr="data-custom=${#messages.msg('test')}"
I am unable to get the value in both the cases.
ultimately the parsing should be like data-custom="test"
here test is key for the value test in a properties file
By using the
data-th-attr="data-custom=#{test}"
or By using
th:attr="data-custom=#{test}"
helped me out, here test is the key for the value in message resource the issue was with the intellij IDEA IDE, it was having a bug that was showing me an unnecessary error.
Use th:attr="data-custom=#{key.for.message}" , this should work.
then after parsing the Expression,
data-custom="value.for.message"

Variable Path Param for REST service testing in Jmeter

I'm testing RESt service which has path parameter.
/my-service/v1/Customer/order/{ordernumber}
I want to increment the number by 1 for each request. How to achieve this in Jmeter? Till now i had been passing a fixed path param, therefor our test result were on only one input parameter.
/my-service/v1/Customer/order/5247710017785924
The good point to start with is putting your initial order value into User Defined Variable
Given start order as "5247710017785924" you need to create an "ordernumber" variable and set it's value to 5247710017785924.
After each request you can increment variable value by adding BeanShell postprocessor to your HTTP Sampler with following code:
long ordernumber = Long.parseLong(vars.get("ordernumber"));
ordernumber++;
vars.put("ordernumber",String.valueOf(ordernumber));
And set ordernumber in your HTTP Sampler path as
/my-service/v1/Customer/order/${ordernumber}
None of the solutions worked for me. Here is what I did
Define HTTP request as shown below and add path /api/v2/state/find/${id} to the request
Right click on HTTP request --> Preprocessor -> User Parameters ->Add variable -> input id and it's value
Start HTTP request, this should work
Use JMeter Counter component to increment variable.
This question is path parameter related, where the value of the order number is incremented by 1 in each successive request. But I faced a scenario where I got a list of order numbers and I had to make request for those order numbers. So, I am gonna answer this question with respect to that, this solution can be applied in both the scenarios.
What I did is put all the parameter paths in a CSV file, like this -
/my-service/v1/Customer/order/5247710017785924
/my-service/v1/Customer/order/5247710017785976
/my-service/v1/Customer/order/5247710017785984
/my-service/v1/Customer/order/5247710017785991
Then I iterated through the list of paths in the CSHTTPle and made http request to the server. To know how to iterate through the CSV file and make http request in Jmeter, you can check this link:
https://stackoverflow.com/a/47159022/5892553
You can use a JMeter Counter:
right click on your Thread Group (under the Test Plan)
select Add–>Config Element–>Counter
set the Starting value (0), Increment (1), Maximum value, Exported Variable Name ("ordernumber")
Then you can use the exported variable name as path param:
/my-service/v1/Customer/order/${ordernumber}
I used a BeanShell PreProcessor to generate an id
vars.put("id", UUID.randomUUID().toString());
Then used the path Http Request
/api/v1/event/${id}/
BINGO!!!

Categories