WebSecurityConfig 类如下:-
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/get*").hasAnyRole();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("user1Pass")
.authorities("USER");
//.and().withUser("admin").password("adminPass")
//.authorities("ADMIN");
}
/*@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("test").password("test123").roles("USER").and().
withUser("test1").password("test123").roles("ADMIN");
}
}*/
}
下面是 REST 控制器:-
@RequestMapping(value="/getprofessors", method=RequestMethod.GET)
public List<Professor> getProfessors() {
//return service.findProfessors();
List<Professor> Professors = professorRepo.findAll();
return Professors;
}
下图代表我正在拨打的 POSTMAN 电话:-
即使没有提供角色规范,访问此控制器时也会出现 403 错误。