报告在 authorizeHttpRequests / authorizeRequests 块中 anyRequest() 之后链接的请求匹配器注册(requestMatchers、mvcMatchers、antMatchers、regexMatchers)。
anyRequest() 必须为授权链中的最后一个匹配器。 在它之后配置另一个匹配器会导致 Spring Security 在启动时失败,并出现 Can't configure requestMatchers after anyRequest。
不正确:
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.requestMatchers("/admin/**").hasRole("ADMIN") // 报告:在 anyRequest() 之后配置
);
正确(将 anyRequest() 移至结尾):
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/home").permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().authenticated()
);