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:at.ac.univie.isc.asio.security.HttpMethodRestrictionFilterTest.java

@Test
public void should_keep_rememberme_type() throws Exception {
    final RememberMeAuthenticationToken token = new RememberMeAuthenticationToken("key", "principal",
            Collections.<GrantedAuthority>singletonList(Permission.INVOKE_UPDATE));
    token.setDetails("details");
    setAuthentication(token);/*from  w w w. j  a v a  2  s . c  o m*/
    request.setMethod(HttpMethod.GET.name());
    subject.doFilter(request, response, chain);
    final Authentication filtered = getAuthentication();
    assertThat(filtered, instanceOf(RememberMeAuthenticationToken.class));
    assertThat(filtered.getPrincipal(), equalTo(token.getPrincipal()));
    assertThat(filtered.getDetails(), equalTo(token.getDetails()));
}

From source file:com.acc.storefront.security.AcceleratorAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
            : authentication.getName();/* w ww .j ava  2s  .  co m*/

    if (getBruteForceAttackCounter().isAttack(username)) {
        try {
            final UserModel userModel = getUserService().getUserForUID(StringUtils.lowerCase(username));
            userModel.setLoginDisabled(true);
            getModelService().save(userModel);
            bruteForceAttackCounter.resetUserCounter(userModel.getUid());
        } catch (final UnknownIdentifierException e) {
            LOG.warn("Brute force attack attempt for non existing user name " + username);
        } finally {
            throw new BadCredentialsException(
                    messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
        }
    }

    checkCartForUser(username);
    return super.authenticate(authentication);

}

From source file:com.himanshu.poc.h2.springboot.AuthenticationProviderImpl.java

@Override
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
    System.out.println(" User name is : " + arg0.getName());
    //arg0.setAuthenticated(false);
    //return arg0;
    if (dummyUsernamePwdMap.get(arg0.getPrincipal()) != null
            && dummyUsernamePwdMap.get(arg0.getPrincipal()).equals(arg0.getCredentials())) {
        System.out.println("Auth success");
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(arg0.getPrincipal(),
                arg0.getCredentials(), arg0.getAuthorities());
        return token;
    }/*  w ww .  j  ava  2  s  . c o  m*/
    System.out.println("Auth failed");
    return null;
}

From source file:org.deegree.securityproxy.service.commons.responsefilter.clipping.AbstractClippingResponseFilterManager.java

private RasterUser retrieveRasterUser(Authentication auth) {
    Object principal = auth.getPrincipal();
    if (!(principal instanceof RasterUser)) {
        throw new IllegalArgumentException("Principal is not a RasterUser!");
    }//from   w  ww .ja v  a 2  s .c o m
    return (RasterUser) principal;
}

From source file:org.atomsphere.management.svc.authentication.UserServiceImpl.java

public IUser getCurrentUser() {
    Authentication authentication = AuthenticationUtils.getAuthentication();
    if (authentication != null) {
        IUser user = (IUser) authentication.getPrincipal();

        return user;
    }/* ww  w  .j  av a2 s .  c o m*/

    return null;
}

From source file:sk.lazyman.gizmo.security.GizmoAuthProvider.java

private Authentication authenticateUsingLdap(Authentication authentication) throws AuthenticationException {
    String principal = (String) authentication.getPrincipal();
    DirContextOperations ctx = ldapBindAuthenticator.authenticate(authentication);

    User user = userRepository.findUserByName(principal);
    if (user == null) {
        user = createUser(ctx, principal);
    }/*from w w w .j  a  v  a2s.co m*/

    if (!user.isEnabled()) {
        throw new BadCredentialsException("GizmoAuthenticationProvider.userDisabled");
    }

    GizmoPrincipal gizmoPrincipal = new GizmoPrincipal(user);

    LOGGER.debug("User '{}' authenticated ({}), authorities: {}", new Object[] { authentication.getPrincipal(),
            authentication.getClass().getSimpleName(), gizmoPrincipal.getAuthorities() });
    return new UsernamePasswordAuthenticationToken(gizmoPrincipal, null, gizmoPrincipal.getAuthorities());
}

From source file:de.hybris.telcotrail.storefront.security.AcceleratorAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
            : authentication.getName();/*from  w w w  .ja va  2  s . c  om*/

    if (getBruteForceAttackCounter().isAttack(username)) {
        try {
            final UserModel userModel = getUserService().getUserForUID(StringUtils.lowerCase(username));
            userModel.setLoginDisabled(true);
            getModelService().save(userModel);
            bruteForceAttackCounter.resetUserCounter(userModel.getUid());
        } catch (final UnknownIdentifierException e) {
            LOG.warn("Brute force attack attempt for non existing user name " + username);
        } finally {
            throw new BadCredentialsException(
                    messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
        }
    }

    // check if the user of the cart matches the current user and if the
    // user is not anonymous. If otherwise, remove delete the session cart as it might
    // be stolen / from another user
    final String sessionCartUserId = getCartService().getSessionCart().getUser().getUid();

    if (!username.equals(sessionCartUserId)
            && !sessionCartUserId.equals(userService.getAnonymousUser().getUid())) {
        getCartService().setSessionCart(null);
    }

    return super.authenticate(authentication);
}

From source file:com.rcn.controller.ResourceController.java

@RequestMapping(value = "/create-certificate", method = RequestMethod.GET)
public String createCert(Authentication principal, Model model) {
    RcnUserDetail user = (RcnUserDetail) principal.getPrincipal();
    Long targetUserId = user.getTargetUser().getId();
    List<CodeName> codeValues = resourceRepository.caList(user.getId(), targetUserId);
    model.addAttribute("caList", codeValues);
    return "create-certificate";
}

From source file:sk.lazyman.gizmo.security.GizmoAuthProvider.java

private Authentication authenticateUsingDb(Authentication authentication) throws AuthenticationException {
    String principal = (String) authentication.getPrincipal();
    String password = (String) ((UsernamePasswordAuthenticationToken) authentication).getCredentials();
    User user = userRepository.findUserByName(principal);
    if (user == null) {
        throw new BadCredentialsException("web.security.provider.invalid");
    }/*from w ww .  j  a  va  2 s  .  com*/

    if (user.getPassword() == null || !user.getPassword().equals(GizmoUtils.toSha1(password))) {
        throw new BadCredentialsException("GizmoAuthenticationProvider.userPasswordIncorrect");
    }

    if (!user.isEnabled()) {
        throw new BadCredentialsException("GizmoAuthenticationProvider.userDisabled");
    }

    GizmoPrincipal gizmoPrincipal = new GizmoPrincipal(user);

    LOGGER.debug("User '{}' authenticated ({}), authorities: {}", new Object[] { authentication.getPrincipal(),
            authentication.getClass().getSimpleName(), gizmoPrincipal.getAuthorities() });
    return new UsernamePasswordAuthenticationToken(gizmoPrincipal, null, gizmoPrincipal.getAuthorities());
}

From source file:org.jblogcms.core.security.service.SecurityServiceImpl.java

@Override
public AccountDetails getCurrentAccount() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    try {/*ww  w  .j a  v a 2 s. co  m*/
        if (!auth.getName().equals("anonymousUser")) {
            return ((AccountDetails) auth.getPrincipal());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}