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

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/**");
}

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.

Spring MVC + Thymeleaf: not loading some resources

I am new to Thymeleaf and Spring MVC.
I have been dealing with the following problem: some resources (css or images) don't get loaded by my webpage while other do. They are in the same path and folder, the syntax is the same (i have checked by just switching the name of the resource and it worked).
For example, my Thymeleaf Template can find and read my own css files, but it won't read the bootstrap-4 one.
Here is my project structure:
And here an example of the code trying to read bootstrap.css:
The same problem happens with images of the same format.
Any ideas of what could be causing the issue?
Thank you in advance
You can register a resource handler by extending a WebMvcConfigurerAdapter.
Something like this.
#EnableWebMvc
#Configuration
public class SpringWebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
Anyway, the css files should be in the css directory
Okay so I understood what the problem was: intelliJ.
It didn't see the new files added to the project.
Running mvn clean install fixed the problem

Spring MVC Cannot configure views' path

I have a project that uses Spring Boot 2.2.5 (Spring version 5) - here is a link to a bare minimum project demonstrating my problem. In all the tutorials I followed they claim views' path can either be configured inside application.properties like this:
spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp
or inside WebMvcConfigurationSupport derived class like this:
#Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.jsp("classpath:/", ".jsp");
}
or like this:
#Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
None of which work in my case. Spring will always serve .jsp files from src/main/webapp and from nowhere else in spite of my configuration or lack of it. No other file types will be served from that directory, not even HTML.
Some tutorials claim that when not configured Spring will serve anywhere from
src/main/resources/static
src/main/resources/public
src/main/resources/resources
src/main/java/META-INF/resources
I am yet to see this.
CSS and Javascript files will be served from src/main/resources but only if I have this in my MVC configuration:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/");
}
Configuring this from application.properties doesn't work
spring.resources.static-locations=classpath:/
In relation to this are there other special folder names like classpath: that can be used? I tried webapp: but it doesn't seem to be expanded
UPDATE: I thought for a moment that maybe subclassing WebMvcConfigurationSupport is to blame since it acts like #EnableWebMvc. Subclassing WebMvcConfigurer brought the following error. Placing #EnableWebMvc solves it.
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:369)
The following method did not exist:
'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.requestMappingHandlerAdapter(org.springframework.web.accept.ContentNegotiationManager, org.springframework.format.support.FormattingConversionService, org.springframework.validation.Validator)'
I read somewhere that JSP view are not supported inside embedded servlets. What a nice feature! Anyway I don't thing this is related to my problem.
I would like to stick to JSP and avoid Thymeleaf as my project is based on React. I will create MVC pages in order to be Search engine friendly, though but I will figure this out along the way.
Here is a screenshot of my project's layout

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

Resources