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:com.devnexus.ting.security.DefaultSecurityFacade.java

@Override
public Authentication getAuthentication() {
    return SecurityContextHolder.getContext().getAuthentication();
}

From source file:com.sapito.config.SecurityUtils.java

/**
 * Check if logged user has authority: 'JEFE_DEPARTAMENTO_ROL' | 
 * 'JEFE_OPERATIVO_ROL' | 'EMPLEADO_BASE_ROL'
 * /*from  w w  w. ja v a2 s .  co m*/
 * @return 'JEFE_DEPARTAMENTO_ROL' | 'JEFE_OPERATIVO_ROL' | 
 * 'EMPLEADO_BASE_ROL' | null if none found
 */
public String getAuthority() {
    Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>) SecurityContextHolder.getContext()
            .getAuthentication().getAuthorities();

    for (GrantedAuthority authoritie : authorities) {
        switch (authoritie.getAuthority()) {
        case "JEFE_DEPARTAMENTO_ROL":
            return authoritie.getAuthority();
        case "JEFE_OPERATIVO_ROL":
            return authoritie.getAuthority();
        case "EMPLEADO_BASE_ROL":
            return authoritie.getAuthority();
        default:
            //System.out.println("SAPO Rol not found!");
        }
    }

    return null;
}

From source file:co.com.carpco.altablero.spring.web.controller.MainBoardController.java

@RequestMapping(value = { "/", "/admin/general" }, method = RequestMethod.GET)
public ModelAndView generalPage() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (!(auth instanceof AnonymousAuthenticationToken)) {

        ModelAndView model = roleUtils.createModelWithUserDetails(auth.getName());
        model.setViewName("admin/general");
        return model;
    } else {/*w w  w .ja  v  a  2  s.  c  om*/
        return new ModelAndView("redirect:/login");
    }
}

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

@SuppressWarnings("unchecked")
public static List<String> getAuthorities() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    List<String> credentials = new ArrayList<String>();
    Collection<GrantedAuthority> grantedAuthorities = (Collection<GrantedAuthority>) authentication
            .getAuthorities();/*from   w  w w. j  ava2  s  . c o m*/
    for (GrantedAuthority grantedAuthority : grantedAuthorities) {
        credentials.add(grantedAuthority.getAuthority());
    }
    return credentials;
}

From source file:org.appverse.web.framework.backend.security.xs.SecurityHelper.java

/**
 * Retrieves the authorities list corresponding to the currently authenticated principal
 * @return the authorities granted to the principal
 *///  w  w w  .  j  av a 2 s  . com
@SuppressWarnings("unchecked")
public static List<String> getAuthorities() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    List<String> credentials = new ArrayList<String>();
    Collection<GrantedAuthority> grantedAuthorities = (Collection<GrantedAuthority>) authentication
            .getAuthorities();
    for (GrantedAuthority grantedAuthority : grantedAuthorities) {
        credentials.add(grantedAuthority.getAuthority());
    }
    return credentials;
}

From source file:com.kcs.core.actions.LoginAction.java

@Override
public String success() throws Exception {
    try {//from w  w w .j  a  va2  s.c  om
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        logger.debug("auth : " + auth);
        setCurrentUser(authorizeService.authenticateUser(auth.getName(),
                convertPasswordBase64(auth.getCredentials().toString())));
        if (null != getCurrentUser()) {
            logger.debug("login success!");
            getCurrentUser().setMenuList(authorizeService.authorizeMenuUser(getCurrentUser().getEmpNo()));
            session.put(DmsConstant.SESSION.LOGIN_KEY, getCurrentUser());
            session.put(DmsConstant.SESSION.LOGIN_ID, getCurrentUser().getEmpNo());
        } else {
            logger.debug("login fail!");
            return FAIL;
        }

        //            UserData cur = new UserData();
        //            cur.setEmpNo("0000");
        //            setCurrentUser(cur);
        //            session.put(DmsConstant.SESSION.LOGIN_KEY, getCurrentUser());
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return SUCCESS;
}

From source file:net.maritimecloud.endorsement.utils.AccessControlUtil.java

public static boolean hasAccessToOrg(String orgMrn) {
    if (orgMrn == null || orgMrn.trim().isEmpty()) {
        logger.debug("The orgMrn was empty!");
        return false;
    }//from   w  w w  .  ja v a  2 s  .  com
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    // First check if the user is a SITE_ADMIN, in which case he gets access.
    /*for (GrantedAuthority authority : auth.getAuthorities()) {
    String role = authority.getAuthority();
    logger.debug("User has role: " + role);
    if ("ROLE_SITE_ADMIN".equals(role)) {
        return true;
    }
    }
    logger.debug("User not a SITE_ADMIN");*/
    // Check if the user is part of the organization
    if (auth instanceof KeycloakAuthenticationToken) {
        logger.debug("OIDC authentication in process");
        // Keycloak authentication
        KeycloakAuthenticationToken kat = (KeycloakAuthenticationToken) auth;
        KeycloakSecurityContext ksc = (KeycloakSecurityContext) kat.getCredentials();
        Map<String, Object> otherClaims = ksc.getToken().getOtherClaims();
        if (otherClaims.containsKey(AccessControlUtil.ORG_PROPERTY_NAME)
                && ((String) otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME)).toLowerCase()
                        .equals(orgMrn.toLowerCase())) {
            logger.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is in "
                    + orgMrn);
            return true;
        }
        logger.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is not in "
                + orgMrn);
        /*} else if (auth instanceof PreAuthenticatedAuthenticationToken) {
            logger.debug("Certificate authentication in process");
            // Certificate authentication
            PreAuthenticatedAuthenticationToken token = (PreAuthenticatedAuthenticationToken) auth;
            // Check that the Organization name of the accessed organization and the organization in the certificate is equal
            InetOrgPerson person = ((InetOrgPerson) token.getPrincipal());
            // The O(rganization) value in the certificate is an MRN
            String certOrgMrn = person.getO();
            if (orgMrn.equals(certOrgMrn)) {
        logger.debug("Entity with O=" + certOrgMrn + " is in " + orgMrn);
        return true;
            }
            logger.debug("Entity with O=" + certOrgMrn + " is not in " + orgMrn);*/
    } else {
        if (auth != null) {
            logger.debug("Unknown authentication method: " + auth.getClass());
        }
    }
    return false;
}

From source file:org.devgateway.toolkit.persistence.dao.AuditorAwareImpl.java

@Override
public String getCurrentAuditor() {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        return null;
    }/*from   w  w w  . ja va2 s .  c  om*/
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    final Object principal = authentication.getPrincipal();
    if (principal instanceof Person) {
        return ((Person) principal).getUsername();
    }
    return null;

}

From source file:ru.org.linux.auth.AuthUtil.java

public static Authentication getAuthentication() {
    if (isSessionAuthorized()) {
        return SecurityContextHolder.getContext().getAuthentication();
    } else {//  w  ww .j av  a  2  s .  c om
        return null;
    }
}

From source file:cz.fi.muni.pa036.airticketbooking.service.SecurityServiceImpl.java

public UserDto getCurrentlyLoggedUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username
    return userService.findByNick(name); //get user by login name
}