我有一个使用Spring Security的Spring MVC web应用程序。我想知道当前登录用户的用户名。我正在使用下面给出的代码片段。这是公认的方式吗?

我不喜欢在这个控制器中调用静态方法——恕我直言,这违背了Spring的全部目的。有没有一种方法来配置应用程序有当前的SecurityContext,或当前的认证,注入代替?

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView showResults(final HttpServletRequest request...) {
    final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
    ...
  }

Spring Security中有一些概念和实现,比如用于获得授权/控制访问的权限的GrantedAuthority接口。

我想要允许的操作,如createSubUsers,或deleteAccounts,我将允许一个管理员(角色ROLE_ADMIN)。

当我在网上看到教程/演示时,我感到困惑。我试图把我读到的东西联系起来,但我认为我们可以互换地对待这两者。

我看到hasRole消费授权字符串?我的理解肯定是错的。Spring Security中的这些概念是什么?

我如何存储用户的角色,与该角色的权限分开?

我还查看了org.springframework.security.core.userdetails.UserDetails接口,该接口在身份验证提供程序引用的DAO中使用,该DAO使用一个User(注意上次的GrantedAuthority):

public User(String username, 
            String password, 
            boolean enabled, 
            boolean accountNonExpired,
            boolean credentialsNonExpired, 
            boolean accountNonLocked, 
            Collection<? extends GrantedAuthority> authorities)

或者有没有别的方法来区分这两个?或者它没有支持,我们必须自己做?