0

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 错误。

4

1 回答 1

0

你检查过 antMatchers() 的其他方法吗?我的意思是 permitAll() 和其他人。hasAnyRole() 可能有问题,因为它需要 String[ ] 。它是空的,因此可能会出现问题。

于 2018-12-30T17:47:29.800 回答