How to serve static files from file system with Quarkus - quarkus

I am running a Quarkus application server (Serving REST via HTTP) behind an ngnix web server. Now I want to serve static content (Flutter web-app) from file system from with the same base url. Letting ngnix serve it would do it but I would like to use the access control configured in Quarkus application.properties for web app part, too.
Shouldn't it be possible to let my Quarkus server also serve the static stuff?
As server and app are in separate repos and maintained separately, both parts should be deployable independently.
I would expect this to be a common issue but can't find a simple solution.
Any hint?

You can leverage Quarkus Reactive Routes with something like:
#ApplicationScoped
public class StaticAuthenticatedDeclarativeRoute {
#Route(path = "/auth-static/*", methods = Route.HttpMethod.GET)
#Authenticated
void secureStatic(RoutingContext rc) {
StaticHandler.create(FileSystemAccess.RELATIVE, "content/").handle(rc);
}
}
and consider the path can also be absolute using FileSystemAccess.ROOT.

I have the same problem (but not the requirement to serve from filesystem).
What you can do according to the Quarkus documentation - HTTP Reference:
Put your static web content into META-INF/resources directory of your application.
Then build your quarkus application and deploy it.

Related

Thymeleaf cache and security in Spring Boot

In my web app (Spring Boot + Spring Security + Thymeleaf) I disabled caching for security purposes:
spring.thymeleaf.cache=false
I can't remember where did I get this information from, or if it's even true.
I have a lot of images on my website and I'd like to cache them. What would you recommend?
Take a look at Spring Security – Cache Control Headers, it explains caching for ResponseEntity object, which can used to return an image in the form of a byte array.
This also shows a way of caching images specifically, although the post itself is from 2015, some parts can still be relevant.
Actually the spring.thymeleaf.cache property has nothing to do with security, but more with performance. If you disable the Thymeleaf cache, the templates will be automatically reloaded when they need to be parsed, it has to do with hot swapping your server-side templates.
This is helpful during development because you can instantly see changes to your templates. If not you would have to restart your application.
See the documentation on the Developer tools on what it's used for.
Spring as of version 4.x somewhere has several ways to implement static resource caching with versioning (cache busting mechanism). Assuming you are serving your images as static resources through Spring, you might want to look into these.
If the images themselves do not need to be secured, serving them up as static resources with caching applied like that should be enough.
Spring Security will automatically "cache bust" all requests, which is by design.
However for images that don't really need to be managed by Spring Security, you can disable it for specific resource directories, in your WebSecurityConfigurerAdapter (in this case the images directory would be in your ../resources/static directory).
#Override
public void configure(WebSecurity webSecurity) {
webSecurity.ignoring().antMatchers(
"/images/**"
)
}
And the Spring Boot way is to add
spring.resources.cache-period=your value here in seconds
You would only want to turn off the Thymeleaf cache in development, when you want to do a hot-reload of a template.
Test it works with
curl -X GET -I https://your-site/images/foo.png
This will cache them at the browser level. If you needed to cache them at a server level, you could use a reverse proxy like nginx.

How to serve static data from outside war file in spring mvc maven application with tomcat

Hi I had created a maven web application with spring mvc framework, I had created war file also deploy in amazon web server because of lots of static files like bootstrap css and js file and other, the war file is heavy and its taking too much time to load first page. the war file is bloated, how to separate out static files, such as bootstrap from your war? how do I resolve this problem?
because of lots of static files like bootstrap css and js file and other, the war file is heavy and its taking too much time to load first page
I'm not sure that heavy war file is a reason why a page is loading slow. There are tons of reasons that can cause such behavior. I'd suggest to look closer on that first.
how to separate out static files, such as bootstrap from your war?
Spring MVC can be configured to serve static assets from a file system:
Here is an example:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/files/**")
.addResourceLocations("file:/opt/files/");
}
More examples and explanation can be found here: http://www.baeldung.com/spring-mvc-static-resources
how do I resolve this problem?
I'm using public CDN servers (https://maxcdn.bootstrapcdn.com, https://cdnjs.cloudflare.com, https://cdn.rawgit.com, https://yandex.st) for the static resources like Bootstrap, jQuery, etc.

Cannot call the REST with usual relative urls of new spring boot project

I have two projects
1. Project-UI (containing angular pages)
2. Project-REST (Rest web services)
Initially they used to be on same tomcat so I could call any of the REST resource with url "/Project-REST/Customer/id/1234" since the context path was same e.g. localhost:8080
Now, the Project-REST has been converted to Spring boot and uses inbuilt tomcat of the boot on a port say 8085. so my relative urls dont work.
is there any way to make it work so that I need not change all my urls..... without entering context path ?
I am using eclipse as IDE
Here are 3 suggestions in random order:
All requests go to tomcat (Project-UI, port 8080), where you configure URL rewriting to redirect some requests to spring-boot (Project-REST port 8085). A quick google search produced this and this, both similar to mod_rewrite of Apache.
If as I understand &Project-UI* one contains static files, you can easily serve these from your spring-boot application at port 8085. Spring boot docs for serving static content here. In summary spring-boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.
Put a proxy server in front of both Project-UI and Project-REST and configure request redirects there. This is a more involved solution but may help you if you more projects in the future.

How to share Thymeleaf templates across domain

I'm developing some Spring applications using Spring Boot 1.4.1 and spring-boot-starter-thymeleaf dependency. I wish to share my Thymeleaf templates (such as header and footer) on my webserver to be able to include them in my applications. The easiest way to achieve this would be to include server root relative link to my templates in each of my apps. This way, my apps will be portable and I can get it to work seamlessly on all my environments (dev, test, prod).
From my perspective, this seems not to be a big deal since the server-relative URLs are already taken into account by Thymeleaf
<th:block th:replace="#{~/my/share/header}" />
However I can't get it to work. I always get an error like
Error resolving template "/my/share/header", template might not exist or might not be accessible by any of the configured Template Resolvers
I can confirm that my template is available because http://localhost/my/share/header.html is responding correctly (btw: I'm using Apache httpd to host my apps so I'm using the same port).
I've also tried to add ".html" at the end of the URL or implement the URLTemplateResolver (see below) but unfortunately, nothing seems to work.
#Bean
public ITemplateResolver templateResolver() {
UrlTemplateResolver templateResolver = new UrlTemplateResolver();
templateResolver.setSuffix(".html");
return templateResolver;
}
Does someone knows how to embed templates using server root relative URLs?
It sounds like there's no way to achieve this in the current version of Thymeleaf as you can see in this GitHub post. My workaround is to use the UrlTemplateResolver as described in the initial post and add a variable in my Spring Boot project (application.properties). This way, I can manage different URL depending of my deployment environments.

Serve static content from folder outside of project

I'm currently developing a Java backend together with JHipster 3 and ran into a problem I don't seem to be able to solve very easily.
I would like to serve static assets – in this case images – from a folder outside of the project in addition to the default front-end generated by JHipster. As of default JHipster seems to serve static assets from one directory out of two depending on environment, as configured in main/java/config/WebConfigurer.java. I would like to point /public/** to a folder in my home catalogue but keep the /** mapping for the Angular front-end.
In general Spring projects you seem to be able to add other sources for static assets by extending WebMvcConfigurerAdapter and override the addResourceHandlers method, but that doesn't seem to have an effect in my case. Adding the #EnableWebMvc annotation breaks the default JHipster mapping for their front-end. If I don't add the annotation I don't even seem to reach handleRequest() in DefaultServletHttpRequestHandler which handles the mapping to the correct servlet.
I can't give any other information on the subject at the moment, but I'm hoping someone with knowledge on JHipster will see this and point me in the right direction.
Thanks in advance, Max.
All app servers have an option to provide additional locations for class path.
For example, Tomcat's property is 'common.loader' in conf/catalina.properties.
You can then use e.g. Spring's ClassPathResource to load a resource manually, or just use a construct like '#Value("classpath:abc.txt") Resource r' to inject something known in advance.

Resources