当前位置: 首页 > 知识库问答 >
问题:

@预授权安全角色批注

司马奇希
2023-03-14

我正在试图理解一些Spring Security代码。我也是Spring Security的新手,我想我在这里遗漏了一些基本的东西。

@Controller
@RequestMapping("/download-resource")
@PreAuthorize(value="hasRole('LINKS_ADMIN')")
public class DownloadResourcesController extends BaseHtmlController 
 {..}

谢谢,雷。

共有1个答案

董子平
2023-03-14

这些角色是用户登录时分配给UserDetails的角色(权限)。这些将由身份验证实现返回。

它们是窗体集合<?扩展GrantedAuthority>,通常使用SimpleGrantedAuthority

例如,在我的应用程序中,每个人都被分配到组中。因此,当用户登录时,我会检查该用户所属的所有组,并将其添加到用户详细信息中。

    for (Group group : groups) {
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + group.getName().toUpperCase()));
    }

在引擎盖下,它是从

SecurityContextHolder.getContext().getAuthentication().getAuthorities();

其中getAuthentication()返回我在上面链接到的身份验证实例,并从该对象获取权限。

 类似资料:
  • spring安全中有一些概念和实现,例如接口,用于获取授权/控制访问的权限。 我希望允许的操作,如createSubUsers或deleteAccounts,我允许管理员(使用角色)执行这些操作。 当我在网上看到教程/演示时,我感到很困惑。我试着把我读到的东西联系起来,但我认为我们可以互换地对待这两者。 我看到使用字符串?我绝对是在理解上做错了。这些在spring安全概念上是什么? 如何将用户的角

  • Using a delegation key The collaborator can now push to the repository using Docker Content Trust. Docker will automatically choose and pick the right key for the targets/release role. Edit the file o

  • Importing a delegation certificate As a repository owner: Add the delegation key to the repository using the targets/releases path, as this is what Docker searches for when signing an image (first tar

  • Generating a delegation key Make sure you’re using OpenSSL 1.0.2+, otherwise the keys will be signed with SHA1, which Notary will refuse to import (invalid SHA1 signature algorithm): ❯ brew install op

  • Delegation roles Delegation roles allows assigning signing privileges to other repository collaborators. This is particularly interesting in organizations that have multiple people working on the same

  • 我正在为网关后面的服务开发一种方法,以便于通信。 该计划使用oauth令牌中的作用域,该令牌将由运行在同一系统上的服务使用。 例如。如果oauth令牌具有作用域“acct”和“user”,bellow api方法将可用。如果ouath令牌的作用域为'admin',但缺少作用域'acct'和'user'中的一个或两个,则在不更改注释的情况下,我希望它也可用。 我希望这个'admin'范围被全球接受,