我需要找出为什么Java包含或等于方法在我的jhipster项目中不起作用?这在UserResource.java中
SecurityContextHolder.getContext().getAuthentication().getAuthorities().contains(AuthoritiesConstants.ADMIN_N);
为此,它返回
false
甚至SecurityContextHolder.getContext().getAuthentication().getAuthorities()= [ROLE_ADMIN]
和AuthoritiesConstants.ADMIN_N=[ROLE_ADMIN]
。方法equals
也无效。那么正确的方法是什么?我的jhipster版本是4.8.2。public final class AuthoritiesConstants {
public static final String ADMIN = "ROLE_ADMIN";
public static final String ADMIN_N = "[ROLE_ADMIN]";
}
最佳答案
您可以尝试以下两种方法:
SecurityContextHolderAwareRequestWrapper.isUserInRole(AuthoritiesConstants.ADMIN)
和:
Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
boolean hasRoleAdmin = authorities.contains(new SimpleGrantedAuthority(AuthoritiesConstants.ADMIN));