List of usage examples for org.springframework.security.core Authentication getAuthorities
Collection<? extends GrantedAuthority> getAuthorities();
AuthenticationManager
to indicate the authorities that the principal has been granted. From source file:com.hp.autonomy.frontend.configuration.authentication.CommunityAuthenticationProviderTest.java
@Test public void testAuthenticateReturnsCorrectUserWithDefaultRoles() { final UserRoles userRoles = mock(UserRoles.class); when(userRoles.getRoles()).thenReturn(Collections.<String>emptyList()); when(userService.getUser(anyString(), eq(true))).thenReturn(userRoles); final Authentication authentication = communityAuthenticationProviderWithDefaultRoles .authenticate(springAuthentication); //noinspection unchecked assertThat((Iterable<GrantedAuthority>) authentication.getAuthorities(), hasItem(new SimpleGrantedAuthority(DEFAULT_ROLE))); }
From source file:mx.edu.um.mateo.general.utils.SpringSecurityUtils.java
/** * Get the username of the original user before switching to another. * /* www.j ava 2s. co m*/ * @return the original login name */ public String getSwitchedUserOriginalUsername() { if (isSwitched()) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); for (GrantedAuthority auth : authentication.getAuthorities()) { if (auth instanceof SwitchUserGrantedAuthority) { return ((SwitchUserGrantedAuthority) auth).getSource().getName(); } } } return null; }
From source file:org.sharetask.controller.UserController.java
@RequestMapping(value = "/login/status", method = RequestMethod.GET) public void loginStatus(final HttpServletRequest request, final HttpServletResponse response) { int resultCode = HttpStatus.UNAUTHORIZED.value(); final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); log.info("authetication: {}", authentication); if (authentication != null && authentication.isAuthenticated() && !authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ANONYMOUS"))) { resultCode = HttpStatus.OK.value(); }//from w ww . j av a 2 s .c o m response.setStatus(resultCode); }
From source file:hotel.web.util.MyAuthenticationHandler.java
@Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { String userTargetUrl = "/user/index.jsp"; // change "memmber" to whatever you use String adminTargetUrl = "/admin/index.jsp"; // change "admin" to whatever you use and add more taretURLs if needed Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); if (roles.contains("ROLE_ADMIN")) { getRedirectStrategy().sendRedirect(request, response, adminTargetUrl); } else if (roles.contains("ROLE_USER")) { getRedirectStrategy().sendRedirect(request, response, userTargetUrl); } else {/* www . j av a 2 s . c o m*/ super.onAuthenticationSuccess(request, response, authentication); return; } }
From source file:gmc.gestaxi.controller.UserServiceImpl.java
@Override public boolean isUserAuthorizedWithRole(String roleToTest) { boolean isAuthorized = false; Authentication userAuth = SecurityContextHolder.getContext().getAuthentication(); if (userAuth != null) { Collection<GrantedAuthority> roles = (Collection<GrantedAuthority>) userAuth.getAuthorities(); for (GrantedAuthority role : roles) { if (role.getAuthority().equalsIgnoreCase(roleToTest)) { isAuthorized = true;//from w w w . ja v a 2s . c om } } } return isAuthorized; }
From source file:org.brekka.pegasus.core.services.impl.MemberServiceImpl.java
@Override public boolean hasAccess(final GrantedAuthority authority) { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); return authorities.contains(authority); }
From source file:fr.univrouen.poste.web.membre.PosteAPourvoirController.java
@RequestMapping(produces = "text/html") @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER') or hasRole('ROLE_MEMBRE')") public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, @RequestParam(value = "sortFieldName", required = false) String sortFieldName, @RequestParam(value = "sortOrder", required = false) String sortOrder, Model uiModel) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); boolean isMembre = auth.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_MEMBRE")); if (isMembre) { String emailAddress = auth.getName(); User user = User.findUsersByEmailAddress(emailAddress, null, null).getSingleResult(); List<PosteAPourvoir> posteapourvoirs = PosteAPourvoir.findPosteAPourvoirsByMembre(user); uiModel.addAttribute("posteapourvoirs", posteapourvoirs); } else if (page != null || size != null) { int sizeNo = size == null ? 10 : size.intValue(); final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo; uiModel.addAttribute("posteapourvoirs", PosteAPourvoir.findPosteAPourvoirEntries(firstResult, sizeNo, sortFieldName, sortOrder)); float nrOfPages = (float) PosteAPourvoir.countPosteAPourvoirs() / sizeNo; uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages)); } else {//w w w .j a v a 2 s . c o m uiModel.addAttribute("posteapourvoirs", PosteAPourvoir.findAllPosteAPourvoirs(sortFieldName, sortOrder)); } addDateTimeFormatPatterns(uiModel); return "posteapourvoirs/list"; }
From source file:de.iew.services.impl.UserDetailsServiceImpl.java
/** * {@inheritDoc}//from w w w . j a v a2 s. c o m * <p> * Die Implementierung wurde von Spring bernommen. * </p> * * @see org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper#isGranted(String) */ public boolean isAuthenticatedUserInRole(String role) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth == null || auth.getPrincipal() == null) { return false; } Collection<? extends GrantedAuthority> authorities = auth.getAuthorities(); if (authorities == null) { return false; } for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) { return true; } } return false; }
From source file:io.gravitee.management.security.listener.AuthenticationSuccessListener.java
@Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final Authentication authentication = event.getAuthentication(); try {/* w w w .j a v a 2s . co m*/ userService.findByName(authentication.getName()); } catch (UserNotFoundException unfe) { final NewUserEntity newUser = new NewUserEntity(); newUser.setUsername(authentication.getName()); final Set<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority) .collect(Collectors.toSet()); newUser.setRoles(roles); userService.create(newUser); } }
From source file:org.trustedanalytics.servicecatalog.service.rest.ServicesController.java
public boolean canDeleteOffering(UUID serviceGuid) { final FilterQuery filter = FilterQuery.from(Filter.SERVICE_PLAN_GUID, FilterOperator.EQ, serviceGuid); boolean isPublic = privilegedClient.getExtendedServicePlans(serviceGuid) .exists(plan -> plan.getEntity().getPublicStatus()).toBlocking().single(); boolean inAnotherOrg = privilegedClient.getExtendedServicePlanVisibility(filter) .distinct(visibility -> visibility.getEntity().getOrgGuid()).count().toBlocking().single() > 1; if (!isPublic && !inAnotherOrg) { return true; }//www . ja v a2 s .co m Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Collection<? extends GrantedAuthority> authorities = Optional.ofNullable(auth.getAuthorities()) .orElse(new ArrayList<>()); return authorities.stream().map(GrantedAuthority::getAuthority).anyMatch(ADMIN_ROLE::equalsIgnoreCase); }