4

I started Spring Boot + Angular application and for now I want to deploy whole thing as a jar. So I created maven config, where angular app gets built and then is copied to /target/classes/resources

But every request to root (localhost:8080) gets blocked by security. When I disable it i can see the page, which means the whole thing is deployed correctly, but somehow spring does not allow me to see it. Here is my simple security config, I want static resources to be unprotected, while any other request requires authentication:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                .anyRequest().authenticated()
                .and().httpBasic();
    }
}

EDIT: A minimal example of my problem is here: https://gitlab.com/jnowacki/security-issue-demo

EDIT 2: I tries all the things from this post: Serving static web resources in Spring Boot & Spring Security application Do I do something wrong on a conceptual level? Is it wrong to serve static content along with Spring Boot app?

KKeff
  • 348
  • 3
  • 12
  • @dur i added a link to repo with minimal example. I need to serve index without any auth, and any other request after authentication. I know i can permit all and then secure just some path after e.g. /api but that is not the point. – KKeff Dec 04 '18 at 21:55
  • Your question is not clear. If you want do permitAll the URL `/` then you have to add that URL to your configuration. – dur Dec 08 '18 at 16:43
  • @dur when i configure it as follows: `http.authorizeRequests() .antMatchers("/").permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .anyRequest().authenticated() .and().httpBasic();` index.html is allowed, but request for css asks for credentials. – KKeff Dec 10 '18 at 09:14

6 Answers6

4

Add this additional override:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
            .antMatchers(AUTH_WHITELIST);
}

where AUTH_WHITELIST will contain the paths to be ignored. For instance:

private static final String[] SWAGGER_AUTH_WHITELIST = {
        // -- swagger ui
        "/v2/api-docs",
        "/swagger-resources",
        "/swagger-resources/**",
        "/swagger-ui.html",
        "/resources/**"
};
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • Can you be more specific? There is no mention of similar thing in the release notes of 2.1: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes – NiVeR Dec 04 '18 at 15:47
  • i added link to minimal example, i dont think its specific to 2.1, its just i have this version and it doesn't work there. – KKeff Dec 04 '18 at 21:57
1

try below.

@Override
public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()
    .antMatchers("/resources/**");
}

Refer spring-securitys-antmatcher

Alien
  • 15,141
  • 6
  • 37
  • 57
0

Use this method to allow static resources to be ignored by spring security

//this method allows static resources to be neglected by spring security
    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
            .antMatchers("/resources/**", "/static/**");
    }
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
0

According to StaticResourceLocation docs:

.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()

will permit access to CSS, JS, ICO, images and NOT to html.

To permit index.html you can use following configuration:

 http.authorizeRequests()
                .antMatchers("/index.html", "/").permitAll()
                .anyRequest().authenticated()
                .and().httpBasic();
0

// extends WebSecrityConfiguratesAdapeter

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Overide
    public void configure(WebSecurity web) throws Exception { 
         web.ignoring()
            .antMatchers(HttpMethod.OPTIONS, ALL_ESCAPE)
            .antMatchers("*.js")
            .antMatchers(")
            .antMatchers(BOWER_COMPONENTS)
            .antMatchers(I18N)
            .antMatchers(CONTENT);           
      }
}
Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
venkey
  • 66
  • 6
0

you must match real path, or example:

.pathMatchers("/assets/**", "/static/**")
            .permitAll()ere
Igor Roman
  • 221
  • 3
  • 7