How to map index.html to root context in spring boot application? - spring

I have created a folder static inside the resources folder of my Spring Boot application. Inside the static folder I have an index.html file. Then I added this to my configuration:
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
Now I can access index.html at localhost:8080/index.html, but what I want to do is to access it at localhost:8080. I have tried this already:
#Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("forward:/index.html");
}
But I get that "forward:/index.html"is not recognized by ServletDispatcher. Can someone help me with this? Thanks in advance.
EDIT
I solved the problem by deleting all my custom configuration, so the default configuration is available to my app. In my particular case, this works for me, but it wouldn't be bad to know how to achieve this with custom configurations. So this question is still open, everybody is welcome to give some input on the subject.

I think it is not necessary to specify the extension and forward. You can try this:
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("index");
}

Related

How to set default landing page in Spring Boot project

I have a Spring Boot project with webapp folder like:
webapp\
myapp\
api\
dashboard.xhtml
auth\
login.xhtml
register.xthml
When I run the sever I need to always enter the url http://localhost:8080/myapp/auth/login.xhtml to begin.
I found this very annoying and want to automatically redirect to this url when I enter just http://localhost:8080.
How can I achieve this?
You can make a new configuration inheriting the WebMvcConfigurer class.
In Spring Boot, the MVC part is measuring automatically, so you wouldn't do any more request controlling part in case you are new to it.
The WebMvcConfigurer class offers addViewControllers virtual function, so that you can override it and add your own controller inside it.
Just like:
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/")
.setViewName("forward:/helloworld.xhtml");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
For more detailed part, you can find it here.

Static resources arent loaded in thymeleaf template

I've gone ahead and tried almost any tutorial with the hopes it wouldfix this.
I'm new to spring boot.
So I have a spring boot web application setup, but css, jscript and any other static content won't be loaded in template. It's not a problem with the css or jscript as implementing them directly into the html file will make it work.
This (http://prntscr.com/lk6f6q) is how my project looks like. "test".js just includes a simple alert call.
Html: https://hastebin.com/ixejakiqev.xml
Pom: https://hastebin.com/vakinawuva.xml
What am I doing wrong? I'm trying to solve this since a week and nothing seems to work. Am I maybe missing a library?
Check the exact issue using network tab of the browser.
and also ensure that you have a class something like below to handle static resources.
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/webjars/**",
"/img/**",
"/css/**",
"/js/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/",
"classpath:/static/img/",
"classpath:/static/css/",
"classpath:/static/js/");
}
}
The security config did not allow unverified access to the static resources.

how to stop accessing css and js from url in static folder ? Spring Boot

I just added all my css and js files in static folder and it could be easily accessed from url, how can I fix it? I added this method to my security configuration
#Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
but it doesn't work
Assuming for an example that you don't want to expose /static/that/ folder.
Rather than Blacklist approach I suggest selectively define a Whitelist of your resources like below (where you don't define say /static/that/)
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/this/,classpath:/public/ # Locations of static resources.
Excert of code above, taken from Spring Boot Documentation

spring - disable all caching for static content

how can I disable caching of the static content?
I tried to put this in my applications.properties:
spring.cache.type=NONE
This is my config:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true).addResolver(
new VersionResourceResolver().addContentVersionStrategy("/**"));
}
Still when I change something in the css file I have to reload the page with the developer console opened in order for it to show.
Thanks!
Move all the resources out of classpath. To replace something loaded to classpath you may need something complex like own class loader etc. Try to move to a separate folder all the resources you need to change.

Resources(js/css/images) are not working after spring security

1) I have configured application using pure java configuration. Everything seems to be working correctly except css/js/images. The application is available on Github at
https://github.com/rajendersaini/cabms/
Just want to point out the configuration
AppConfig - spring mvc config - register resources using
registry.addResourceHandler("/resources/**")
.addResourceLocations("/WEB-INF/resources/").setCachePeriod(31556926);
and in SecurityConfig -
http.authorizeRequests()
.antMatchers(LoginController.AUTHLOGIN,
ResourceConfig.RESOURCE_PATH_MATCHER).permitAll()
.anyRequest().authenticated().and().formLogin()
.loginPage(LoginController.AUTHLOGIN);
Can you please help fixing this issue?
Where do your static resources end up in your war file? I ask because you have them under the WEB-INF directory. I don't know if Spring will allow you to serve public content out of WEB-INF.
In my build, I have my static stuff under
src/main/webapp/resources/...
/css
/js
/img
This way, they end up at the top level of the war file under the /resources directory. You could give that a shot.
1) Finally my application resources are working - few changes I have made, In my mvc app config, I don't know when I introduced this line and this was the culprit. I will investigate it later
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
2) Java webapp initializer was looking for mapping which should be "/*" instead of "/"
3) In headers and footer we need to have context appended to resource paths.
You don't specify a secure rule which will ignore an application security for static resources. Try to add code like this in your Spring Security configuration class:
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}

Resources