键斗篷硬编码角色

eimct9ow  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(342)

是否可以动态地实现“hasanyrole”,而不是在服务内部对其进行硬编码,以便用户可以随时更改它?我知道有一种方法可以对作用域执行类似的操作,但它不适合所需的需求。

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http
            .authorizeRequests()
            .antMatchers("/*").permitAll()
            .antMatchers("/test/roles").hasAnyRole("BYREAD", "BYEDIT")
            .anyRequest().permitAll();
}
z6psavjg

z6psavjg1#

根据您希望用户如何更新角色,您可以使用引用 Bean .

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/test/roles").access("@permission.check(authentication)")
            // ...
}

@Bean
public PermissionChecker permission() {
    // ...
}

static class PermissionChecker {

    public boolean check(Authentication authentication) {
        // ...
    }

}

在上面的例子中 @permissionBean 打电话 permission .
您可以将所需的任何逻辑添加到 check 方法。
有关更多详细信息,请参阅spring安全参考文档中的本节。

相关问题