为了账号安全,请及时绑定邮箱和手机立即绑定

403 Forbidden 即使我授予访问权限

403 Forbidden 即使我授予访问权限

慕娘9325324 2023-05-24 15:16:51
我使用 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')"),问题是一样的。@Overrideprotected void configure(HttpSecurity http) throws Exception{    http        .authorizeRequests()         .antMatchers("/home")          .access("hasRole('USER')")          .antMatchers("/","/**").access("permitAll")          .anyRequest().authenticated()          .and()            .formLogin()            .and()        .httpBasic();}
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

您需要使用权限而不是角色。


@Override

protected void configure(HttpSecurity http) throws Exception {

http

    .authorizeRequests()

     .antMatchers("/home").hasAuthority("USER")

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

      .anyRequest().authenticated()

      .and()

        .formLogin()

        .and()

    .httpBasic();

}


查看完整回答
反对 回复 2023-05-24
  • 1 回答
  • 0 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信