Static resource cache issue - Spring MVC - spring

I have a spring MVC based application, where I have used spring MVC caching for static resource.
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"
cache-period="${RESOURCE_CACHE_PERIOD}"/>
The RESOURCE_CACHE_PERIOD is property in a properties file with value as 2 (for development purpose).
The static resources such as .js files remain in cache after even change and I have to publish my application again to see the changes.
What might be the issue?

I suspect you are changing the resources in your local code but not within the web container that is running your app.
Can you try altering the js from within the webapp that is actually running?

Related

LegacyCookieProcessor in standalone Tomcat and Spring Boot [duplicate]

My code is working on tomcat 8 version 8.0.33 but on 8.5.4 i get :
An invalid domain [.mydomain] was specified for this cookie.
I have found that Rfc6265CookieProcessor is introduced in tomcat 8 latest versions.
It says on official doc that this can be reverted to LegacyCookieProcessor in context.xml but i don't know how.
Please let me know how to do this.
Thanks
You can try in context.xml
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
reference:
https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html
Case 1: You are using Standalone Tomcat & have access to change files in tomcat server
Please follow answer by #linzkl
Case 2: You are using Standalone Tomcat but you don't have access to change files in tomcat server
Create a new file called context.xml under src/main/webapp/META-INF folder in your application & paste the content given below
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>
When you deploy your application in Standalone Tomcat, the context.xml file you placed under META-INF folder will override the context.xml file given in tomcat/conf/context.xml
Note: If you are following this solution, you have to do it for every single application because META-INF/context.xml is application specific
Case 3: You are using Embedded Tomcat
Create a new bean for WebServerFactoryCustomizer
#Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
#Override
void customize(TomcatServletWebServerFactory tomcatServletWebServerFactory) {
tomcatServletWebServerFactory.addContextCustomizers(new TomcatContextCustomizer() {
#Override
public void customize(Context context) {
context.setCookieProcessor(new LegacyCookieProcessor());
}
});
}
};
}
Enabling the LegacyCookieProcessor which is used in previous versions of Tomcat has solved the problem in my application. As linzkl mentioned this is explained in Apache's website https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html.
The reason is that the new version of Tomcat does not understand the . (dot) in front of the domain name of the Cookie being used.
Also, make sure to check this post when you are using Internet Explorer. Apparently, it's very likely to break.
You can find context.xml in the following path.
tomcat8/conf/context.xml
<?xml version="1.0" encoding="UTF-8”?>
<!-- The contents of this file will be loaded for each web application —>
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor"/>
</Context>
The problem is still with Tomcat9. Same process need to follow for Tomcat 9 to set the class.
Add the class in context.xml file.
If you are using eclipse to run the application, need to set in the context.xml file in the server folder. Refer the below screenshot for more reference.
Hope this helps someone.
SameSite issue in tomcat version < 8.5.47 has resolved
In Tomcat 8.5.47 and bellow (Tomcat 8 versions), setting CookieProcessor tag to enable same site (as given bellow) in context.xml does not work due to a bug in Tomcat.
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" sameSiteCookies="none" />
If you find in this situation where it is not a easy thing to upgrade tomcat immediately (which I faced recently), or if you find any other case where you just need custom processing in cookies; You can write your own CookieProcessor class to get around.
Please find a custom CookieProcessor implementation and details of it's deployment steps here.
In my case I wrote a custom CookieProcessor based on LegacyCookieProcessor source code that allows tomcat 8.5.47 to enable SameSite attribute in cookies.
As mentioned by #atul, this issue persists in Tomcat 9. It will most likely persist moving forward with all future versions of Tomcat, since this is the new standard.
Using the legacy cookie processor (by adding the line above to the context.xml file) is working well for us. However, the true 'fix' is to adjust how your cookie is formed in the first place. This will need to be done in your application, not in Tomcat.
The new cookie processor does not allow the domain to start with a . (dot). Adjusting your cookie (if possible) to start with a value other than that will fix this problem without reverting to the old, legacy cookie processor.
Also, it should be obvious, but I didn't see it mentioned above: after updating the context.xml file, you need to restart the Tomcat service for the change to take effect.
Cheers!

spring boot static resource 404 error

I am studying spring boot.
And now I have to deploy my static resources to the web project, linking it,
but it returns 404 error
Also, when I access to "localhost:8080/appie/resources/js/common.js" browser returns 404 error(my context path is /appie).
What could be that reason?
server.contextPath=/appie
Spring Boot by convention serves static resources under src/main/resources/static.
The path of the resource will be relative to this, so if you put common.js under static resources, you will be able to access to it in:
localhost:8080/appie/common.js
To access to it in as resources/js/common.js create this folder structure under static.
You should read this guide about serving static content with spring boot.
It says the following:
Spring Boot will automatically add static web resources located within any of the following directories:
/META-INF/resources/
/resources/
/static/
/public/
After placing your js file in one of this directories you can access it via $host:$port/$contextPath/common.js

Spring & static content ( css, js)

I want to create simple app, that would use css and js. But I can't reach that static content... I've found some solutions, but I wasn't able to modify them for my solution :(
Would be anybody please so kind and could show some explicit solution (where to add/modify something and what exactly to add/modify) ?
my app structure in navigator:
http://i.nahraj.to/f/gNc.jpg
Content of web.xml and servlet-context.xml http://pastebin.com/fVcNZPst
I'm running my app on tomcat server.
The home.jsp page is correct, because when I rewrite it to home.html and open it via web browser, it shows correctly. I would really appreciate any help.
There are a few issues in your structure:
a. You have put <resources mapping="/resources/**" location="/resources/" /> for mapping your resources, this would map anything with uri starting with /resources/ to locations under your webapp, in your structure that would be src/main/webapp/resources, you however don't have that folder.
b. If you want your files from webapp/css, webapp/img, webapp/js to be available either you can move them into src/main/webapp/resources folder and access them with say /resources/js/test.js uri or just put this entry into your servlet-context.xml file also - <mvc:default-servlet-handler /> - if you are interested I have provided more details here

I do not want to map all my xhtml pages by a controller

But I want to use Spring security.
I think i have to use DispatcherServlet and its configuration in web.xml
I am developing an application that is nor jsp nor jsf project, i am going to make all connection based on javascript/ajax/jquery via server communication.
Thus i do not want to map my xhtml pages to a controller.
But i have a single controller with #RequestMapping(/auth/login) i only want it to run when i request /auth/login this is not the problem, it is working excellently.
But when i use
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:META-INF/spring-servlet.xml
1
spring
/heythere/*
and call http://localhost:8080/app/myhtml.xhtml it tells me i have no mapping for this uri.
I do not want mapping, nor controller to run, only want to see the page.
But DispatcherServlet needs to map it, how can i tell DispatcherServlet not to map ordinary xhtml pages?
Option 1:
Inside your spring web mvc application context XML you should put something like:
<mvc:view-controller path="/myhtml.xhtml"/>
The downside is you'll have to do this per page.
Option 2:
Use a Resource Handler:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/static/**"/>
Your page would be visible like http://localhost:8080/app/static/myhtml.xhtml.
More info can be found in Spring's Doc.

How handle resources likes images, *.css with Spring Mvc

I've a problem with handle static resources.
It's my web-app
WebAppRoot
-resources
-css
-style.css
-imahes
-jsp
-template.jsp
-secure
-template_secure.jsp
so
myLocation=resources
How can I handle resource in a central way, for example I put location in a variable and
use this in tag
I tried with but I don't understand how have to use.
If you are using Spring 3.0.4 or later, you can use the < mvc:resources ...> element in your application configuration, and you will not need to create a Controller to handle static content for you.
It is as simple as specifying the location of your static content, and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
This uses the mvc namespace found http://static.springsource.org/schema/mvc/spring-mvc.xsd.

Resources