403 Forbidden 即使我授予访问权限

我使用 spring security 创建了一个项目。


在configure(HttpSecurity http)我为用户设置访问"/home"权限时,但在我登录后,它显示:


403禁止访问



我创建了一个Entity class named User implementing UserDetails并且getAuthorities()我只是重新运行Arrays.asList(new SimpleGrantedAuthority("USER"));


对于 http 对象,我尝试使用直接.hasRole('USER')方法而不是.access("hasRole('USER')"),问题是一样的。


@Override

protected void configure(HttpSecurity http) throws Exception{

    http

        .authorizeRequests()

         .antMatchers("/home")

          .access("hasRole('USER')")

          .antMatchers("/","/**").access("permitAll")

          .anyRequest().authenticated()

          .and()

            .formLogin()

            .and()

        .httpBasic();

}


慕娘9325324
浏览 84回答 1
1回答

繁花不似锦

您需要使用权限而不是角色。@Overrideprotected void configure(HttpSecurity http) throws Exception {http    .authorizeRequests()     .antMatchers("/home").hasAuthority("USER")      .antMatchers("/","/**").access("permitAll")      .anyRequest().authenticated()      .and()        .formLogin()        .and()    .httpBasic();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java