Example usage for org.springframework.security.core.context SecurityContextHolder getContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContextHolder getContext.

Prototype

public static SecurityContext getContext() 

Source Link

Document

Obtain the current SecurityContext.

Usage

From source file:ro.allevo.fintpws.security.RolesUtils.java

public static boolean hasReportsRole() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserEntity loggedUser = (UserEntity) authentication.getPrincipal();
    List<RoleEntity> roles = (List<RoleEntity>) loggedUser.getAuthorities();
    for (int roleIndex = 0; roleIndex < roles.size(); roleIndex++) {
        if (roles.get(roleIndex).getAuthority().equals("Reports")) {
            return true;
        }//from w w  w  .  j  a  va 2  s . c o m
    }
    return false;
}

From source file:fr.gael.dhus.service.SecurityService.java

/**
 * Get currently connected User.//w w w  . j ava  2s.  c o  m
 * 
 * @return Current User.
 */
public User getCurrentUser() {
    SecurityContext context = SecurityContextHolder.getContext();

    if (context == null) {
        logger.error("No security context");
        return null;
    }

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        logger.error("No auth in security context");
        return null;
    }
    Object principal = auth.getPrincipal();
    if (principal instanceof User) {
        return (User) principal;
    }
    logger.debug("Principal class : " + principal.getClass());
    return null;
}

From source file:fr.univrouen.poste.web.ProfilChoiceController.java

@RequestMapping
public String profilChoice(@RequestParam(required = false) String profil) {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(auth.getAuthorities());

    if (profil != null) {
        logger.info(auth.getName() + " a slectionn le profil " + profil);
        if ("membre".equals(profil)) {
            authorities.remove(new GrantedAuthorityImpl("ROLE_CANDIDAT"));
        }//from   w  ww  .j  a v  a  2  s  . co  m
        if ("candidat".equals(profil)) {
            authorities.remove(new GrantedAuthorityImpl("ROLE_MEMBRE"));
        }
        auth = new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials(), authorities);
        SecurityContextHolder.getContext().setAuthentication(auth);
    }

    if (auth.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_CANDIDAT"))
            && auth.getAuthorities().contains(new GrantedAuthorityImpl("ROLE_MEMBRE"))) {
        return "profilChoice";
    } else {
        return "index";
    }
}

From source file:com.mycompany.thymeleafspringapp.controller.DealsController.java

private Users getUserDetails() {
    Users userDetails = (Users) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return userDetails;
}

From source file:com.sentinel.config.StatelessAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    LOG.trace("Method: doFilter called.");

    Authentication authentication = tokenAuthenticationService.getAuthentication((HttpServletRequest) request);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    chain.doFilter(request, response);//w  w w  .j a va 2  s  .c o  m
    // SecurityContextHolder.getContext().setAuthentication( null );

    LOG.trace("Method: doFilter finished.");
}

From source file:com.traffitruck.web.JsonController.java

@RequestMapping(value = "/load_user_details/{licensePlateNumber}/{loadId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public LoadAndUser getLoadAndUser(@PathVariable String licensePlateNumber, @PathVariable String loadId) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String username = authentication.getName();
    // verify the truck belongs to the logged-in user
    Truck truck = dao.getTruckByUserAndLicensePlate(username, licensePlateNumber);
    if (truck == null) { // the logged in user does not have a truck with this license plate number
        return null;
    }//  ww w . j  ava  2  s . c  o m
    Load load = dao.getLoad(loadId);
    LoadsUser user = dao.getUser(username);
    return new LoadAndUser(load, user);
}

From source file:com.lll.util.SpringSecurityUtils.java

/**
 * ?, ??true.//w  w w.  j ava  2 s .  c  o m
 */
public static boolean hasAnyRole(String[] roles) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Collection<GrantedAuthority> granteds = authentication.getAuthorities();
    for (String role : roles) {
        for (GrantedAuthority authority : granteds) {
            if (role.equals(authority.getAuthority())) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.packtpub.springsecurity.JBCPPets.java

@Override
public void init() {
    log.trace("Initializing Home page...");

    Object o = SecurityContextHolder.getContext().getAuthentication();

    addWindow(getHomeWindow());/*w  ww  . j a  v a2  s. com*/
    addWindow(getChangePasswordWindow());
    addWindow(getRegisterWindow());
    addWindow(getLoginWindow());

    if (o instanceof AnonymousAuthenticationToken) {
        setMainWindow(getLoginWindow());
    } else {
        setMainWindow(getHomeWindow());
    }

    log.trace("Initialied Home page");
}

From source file:eu.elixir.ebi.ega.access.config.CachingRemoteTokenService.java

@Override
@Cacheable(cacheNames = "tokens", key = "#root.methodName + #accessToken")
public OAuth2Authentication loadAuthentication(String accessToken)
        throws org.springframework.security.core.AuthenticationException, InvalidTokenException {
    log.info("loadAuthentication: " + accessToken);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    log.debug("Entering CachingRemoteTokenService auth: " + auth);
    return super.loadAuthentication(accessToken);
}

From source file:org.musicrecital.webapp.services.impl.SpringSecurityContext.java

public boolean isLoggedIn() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null && authentication.getPrincipal() != null) {
        if ("anonymousUser".equals(authentication.getName())) {
            return false;
        }/*from w w w. j  a v a2 s .  co  m*/
        return authentication.isAuthenticated();
    }
    return false;
}