Spring security 使用spring security时如何获取身份验证信息?

Spring security 使用spring security时如何获取身份验证信息?,spring-security,Spring Security,在我使用标签的页面: 安全性:授权ifanygrated=“角色\用户,角色\管理员” ... 它起作用了。 但在服务器端:我使用SecurityContextHolder.getContext().getAuthentication().isAuthenticated(),它总是正确的。当我没有登录时,系统将使用anonymousUser作为登录用户 如何避免这种情况?如果是spring security 2.x,则可以使用AuthorityUtils.userHasAuthority(字符串

在我使用标签的页面: 安全性:授权ifanygrated=“角色\用户,角色\管理员” ... 它起作用了。 但在服务器端:我使用SecurityContextHolder.getContext().getAuthentication().isAuthenticated(),它总是正确的。当我没有登录时,系统将使用anonymousUser作为登录用户


如何避免这种情况?

如果是spring security 2.x,则可以使用
AuthorityUtils.userHasAuthority(字符串权限)
对角色进行显式检查


您可以迭代SecurityContextHolder.getContext().getAuthentication().GetAuthority(),并确保只允许对所需角色进行操作。

SecurityContextHolder.getContext().getAuthentication().IsAuthenticationed()几乎总是返回true。 用这个


谢谢,我知道这种方法,我知道有一种方法。SecurityContextHolder.getContext().getAuthentication().getAuthories()
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated();