我试图仅允许具有角色“ ADMIN”的用户访问以下端点:
../ adminconfig / *(URL中带有“ adminconfig”的用户)
这是我的配置:
@SpringBootApplication
@EnableWebSecurity
@RestController
public class BootApplication extends WebSecurityConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("{noop}1").roles("USER").and()
.withUser("admin").password("{noop}1").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests()
.anyRequest().authenticated()
.antMatchers("/adminconfig/**").hasRole("ADMIN")
.and()
.formLogin().and()
.httpBasic();
}
}
但是我仍然可以与任何其他用户(例如,具有“ USER”角色)一起访问它们。我在做什么错了?
答案 0 :(得分:0)
找到了解决方案。我不得不将antMatchers()..移动到.anyRequest()。authenticated()之上:
.antMatchers("/adminconfig/**").hasRole("ADMIN")
.anyRequest().authenticated()