In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces what problems springboot will encounter when accessing static resources, which can be used for reference by interested friends. I hope you will gain a lot after reading this article.
The pit encountered in accessing static resources and its solution
At first, it was carried out with this structure, and there was a red article on the result page, and the page visited was like this.
Finally found the problem, although each adjustment path is wrong, and finally look at a variety of ways to see:
Add:
Package com.example.demo.config;import org.springframework.stereotype.Component;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @ Componentpublic class WebConfig implements WebMvcConfigurer {/ * * add a static resource file, which can be accessed directly from the outside at the address * * @ param registry * / @ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ static/**") .addResourceLocations ("classpath:/static/");}}
In this way, we can ask the place:
The problem of directly accessing static resources
Generally speaking, the front and rear ends are separated. SpringBoot mainly provides interface services, but sometimes some small projects want to complete both front and back ends of a jar, so some static resources such as pages are put into the SpringBoot. Here is a record of the static resource access method and the changes made after the introduction of shiro.
SpringBoot default static resource access configuration
The default configuration of SpringBoot allows direct URL access to static resources in the following path
Classpath:/META-INF/resources/
Classpath:/resources/
Classpath:/static/
Classpath:/public/
Order priority in the above order
Assume that the port is set to 8080 surrogate URL to access http://localhost:8080/index.html. Please note that static/ is not added in the URL path.
The test results show that index.html under META-INF- > resources has priority access.
The value of the static resource path configured by SpringBoot by default is controlled by the variable spring.resources.static-locations, and we generally don't have to modify it.
Spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources. Intercept filtering after introducing shiro or security
Suppose that shiro or security blocks all access paths (/ * *) of SpringBoot at the beginning. In this case, I want to put my pages, js, css, images and other static resources under static, so that shiro or security do not intercept these resources. How can I configure them?
Suppose your static resource directory is as follows:
At first I thought it was like this:
FilterRuleMap.put ("/ static/**", "anon")
That is, release all static resources under the static path, but find access to 404. 0.
In fact, src/main/resources/static is the directory where static resources are stored rather than the access directory of url. You should configure and filter the resources under the static directory.
You can configure this. The following is shiro's static resource filtering configuration, like security's, mainly which url paths need to be filtered.
/ / Image js file filtering configuration filterRuleMap.put ("/ css/**", "anon"); filterRuleMap.put ("/ js/**", "anon"); filterRuleMap.put ("/ img/**", "anon"); filterRuleMap.put ("/ pages/**", "anon"); / / homepage filtering configuration filterRuleMap.put ("/ index.html", "anon"); filterRuleMap.put ("/", "anon")
This allows you to access static resources and access index.html.
If you find that you need to configure a lot of files, you can put all the files in a unified directory myfiles, and filter this directory, as shown below:
FilterRuleMap.put ("/ myfiles/**", "anon"); / / homepage filtering configuration filterRuleMap.put ("/ index.html", "anon"); filterRuleMap.put ("/", "anon")
But when you visit the home page in this way, you need to add the myfiles, localhost:8080/myfiles/index.html to the URL path.
A clumsy solution is to add an extra index.html page and jump directly to the / myfiles/index.html page.
_ window.location.href = 'pages/index.html'
Thank you for reading this article carefully. I hope the article "what problems springboot will encounter when accessing static resources" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.