I am trying to develop Spring Boot web application and securing it using Spring security java configuration.
After placing my static web resources in 'src/main/resources/public' as advised here in Spring blog, I am able to get the static resources. i.e hitting https://localhost/test.html
in browser do serves the html content.
Problem
After I enabled Spring Security, hitting the static resource URL requires authentication.
My relevent Spring Security Java config looks like this:-
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.
authorizeRequests()
.antMatchers("/","/public/**", "/resources/**","/resources/public/**")
.permitAll()
.antMatchers("/google_oauth2_login").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home")
.and()
.csrf().disable()
.logout()
.logoutSuccessUrl("/")
.logoutUrl("/logout") // POST only
.and()
.requiresChannel()
.anyRequest().requiresSecure()
.and()
.addFilterAfter(oAuth2ClientContextFilter(),ExceptionTranslationFilter.class)
.addFilterAfter(googleOAuth2Filter(),OAuth2ClientContextFilter.class)
.userDetailsService(userService);
// @formatter:on
}
How should I configure antMatchers to permit static resources placed inside src/main/resources/public ?
Here is the ultimate solution, after 20+ hours of research.
Step 1. Add 'MvcConfig.java' to your project.
Step 2. Add
configure(WebSecurity web)
override to your SecurityConfig classStep 3. Place all static resources in webapp/resources/..
If you are using webjars. You need to add this in your
configure
method:http.authorizeRequests().antMatchers("/webjars/**").permitAll();
Make sure this is the first statement. For example:
You will also need to have this in order to have webjars enabled:
i had the same issue with my spring boot application, so I thought it will be nice if i will share with you guys my solution. I just simply configure the antMatchers to be suited to specific type of filles. In my case that was only js filles and js.map. Here is a code:
What is interesting. I find out that resources path like "resources/myStyle.css" in antMatcher didnt work for me at all. If you will have folder inside your resoruces folder just add it in antMatcher like "/myFolder/myFille.js"* and it should work just fine.
Ignore any request that starts with "/resources/". This is similar to configuring http@security=none when using the XML namespace configuration.
There are a couple of things to be aware of:
src/main/resources/public
will be served from the root of your application. For examplesrc/main/resources/public/hello.jpg
would be served fromhttp://localhost:8080/hello.jpg
This is why your current matcher configuration hasn't permitted access to the static resources. For
/resources/**
to work, you would have to place the resources insrc/main/resources/public/resources
and access them athttp://localhost:8080/resources/your-resource
.As you're using Spring Boot, you may want to consider using its defaults rather than adding extra configuration. Spring Boot will, by default, permit access to
/css/**
,/js/**
,/images/**
, and/**/favicon.ico
. You could, for example, have a file namedsrc/main/resources/public/images/hello.jpg
and, without adding any extra configuration, it would be accessible athttp://localhost:8080/images/hello.jpg
without having to log in. You can see this in action in the web method security sample where access is permitted to the Bootstrap CSS file without any special configuration.