I am sorry, English is not my first language. If you do not understand, please send email to me, zhangbin.zj@gmail.com. Thanks.

Summary

when I use spring boot and spring security, the web application can not load css and js success

Actual Behavior

I use spring boot and spring security, and I put my css and js files in "/resources/static/css", "/resources/static/css",I use UserDetailsService to load user by username. Now, when I request css , it can not load success, it redirect to "index" page

code: - config: https://github.com/zhangbin/infos/blob/master/src/main/java/work/variety/infos/config/SecurityConfig.java - pom: https://github.com/zhangbin/infos/blob/master/pom.xml

Configuration

dependency


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

securityConfig


@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/css/**", "/js/**","/fonts/**","/**/favicon.ico", "/about").permitAll()
                .antMatchers("/infos/**").hasRole("USER")
                .and()
                .formLogin().passwordParameter("password").usernameParameter("phone")
                .successForwardUrl("/index")
                .loginPage("/login").failureUrl("/login-error");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }
}

Version

spring boot version:1.5.8 spring-boot-starter-security version: 1.5.8 spring security version:4.2.3

Sample

Comment From: toby1024

it`s ok,my ide's reason

Comment From: AmericanDreams

wat was the reason ? i face to same error

Comment From: sikierto

May I know the reason? I am having this error also. It is working fine. Until I restarted my IDE and get this error.

PageNotFound : No mapping for GET /css/main.css

Comment From: rwinch

It is likely because you need a line for permitAll for anything that starts with /resources/. For example:

protected void configure(HttpSecurity http) throws Exception {
    http
        // ...
        .authorizeRequests()
            .antMatchers("/resources/**").permitAll()
            // ...
            ;
}

If you continue to have problems, please ask on StackOverflow which is the best place for questions

Comment From: ezizrehimov

this worked for http://localhost:8080/clients/new and http://localhost:8080/clients and http://localhost:8080/, but when http://localhost:8080/clients/edit/13 not worked,

Comment From: hannah23280

It is likely because you need a line for permitAll for anything that starts with /resources/. For example:

java protected void configure(HttpSecurity http) throws Exception { http // ... .authorizeRequests() .antMatchers("/resources/**").permitAll() // ... ; }

If you continue to have problems, please ask on StackOverflow which is the best place for questions

I try out your solution, it does not work.. This is not suprised as the URL does not even contain "resources" as part of its path,