1

这是这个问题的后续问题

我将我的 Vaadin 20 应用程序迁移到 21 以使用基于视图的访问控制。注释@PermitAll@AnonymousAllowed工作正常。但是,当我尝试将路由限制为特定用户角色时,@RolesAllowed我无法访问此站点(使用具有此角色的用户登录)。是否需要一些特殊代码才能让 Vaadin 识别我经过身份验证的用户的角色?

角色限制页面:

@Component
@Route(value = "admin", layout = MainLayout.class, absolute = true)
@RolesAllowed("admin")
@UIScope
public class AdminView ...

安全配置

@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends VaadinWebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        setLoginView(http, LoginView.class, "/login");
    }
    
    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
        web.ignoring().antMatchers("/images/**");
    }
}
4

2 回答 2

1

您传入的角色@RolesAllowed区分大小写,并且应该与您在 Spring Security 中的角色相匹配。最有可能在您的情况下,您想使用@RolesAllowed({"ROLE_ADMIN"}). 您可以在此处的文档中阅读更多信息https://vaadin.com/docs/v21/flow/integrations/spring/view-based-access-control/#annotating-the-view-classes

于 2021-10-07T07:31:09.703 回答
0

经过大量调试,我发现了问题,我的实现中的getAuthorities()FunctionUserDetails.java是不正确的。具有一个角色的工作虚拟版本如下所示:

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of( new SimpleGrantedAuthority("ROLE_" + "admin"));
    }

重要的是"ROLE_"在实际角色名称之前添加。然后我可以@RolesAllowed("admin")在视图类中使用。

于 2021-10-07T12:18:37.880 回答