@Test public void testCurrentUserHasPermission() { List<GrantedAuthority> auths = new ArrayList<>(); auths.add(new SimpleGrantedAuthority("ROLE1")); auths.add(new SimpleGrantedAuthority("ROLE2")); UserDetails userDetails = new User("admin", "password", auths); SessionUser sessionUser = new SessionUser(userDetails); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( sessionUser, null, auths); SecurityContextHolder.getContext().setAuthentication(authentication); assertTrue(SecurityUtil.currentUserHasPermission("ROLE1")); assertFalse(SecurityUtil.currentUserHasPermission("ROLE3")); }
public static boolean currentUserHasPermission(final String permission) { final SessionUser user = SecurityUtil.getSessionUser(); if (user!=null && user.getAuthorities()!=null) { final GrantedAuthority auth = new SimpleGrantedAuthority(permission); return user.getAuthorities().contains(auth); } return false; }
SecurityUtil { public static boolean currentUserHasPermission(final String permission) { final SessionUser user = SecurityUtil.getSessionUser(); if (user!=null && user.getAuthorities()!=null) { final GrantedAuthority auth = new SimpleGrantedAuthority(permission); return user.getAuthorities().contains(auth); } return false; } }
SecurityUtil { public static boolean currentUserHasPermission(final String permission) { final SessionUser user = SecurityUtil.getSessionUser(); if (user!=null && user.getAuthorities()!=null) { final GrantedAuthority auth = new SimpleGrantedAuthority(permission); return user.getAuthorities().contains(auth); } return false; }  }
SecurityUtil { public static boolean currentUserHasPermission(final String permission) { final SessionUser user = SecurityUtil.getSessionUser(); if (user!=null && user.getAuthorities()!=null) { final GrantedAuthority auth = new SimpleGrantedAuthority(permission); return user.getAuthorities().contains(auth); } return false; }  static SessionUser getSessionUser(); SessionUser getUser(); static boolean currentUserHasPermission(final String permission); static Integer userTimeZone(); static Date userNow(); Date getNow(); }
SecurityUtil { public static boolean currentUserHasPermission(final String permission) { final SessionUser user = SecurityUtil.getSessionUser(); if (user!=null && user.getAuthorities()!=null) { final GrantedAuthority auth = new SimpleGrantedAuthority(permission); return user.getAuthorities().contains(auth); } return false; }  static SessionUser getSessionUser(); SessionUser getUser(); static boolean currentUserHasPermission(final String permission); static Integer userTimeZone(); static Date userNow(); Date getNow();  }