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:be.bittich.quote.service.impl.SecurityServiceImpl.java

@Override
public User getUserFromSecurityContext() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    checkNotNull(auth, "Erreur: Utilisateur non connect");
    return userService.findOneByUsername(auth.getPrincipal().toString());
}

From source file:eu.supersede.fe.rest.GadgetRest.java

@RequestMapping(value = "/panel", method = RequestMethod.GET)
public Long getNumPanels(Authentication auth) {
    Long p = null;/*w w  w.ja  va  2  s.  c  o  m*/

    DatabaseUser user = (DatabaseUser) auth.getPrincipal();
    Long userId = user.getUserId();

    UserDashboard ud = userDashboards.findOne(userId);

    if (ud == null) {
        p = USER_DASHBOARD_PANELS_DEFAULT;
    } else {
        p = ud.getPanels();
    }

    return p;
}

From source file:org.callistasoftware.netcare.web.mobile.controller.BankIdController.java

@RequestMapping(value = "/complete", method = RequestMethod.GET, produces = "application/json")
@ResponseBody/* www .jav  a2  s .  com*/
public UserDetails collect(final HttpServletResponse response) throws IOException {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        return (UserDetails) auth.getPrincipal();
    }

    log.debug("We have no authentication.. Returning unathorized.");

    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    return null;
}

From source file:org.keycloak.adapters.springsecurity.authentication.DirectAccessGrantAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = resolveUsername(authentication.getPrincipal());
    String password = (String) authentication.getCredentials();
    RefreshableKeycloakSecurityContext context;
    KeycloakAuthenticationToken token;//from   w  w  w. j ava2 s  . c  o  m
    Collection<? extends GrantedAuthority> authorities;

    try {
        context = directAccessGrantService.login(username, password);
        authorities = KeycloakSpringAdapterUtils.createGrantedAuthorities(context, grantedAuthoritiesMapper);
        token = new KeycloakAuthenticationToken(
                KeycloakSpringAdapterUtils.createAccount(keycloakDeployment, context), authorities);
    } catch (VerificationException e) {
        throw new BadCredentialsException("Unable to validate token", e);
    } catch (Exception e) {
        throw new AuthenticationServiceException("Error authenticating with Keycloak server", e);
    }

    return token;
}

From source file:org.ngrinder.security.UserSwitchPermissionVoter.java

@Override
public int vote(Authentication authentication, FilterInvocation filter,
        Collection<ConfigAttribute> attributes) {
    if ("anonymousUser".equals(authentication.getPrincipal())) {
        return ACCESS_DENIED;
    }//from  w  w w. java 2  s.  c  o m
    if (!(authentication.getPrincipal() instanceof SecuredUser)) {
        return ACCESS_DENIED;
    }
    SecuredUser secureUser = cast(authentication.getPrincipal());
    User user = secureUser.getUser();
    if (user.getRole() == Role.ADMIN) {
        return ACCESS_GRANTED;
    }

    String realm = StringUtils.split(filter.getHttpRequest().getPathInfo(), '/')[0];
    if (secureUser.getUsername().equals(realm)) {
        return ACCESS_GRANTED;
    } else {
        List<User> owners = user.getOwners();
        for (User each : owners) {
            if (realm.equals(each.getUserId())) {
                return ACCESS_GRANTED;
            }
        }
    }
    return ACCESS_DENIED;
}

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

/**
 * Get the tickets of the currently logged in user. All the tickets owned by this user will be returned.
 *
 * @param auth The current User, injected by spring
 *
 * @return The current owned tickets, if any exist
 *///from   www .j  a v a  2 s. co m
@RequestMapping(value = "/tickets", method = RequestMethod.GET)
public Collection<Ticket> getAllTickets(Authentication auth) {
    UserDetails currentUser = (UserDetails) auth.getPrincipal();
    return ticketService.findValidTicketsByOwnerUsername(currentUser.getUsername());
}

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

/**
 * Get the current open order include expired orders. A User van only have one open order.
 *
 * @param auth The current User, injected by Spring
 *
 * @return The current open order, if any exist
 *//* w w w.ja v  a 2s.  c o m*/
@RequestMapping(value = "/orders/open", method = RequestMethod.GET)
public List<Order> getOpenOrder(Authentication auth) {
    UserDetails currentUser = (UserDetails) auth.getPrincipal();
    return orderService.getOpenOrders(currentUser.getUsername());
}

From source file:org.juiser.spring.security.user.SecurityContextUser.java

@Override
protected User findUser() {
    Authentication authc = getValidAuthentication();
    Assert.notNull(authc, "Current SecurityContext Authentication cannot be null.");
    Object value = authc.getPrincipal();
    Assert.isInstanceOf(ForwardedUserDetails.class, value,
            "securityContext.getAuthentication().getPrincipal() must contain a ForwardedUserDetails instance.");
    User user = null;//from   ww w  . j a v a  2 s .  c  o m
    if (value instanceof ForwardedUserDetails) {
        ForwardedUserDetails details = (ForwardedUserDetails) value;
        user = details.getUser();
    }
    if (user != null) {
        return user;
    }
    String msg = "Unable to acquire required " + User.class.getName()
            + " instance from the current SecurityContext Authentication.";
    throw new IllegalStateException(msg);
}

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

/**
 * Get all the Teams the current user is a member of.
 *
 * @param auth Current Authentication object, automatically taken from the SecurityContext
 *
 * @return A Collection of Teams of which the current User is a member
 *///from ww  w  .j av a 2  s.com
@JsonView(View.Team.class)
@RequestMapping(value = "/teams", method = RequestMethod.GET)
public Collection<Team> getCurrentTeams(Authentication auth) {
    UserDetails currentUser = (UserDetails) auth.getPrincipal();
    return teamService.getTeamsByUsername(currentUser.getUsername());
}

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

/**
 * Get all Orders that the current User created. This doesn't include expired orders. This will be a Collection with
 * size 0 or 1 of the majority, but it can contain more orders.
 *
 * @param auth The current User/*from   ww w.ja v a  2  s.c o m*/
 *
 * @return A collection of Orders of the current User.
 */
@JsonView(View.OrderOverview.class)
@RequestMapping(value = "/orders", method = RequestMethod.GET)
public Collection<Order> getAllOrders(Authentication auth) {
    UserDetails currentUser = (UserDetails) auth.getPrincipal();
    return orderService.findOrdersByUsername(currentUser.getUsername());
}