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:org.dhara.CustomUserService.java

@Override
public User getAuthenticatedUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null && authentication.getPrincipal() instanceof User) {
        return (User) authentication.getPrincipal();
    } else {/* www.  j a  v a2s  . c  o  m*/
        throw new SecurityException("Could not get the authenticated user!");
    }
}

From source file:nl.surfnet.coin.api.basic.MockBasicAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials(),
            Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
    token.setDetails(authentication.getDetails());
    return token;
}

From source file:edu.byu.mpn.rest.BaseController.java

private JwtUserDetails getJwtUserDetails() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication auth = context.getAuthentication();
        if (auth != null) {
            return (JwtUserDetails) auth.getPrincipal();
        }//from   ww w.j a  v  a2 s .c  o  m
    }
    return null;
}

From source file:org.jasig.springframework.security.servlet.test.ServletSecurityTestController.java

protected String handle(ModelMap model) {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();

        final String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
            model.put("userDetails", principal);
        } else {/*from   w ww.j  a v a  2 s  .c o  m*/
            username = principal.toString();
        }

        model.put("username", username);
    }

    return "servletDisplayUserInfo";
}

From source file:eu.supersede.dm.rest.UserRest.java

/**
 * Return the current user.//  w w  w.jav a  2  s . co  m
 * @param authentication
 */
@RequestMapping("/current")
public User getUser(Authentication authentication) {
    DatabaseUser currentUser = (DatabaseUser) authentication.getPrincipal();
    Long userId = currentUser.getUserId();

    return DMGame.get().getUser(userId, currentUser.getTenantId(), currentUser.getToken());
}

From source file:org.jasig.springframework.security.portlet.test.PortletSecurityTestController.java

protected String handle(ModelMap model) {
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();

        final String username;
        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
            model.put("userDetails", principal);
        } else {/*from  w  ww . j a v  a2  s.co m*/
            username = principal.toString();
        }

        model.put("username", username);
    }

    return "portletDisplayUserInfo";
}

From source file:ch.wisv.areafiftylan.users.controller.CurrentUserRestController.java

@RequestMapping(value = "/teams/invites", method = RequestMethod.GET)
public List<TeamInviteResponse> getOpenInvites(Authentication auth) {
    User currentUser = (User) auth.getPrincipal();

    return teamService.findTeamInvitesByUsername(currentUser.getUsername());
}

From source file:com.climate.oada.security.oauth.CustomUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) {

    LOG.info("Going to process authentication: " + authentication);
    if (authentication != null && authentication.getPrincipal() != null
            && authentication.getCredentials() != null) {

        LOG.info("authentication principal: " + authentication.getPrincipal());
        LOG.info("authentication credentials: " + authentication.getCredentials());

        /*//from w  w w  .  j  ava  2 s  . co m
         * authentication.getPrincipal() <=> userName
         * authentication.getCredentials() <=> password
         */
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);
        return auth;
    }
    throw new BadCredentialsException("Invalid User Credentials");
}

From source file:com.github.lynxdb.server.api.http.handlers.EpSuggest.java

private ResponseEntity response(SuggestRequest _request, Authentication _authentication) {

    User user = (User) _authentication.getPrincipal();

    List<String> result;

    switch (_request.type) {
    case "metrics":
        if (_request.q == null || _request.q.isEmpty()) {
            result = suggests.byName(vhosts.byId(user.getVhost()));
        } else {/*from  w w  w . j  a  v  a2 s.co  m*/
            result = suggests.byName(vhosts.byId(user.getVhost()), _request.q);
        }
        break;
    case "tagv":
        if (_request.q == null || _request.q.isEmpty()) {
            result = suggests.byTagValue(vhosts.byId(user.getVhost()));
        } else {
            result = suggests.byTagValue(vhosts.byId(user.getVhost()), _request.q);
        }
        break;
    case "tagk":
        if (_request.q == null || _request.q.isEmpty()) {
            result = suggests.byTagKey(vhosts.byId(user.getVhost()));
        } else {
            result = suggests.byTagKey(vhosts.byId(user.getVhost()), _request.q);
        }
        break;
    default:
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, "Insupported suggest type : " + _request.type,
                null).response();
    }

    if (result.size() < _request.max) {
        return ResponseEntity.ok(result);
    } else {
        return ResponseEntity.ok(result.subList(0, _request.max));
    }
}