curly brace in spring boot default property value - java

In my application.yml for my spring boot application I have this property:
apiKey: ${SECRET_ADYEN_API_KEY:djhksy{f7dsadasb}
The problem is that the API key contains a forward curly brace, which messes up the parsing of the key in the app. It becomes ${SECRET_ADYEN_API_KEY:djhksy{f7dsadasb}, not simply djhksy{f7dsadasb which is the actual API key. How can I get around this? I tried escaping the { character with \ and \\ without luck.

Could you please try separating the default value (djhksy{f7dsadasb) to a different property just like below, It is working for me :
default-secret-key: djhksy{f7dsadasb
apiKey: ${SECRET_ADYEN_API_KEY:${default-secret-key}}

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.

Spring SpEL not compiling even when spring.expression.compiler.mode is set

I have a SpEL expression that I am trying to use for an FtpMessageHandler (specifically for outputHandler.setRemoteDirectoryExpressionString("headers['" + ftpOutPath + "']");).
Through debugging I have found that this SpEL expression is not getting compiled. When compilation time comes, SpelCompilerMode is set to OFF. So as a result the expression is not being compiled and just returning null (and my FTP client is trying to just write to the root directory instead of the one I am specifying).
Looking at the Spring SpEL documentation (https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/core.html#expressions) , it says that in order to enable SpEL compilation you need to set the property spring.expression.compiler.mode. I have done this to no effect - I have set spring.expression.compiler.mode to immediate in my application.properties file, yet SpelCompilerMode is still OFF.
Why is this happening? How can I fix this?
I know someone will ask for a code sample but honestly I have no idea what I can provide as this is all stuff internal to Spring. But here are some samples of related code:
The FtpMessageHandler:
#Value("${my.ftp.outPath}")
private String ftpOutPath;
#Bean
#ServiceActivator(inputChannel = "myChannel")
public MessageHandler getOutputHandle(){
FtpMessageHandler outputHandler = new FtpMessageHandler(mySessionFactory());
outputHandler.setRemoteDirectoryExpressionString("headers['" + ftpOutPath + "']");
return outputHandler;
}
application.yaml
spring:
expression:
compiler:
mode: immediate
my:
ftp:
outPath: myPath
I still am not sure why the expression was not compiling / intrepreting properly, however changing the code to use LiteralExpression worked instead of using the String.
outputHandler.setRemoteDirectoryExpression(new LiteralExpression(ftpOutPath));

Spring 4 Path Parameter ending in a slash

I'm trying to get my Spring 4 application to allow Path Parameters that end with an escaped slash.
The #RequestMapping I've got is:
#RequestMapping("/{externalSystemId}/{externalRequestId}/events")
And the URL that I'm calling with is /dummy/ab%2F/events. So "externalRequestId" is the one that has the trailing encoded slash.
I've gotten to the point where the handler is called correctly, and where escaped slashes in the middle of the path segment work - e.g. /dummy/ab%2Fcd/events, but not at the end.
What I've done so far:
Added system properties to Tomcat:
-Dorg.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true
-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
Custom extension of UrlPathHelper that replaces getServletPath with one that simply returns request.getRequestURI()
Configure the UrlPathHelper with:
urlDecode = false
removeSemicolonContent = false
Use this UrlPathHelper from mvc:path-matching, within mvc:annotation-driven
And I'm at a complete loss as to where to go next. I've even been debugging through both Spring and Tomcat to try and work it out, and I'm stumped. It looks like it's because UrlPathHelper.getPathWithinServletMapping() is calling request.getPathInfo(), and that is returning the path with the %2F stripped out. And that in turn is because somewhere else in Tomcat - I've lost the window now - it has decoded the %2F into a /, then replaced the "//" that is now present with a "/" instead.
This feels like it should be relatively straightforward, so what am I missing?
I've tried it on both Tomcat 7 and Tomcat 8, and with various versions of Spring 4 - currently on 4.3.14.
Problem solved.
The SlashFriendlyUrlPathHelper that I had that was the extension of UrlPathHelper was causing problems, because my webapp is not the Root webapp. Remove that, but keep every other change in place, and it starts to work properly.

Spring Security antMatcher expression not works with path variable

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

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"

Categories