如何从 Set 中获取对象?

huangapple go评论62阅读模式
英文:

How am I getting objects from a Set?

问题

public Set<SimpleGrantedAuthority> getGrantedAuthority() {
    Set<SimpleGrantedAuthority> permissions = getPermissions().stream()
            .map(permission -> new SimpleGrantedAuthority(permission.getPermission()))
            .collect(Collectors.toSet());
    permissions.add(new SimpleGrantedAuthority("ROLE_" + this.name()));
    return permissions;
}

这个方法的目标是为给定的角色生成授权权限。让我解释一下其中的代码:

  1. getPermissions() 方法返回一个角色拥有的权限集合。

  2. stream() 方法将权限集合转化为一个流,使得可以对每个权限进行操作。

  3. .map(permission -> new SimpleGrantedAuthority(permission.getPermission())) 使用 map 操作将每个权限映射为一个 SimpleGrantedAuthority 对象,其中 permission.getPermission() 获取权限的字符串表示。

  4. .collect(Collectors.toSet()) 通过 collect 操作将映射后的 SimpleGrantedAuthority 对象收集到一个新的 Set 集合中,这个集合保存了角色的权限。

  5. permissions.add(new SimpleGrantedAuthority("ROLE_" + this.name())) 添加一个代表角色的授权,以 "ROLE_" 作为前缀,加上角色的名称。

  6. 最后,方法返回包含角色权限的 Set<SimpleGrantedAuthority>

总的来说,这个方法通过将角色的权限和代表角色的授权都添加到一个 Set 集合中,返回了一个用于 Spring Security 的权限集合。在整个过程中,使用了流操作和集合操作,将权限对象转换并组合成需要的形式。

英文:

I started learning spring security few days ago. Found the same code in two tutorials and I'm not sure how is it doing it's job. I realize that's it is just plain java but I don't seem to understand it.

public Set&lt;SimpleGrantedAuthority&gt; getGrantedAuthority(){
        Set&lt;SimpleGrantedAuthority&gt; permissions = getPermissions().stream()
                .map(permission -&gt; new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toSet());
        permissions.add(new SimpleGrantedAuthority(&quot;ROLE_&quot; + this.name()));
        return permissions;
    } 

From my understanding of the code, with stream() I'm filling the Set with SimpleGrantedAuthority objects and that is fine, but what and how does this line work permissions.add(new SimpleGrantedAuthority(&quot;ROLE_&quot; + this.name())); ?
Whole method should just return permissions for the given role but I am not sure how do i end up with just few right ones in the method return.

Method call:

 protected UserDetailsService userDetailsService() {
        UserDetails annaS = User.builder()
                .username(&quot;annasmith&quot;)
                .password(passwordEncoder.encode(&quot;password&quot;))
                .authorities(STUDENT.getGrantedAuthority())
                .build();
}

Role enum:

public enum ApplicationUserRole {
    STUDENT(Sets.newHashSet()),
    ADMIN(Sets.newHashSet(COURSE_READ,COURSE_WRITE, STUDENT_WRITE, STUDENT_READ)),
    ADMINTRAINEE(Sets.newHashSet(COURSE_READ,STUDENT_READ));

    private final Set&lt;ApplicationUserPermission&gt; permissions;

    ApplicationUserRole(Set&lt;ApplicationUserPermission&gt; permissions) {
        this.permissions = permissions;
    }

    public Set&lt;ApplicationUserPermission&gt; getPermissions() {
        return permissions;
    }

    public Set&lt;SimpleGrantedAuthority&gt; getGrantedAuthority(){
        Set&lt;SimpleGrantedAuthority&gt; permissions = getPermissions().stream()
                .map(permission -&gt; new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toSet());
        permissions.add(new SimpleGrantedAuthority(&quot;ROLE_&quot; + this.name()));
        return permissions;
    } 
}

Permission enum:

public enum ApplicationUserPermission {
    STUDENT_READ(&quot;student:read&quot;),
    STUDENT_WRITE(&quot;student:write&quot;),
    COURSE_READ(&quot;course:read&quot;),
    COURSE_WRITE(&quot;course:write&quot;);

    private final String permission;

    ApplicationUserPermission(String permission) {
        this.permission = permission;
    }

    public String getPermission() {
        return permission;
    }
}

Would appreciate if someone would explain me the line in detail, if there is anything to analyze in the first place.

答案1

得分: 1

在Spring Security中,您可以使用ROLES和/或特权填充UserDetails的authorities。您处理此信息的方式取决于您的选择,因此引用的这一行代码只是将ApplicationUserRole与相同位置的特权添加在一起。

> 对于框架来说,差异是微小的 - 它基本上以完全相同的方式处理它们。

> Spring Security框架没有提供任何关于如何使用此概念的指导,因此选择完全取决于实现。

您可以在Spring Security配置中使用这个。

例如:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // ...
    .antMatchers("/protectedbyrole").hasRole("STUDENT")
    .antMatchers("/protectedbyauthority").hasAuthority("student:read")
    // ...
}

在https://www.baeldung.com/spring-security-granted-authority-vs-role 链接中阅读更多。

英文:

In Spring Security, you can fill UserDetails authorities with ROLES and/or privilegies. The way you deal with this information is your choice, so what the quoted line is doing is just adding the ApplicationUserRole together the privilegies in the same place.

> For the framework, the difference is minimal – and it basically deals with these in exactly the same way.

> The Spring Security framework doesn't give any guidance in terms of how we should use the concept, so the choice is entirely implementation specific.

You could use that in Spring Security Configuration.

eg:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // ...
    .antMatchers(&quot;/protectedbyrole&quot;).hasRole(&quot;STUDENT&quot;)
    .antMatchers(&quot;/protectedbyauthority&quot;).hasAuthority(&quot;student:read&quot;)
    // ...
}

Read more in https://www.baeldung.com/spring-security-granted-authority-vs-role.

huangapple
  • 本文由 发表于 2020年9月6日 00:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定