Example usage for org.springframework.security.core Authentication getPrincipal

List of usage examples for org.springframework.security.core Authentication getPrincipal

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getPrincipal.

Prototype

Object getPrincipal();

Source Link

Document

The identity of the principal being authenticated.

Usage

From source file:jetx.ext.springsecurity.SpringSecurityTags.java

public static void principal(JetTagContext ctx, String property) throws IOException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal != null) {
            try {
                String val = PropertyUtils.getProperty(principal, property).toString();
                ctx.getWriter().print(val);
            } catch (Exception e) {
                // 
            }/*  w w w  .  j  a va 2 s  .co  m*/
        }
    }
}

From source file:org.brekka.pegasus.core.services.impl.AccessorContextImpl.java

private static AccessorContext accessorContext(final boolean useStub) {
    AccessorContext accessorContext = null;
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof PegasusPrincipalAware) {
            PegasusPrincipal pegasusPrincipal = ((PegasusPrincipalAware) principal).getPegasusPrincipal();
            MemberContext memberContext = pegasusPrincipal.getMemberContext();
            if (memberContext == null) {
                throw new PegasusException(PegasusErrorCode.PG623,
                        "No AccessorContext available for authentication: %s", authentication);
            }//from www.  j ava 2 s.  c o m
            accessorContext = memberContext.getAccessorContext();
        }
        if (principal instanceof AccessorContextAware) {
            accessorContext = ((AccessorContextAware) principal).getAccessorContext();
        }
    }
    if (accessorContext == null) {
        if (useStub) {
            accessorContext = new AccessorContextImpl();
        } else {
            throw new PegasusException(PegasusErrorCode.PG623,
                    "No AccessorContext available for the current security context '%s'",
                    authentication != null ? authentication.getClass().getName() : null);
        }
    }
    return accessorContext;
}

From source file:org.meruvian.yama.web.SessionCredentials.java

public static User getCurrentUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()) {
        return null;
    }//  w  w w  . j  a va  2  s . c  o m

    if (authentication.getPrincipal() instanceof DefaultUserDetails) {
        DefaultUserDetails user = (DefaultUserDetails) authentication.getPrincipal();
        return user.getUser();
    }

    if (authentication.getPrincipal() instanceof String) {
        String principal = (String) authentication.getPrincipal();
        if ("anonymousUser".equals(principal)) {
            return null;
        }

        User user = new User();
        user.setUsername((String) authentication.getPrincipal());
        return user;
    }

    return null;
}

From source file:nl.surfnet.coin.selfservice.util.SpringSecurity.java

/**
 * Get the  currently logged in user from the security context.
 *
 * @return String/*  w w  w  . j  av a  2 s.  c o  m*/
 * @throws SecurityException in case no principal is found.
 */
public static CoinUser getCurrentUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null) {
        return new CoinUser();
    }
    Object principal = auth.getPrincipal();
    if (principal != null && principal instanceof CoinUser) {
        return (CoinUser) principal;
    }
    return new CoinUser();
}

From source file:com.rosy.bill.security.SpringSecurityUtils.java

/**
 * ??, SpringSecurityUser?, ?null./*  www . j ava2s .  co  m*/
 */
@SuppressWarnings("unchecked")
public static <T extends User> T getCurrentUser() {
    Authentication authentication = getAuthentication();

    if (authentication == null) {
        return null;
    }

    Object principal = authentication.getPrincipal();
    if (!(principal instanceof User)) {
        return null;
    }

    return (T) principal;
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditInfoExtractor.java

private static void fillAuthInfo(Map<String, String> info, HttpServletRequest httpServletRequest) {
    Principal userPrincipal = httpServletRequest.getUserPrincipal();
    String userName = "";
    String userRole = "";
    String userGroups = "";
    if (userPrincipal != null && userPrincipal instanceof Authentication) {
        Authentication authentication = (Authentication) userPrincipal;
        Object principal = authentication.getPrincipal();
        if (principal != null && principal instanceof User) {
            User user = (User) principal;
            userName = user.getName();/*from ww  w.j  av  a2s  .  co  m*/
            userRole = user.getRole().name();
            userGroups = groupsToString(user.getGroups());
        }
    }
    info.put(AuditInfo.USER_NAME.getKey(), userName);
    info.put(AuditInfo.USER_ROLE.getKey(), userRole);
    info.put(AuditInfo.USER_GROUPS.getKey(), userGroups);
}

From source file:nl.surfnet.coin.selfservice.util.SpringSecurity.java

/**
 * @return//from   ww w  .  j a va  2s. c o  m
 */
public static boolean isFullyAuthenticated() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication != null && authentication.isAuthenticated()
            && authentication.getPrincipal() instanceof CoinUser;
}

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  w  w  .  j a  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.clz.share.sec.util.SignInUtils.java

private static Principal signinPrivate(String userId) {
    Authentication authentication = new UsernamePasswordAuthenticationToken("user", "pass",
            AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return (Principal) authentication.getPrincipal();
}

From source file:org.darwinathome.server.controller.PlayerController.java

private static Player getPlayer() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }//from   w  ww. ja va 2  s  .c  o  m
    Object principal = authentication.getPrincipal();
    if (principal instanceof UserService.PlayerHolder) {
        UserService.PlayerHolder playerHolder = (UserService.PlayerHolder) authentication.getPrincipal();
        return playerHolder.getPlayer();
    } else {
        return null;
    }
}