Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角色和权限如何转换为Spring安全性?_Spring_Security_Roles_Rights_Authority - Fatal编程技术网

角色和权限如何转换为Spring安全性?

角色和权限如何转换为Spring安全性?,spring,security,roles,rights,authority,Spring,Security,Roles,Rights,Authority,在我的应用程序中,我通常创建三个用于访问管理的表。角色、权限和在角色和权限之间映射的关联表 我正在尝试将这种方法转化为Spring安全性,在阅读[本文][1]之后,我认为我走上了正确的道路。我创建了一个自定义AuthenticationProvider并实现了authenticate()方法,如下所示: @Override public Authentication authenticate(Authentication authentication) throws Authentication

在我的应用程序中,我通常创建三个用于访问管理的表。角色、权限和在角色和权限之间映射的关联表

我正在尝试将这种方法转化为Spring安全性,在阅读[本文][1]之后,我认为我走上了正确的道路。我创建了一个自定义AuthenticationProvider并实现了
authenticate()
方法,如下所示:

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UserProfile profile = userProfileService.findByEmail(authentication.getPrincipal().toString());

    if(profile == null){
        throw new UsernameNotFoundException(String.format("Invalid credentials", authentication.getPrincipal()));
    }

    String suppliedPasswordHash = DigestUtils.shaHex(authentication.getCredentials().toString());

    if(!profile.getPasswordHash().equals(suppliedPasswordHash)){
        throw new BadCredentialsException("Invalid credentials");
    }

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(profile, null, profile.getAuthorities());

    return token;
}
profile.getAuthorities()
方法创建一个权限列表(权限包装在我自己的GrantedAuthority实现中)。因此,UsernamePasswordAuthenticationToken对象是使用此列表创建的。这是UserProfile.GetGrantedAuthories()方法,用于处理以下问题:

public Collection<? extends GrantedAuthority> getAuthorities() {
    Set<ProduxAuthority> authorities = new HashSet<ProduxAuthority>();
    for (Role role : roles) {
        for (Right right : role.getRights()) {
            ProduxAuthority produxAuthority = new ProduxAuthority(right.getName());
            authorities.add(produxAuthority);
        }
    }
    return authorities;
}

公共收藏我将再次回答我自己的问题:

Spring不知道hasPermission方法使用的权限和权限仅适用于Spring Security中相对复杂的域对象安全性/ACL。Spring的简单安全性只知道角色和角色,或者更通用的“权限”(在一般意义上,不要与域对象安全/ACL中的权限混淆),比如身份验证,匿名地在身份验证对象的第三个构造函数参数中交给Spring

我总结了我自己网站上的所有内容,并创建了一个示例实现,它将权限填充到角色中,这样仍然可以做到相当灵活