Encrypted password not reading properly while bean is created in spring - spring

I am beginner in spring area & tried reaching on this but it seems something is going wrong with my configuration.
Problem statement:
one of encrypted password property is declared in application.properties & its simple as below :
server.authentication.external.password={ENCRYPT}5nYRejahndjhsyfvvjehbd==
My bean is created like below :
public Client externalClient(#Value("${server.authentication.external.username}") String username,
#Value("${server.authentication.external.password}") String password){
....
}
Issue is expected password to be read with "#Value("${server.authentication.bapi.password}"" should be "{ENCRYPT}5nYRejahndjhsyfvvjehbd==" but i am getting "5nYRejahndjhsyfvvjehbd==" The prefix didnt readed. Hence my password is working.Trying to understand why this prefix was escaped

why this prefix was escaped because in spring, value of application.properties field should be in string form, it should not contain contain curly brackets like {ENCRYPT} it represents a json object or a map not string. that's why spring is not including and parsing it as value.

Related

How to handle invalid/extra special characters & = in request url-SpringBoot?

I have a Rest service where get call if I send multiple invalid/extra & and = characters then also my endpoint does not throw any error. I would like to throw back invalid request error if url contains any extra special character like & or =.
for example:
http://localhost:8080/myservice?rollNo=03456789321&school=Myschool //This is Okay for me
http://localhost:8080/myservice?rollNo=03456789321&school= //should throw error as school is not having value
http://localhost:8080/myservice?rollNo=03456789321&&&&school=Myschool
//should throw error as &&&& is multiple where it should only one
http://localhost:8080/myservice?rollNo=03456789321&= //should throw error as &= is there at end having no sence.
Note that , I am hitting these request from postman , and I have doubt that postman do something with these parameters, cause I am not able to find these extra characters in spring boot while debugging.
Any way through which i can get whole request url in my controller so that I can find out for these charecters comming?
Any built in springboot annotation is there to handle such a cases?
I got my problem solved.
After lot of research , and some observation I came to know that when you pass any number of characters among & and = in request url, the rest client tools like postman , or advanced rest client will refine the url before hitting actual server and remove those extra un-necessary characters. SO if you write multiple &&&& or == charecters in url , it will consider each extra & as blank parameter and will ignore while sending final request, only of those characters which has parameter names besides it it will taken as part of refined request.
you can see in screenshot bellow:
You can Use #RequestParam in your Spring Boot rest Controller
Something of the following
#GetMapping(value = "/myservice")
public boolean doSomething(#RequestParam("rollNo") Integer rollNo , #RequestParam("school") String school) {
doValidation(rollNo,school);
// Do Something
return true;
}
#RequestParam will make sure that your Url need to have these Params rollNo & school. Without it it will throw error.
But if you were to pass an empty string like &school= in your second example. The controller will get an empty String.
You can add a basic validation layer right before you do anything in you controller to handle this condition.

What is String:.+ in spring request mapping's path param

While I was modifying the code written by other developer I come across an end point #RequestMapping(value = "/ICD/{icdcode:.+} and wanted to know what is :.+ in the path variable.
This has already been answered
Spring MVC #PathVariable getting truncated
Spring MVC #PathVariable with dot (.) is getting truncated
Spring - Path variable truncate after dot - annotation
Basically, it is a regular expression. Spring considers that anything behind the last dot is an extension and get rid of it.
If you have a mapping to /somepath/{email} and try /somepath/test#gmail.com the value for the path parameter email will be test#gmail
Using the regular expression {pathparam:.+} everything is considered part of the value, even what is behind the last dot.

Accessing rest of URI as PathVariable [duplicate]

I have a use case. Spring MVC REST Url receive content using the GET method code is as follows:
#RequestMapping("/q/{key}")
public String query(#PathVariable() String key, Model model){
//todo`
}
But the front end of such a request: /q/SiGeC%2FSi%E5%BC%82%E8%B4%A8%E7%BB%93. %2F decoded character /. The controller can not match mapping request.
How should I do?
You can include regular expressions in your path variable as such:
#RequestMapping("/q/{key:.*}")
This will grab EVERYTHING after the /q/. Or you can make it a more specific regex to match the pattern you are actually expecting.
Annotations of # PathVariable may not be able to solve this problem.Last use the workaround is resolved.Code is as follows:
#RequestMapping("/q/**")

What is the meaning of {id:.+} in a Spring MVC requestmapping handler method?

I came across a method in the controller. What is this id:.+ ??
#RequestMapping(value="/index/{endpoint}/{type}/{id:.+}", method=RequestMethod.POST, consumes=kContentType, produces=kProducesType)
#ResponseBody
public String indexData(#PathVariable(value="endpoint") String endpoint, #PathVariable(value="type") String type, #PathVariable(value="id") String id, #RequestBody String body, HttpServletRequest request) {
logger.debug("In web controller for endpoint " + endpoint);
return indexController.indexData(endpoint, type, id, body, getSecurityContextProvider(request));
}
The syntax of a path variable in a spring MVC controller requestmapping is {variable_name:regular_expression}. You can optionally omit the regular expression, which leads to what you see more often, {id}.
So, for the example /index/{endpoint}/{type}/{id:.+} the variable name is id and the regular expression is .+ (see below reference to spring docs).
The regular expression .+ is stating "match the metacharacter . one or more times". The '.' metacharacter represents any character including white space (though some implementations will not match newlines). See http://en.wikipedia.org/wiki/Regular_expression
The regular expression is being used to help Spring determine the value of the variable because you can have complex variable names or there might be other important information at the end of the path that would otherwise get sucked into the variable value if Spring just said "go until the end of the path" (eg. filename extensions or path variables).
It's possible that in your example, the id variable can contain special characters that would otherwise cause Spring to terminate the variable prematurely. I've run into this problem before when trying to use a filename that contained a file extension (foobar.jpg). Spring would return only the "foobar" part of the variable because Spring was assuming I wanted it to terminate the variable value at the period delimiter. So, in this case, to make sure that "id" matches the full value, you put the regex that tells Spring to go ahead and match everything between the last forward slash and the end of the path. SO Reference: Spring MVC #PathVariable getting truncated
Here's the excerpt from the Spring docs that deals with complex variable matching:
Sometimes you need more precision in defining URI template variables. Consider the URL "/spring-web/spring-web-3.0.5.jar". How do you break it down into multiple parts?
The #RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression."
Here is their (fairly complex) example:
#RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(#PathVariable String version, #PathVariable String extension) {
// ...
}
Source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
The example they provide shows how you perform complex mappings from requests to controller method paramters that you wouldn't be able to without using a regular expression.

Named Parameters rather than positional for Spring MessageSource.getMessage()

we are using Spring MessageSource to build error messages in our app.
We populate our error messages like this
dobInvalid = The DOB supplied {0} is invalid
We want to use named parameters so we can do
dobInvalid = The DOB supplied {dob} is invalid
Looking in the Api docs for getMessage it appears to suggest you can do this
http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/context/MessageSource.html
args - Array of arguments that will be filled in for params within the
message (params look like "{0}", "{1,date}", "{2,time}" within a
message), or null if none.
Obviously we can write our own but was wondering if spring can do it and if anyone can provide an example or using named parameters rather positional parameters.
Cheers
Mark
AIUI, Spring MessageSource works with JDK MessageFormat, so there is no such a named parameter. {1,date} is an example, where "date" refers to formatType, no to an arbitrary named parameter.
The general form of a parameter is:
{ ArgumentIndex , FormatType , FormatStyle }

Resources