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:app.config.CustomSecurity.java

public boolean hasRole(String expectedRoleValue) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Object[] grr = auth.getAuthorities().toArray();
    String userRoleValue = grr[grr.length - 1].toString();

    Role currentUserRole = Role.getRoleByLabel(userRoleValue);
    Role expectedRole = Role.getRoleByLabel(expectedRoleValue);

    return currentUserRole.ordinal() >= expectedRole.ordinal();
}

From source file:com.ge.predix.acs.zone.resolver.SpringSecurityZoneResolver.java

public static ZoneEntity getZoneEntity() {
    AcsRequestContext acsRequestContext = AcsRequestContextHolder.getAcsRequestContext();
    ZoneEntity result = (ZoneEntity) acsRequestContext.get(ACSRequestContextAttribute.ZONE_ENTITY);

    // This could happen if a zone was removed from ACS but a registration still exists in ZAC
    if (null == result) {
        ZoneOAuth2Authentication zoneAuth = (ZoneOAuth2Authentication) SecurityContextHolder.getContext()
                .getAuthentication();// w  ww.  j  a v  a  2s.co m
        throw new InvalidACSRequestException("The zone '" + zoneAuth.getZoneId() + "' does not exist.");
    }

    return result;
}

From source file:by.bsuir.finance.controllers.UserInfoController.java

@RequestMapping(value = "/username", method = RequestMethod.GET)
public @ResponseBody String getUserName() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username
    return name;//w w w.  j ava  2s .c  o  m
}

From source file:bookpub.security.SecurityUtils.java

public static User getUser() {
    return isAuthenticated() ? (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()
            : null;
}

From source file:net.kamhon.ieagle.aop.DefaultAppDetailsBaseAdvice.java

@Override
public Object getUserId() {
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null
            && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return user.getUserId();
    }/*from ww w  .jav  a  2 s. c  o m*/
    return SYSTEM_USER_ID;
}

From source file:hu.unideb.studentSupportInterface.backing.UserInfo.java

@PostConstruct
public void initBean() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    user = (User) userDao.loadUserByUsername(auth.getName());

    roleMap = new HashMap<Role, String>();

    roleMap.put(Role.TUTOR, "Oktat");
    roleMap.put(Role.ADMIN, "Admin");
    roleMap.put(Role.ASSESSOR, "rtkel");
    roleMap.put(Role.UPLOADER, "Feltlt");

}

From source file:com.mycompany.CRMFly.Security.AccountController.java

public String submitChangePasswordPage() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username = principal.toString();

    if (principal instanceof UserDetails) {
        username = ((UserDetails) principal).getUsername();
    }/*from  w w  w  .j a va  2  s .  co  m*/

    changePasswordDao.changePassword(username, newPassword);
    SecurityContextHolder.clearContext();

    FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, " ? .", null));

    return null;
}

From source file:eu.europeana.core.util.web.ControllerUtil.java

public static User getUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }/*  w  ww  .  ja  v  a  2 s.  com*/
    Object principal = authentication.getPrincipal();
    if (principal instanceof SpringUserService.UserHolder) {
        SpringUserService.UserHolder userHolder = (SpringUserService.UserHolder) authentication.getPrincipal();
        return userHolder.getUser();
    } else {
        return null;
    }
}

From source file:com.mothsoft.alexis.security.CurrentUserUtil.java

public static UserAuthenticationDetails getCurrentUser() {
    final SecurityContext ctx = SecurityContextHolder.getContext();
    final Authentication authentication = ctx.getAuthentication();

    try {/*  w ww. jav  a 2 s.c  o m*/
        return authentication != null && authentication.isAuthenticated()
                ? (UserAuthenticationDetails) authentication.getPrincipal()
                : null;
    } catch (ClassCastException e) {
        throw new AuthenticationServiceException(e.getLocalizedMessage(), e);
    }
}

From source file:com.francetelecom.clara.cloud.TestHelper.java

public static void loginAsUser() {
    SecurityContextHolder.getContext().setAuthentication(new AliceAuthentication());
}