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.openmhealth.dsu.controller.EndUserController.java

/**
 * Deletes an existing user./*from   w  w w .  ja v  a  2  s.  c om*/
 * @param authentication
 * @return a response entity with status OK if the user was deleted, BAD_REQUEST if the request is invalid
 */
@PreAuthorize("#oath2.isUser()")
@RequestMapping(value = "/users/del", method = POST)
public ResponseEntity<String> deleteUser(Authentication authentication) {
    String user = ((EndUserUserDetails) authentication.getPrincipal()).getUsername();
    endUserService.delete(user);
    return new ResponseEntity<>("Success", OK);
}

From source file:nl.surfnet.coin.api.saml.SAMLAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    final SAMLAuthenticationToken newAuthenticationToken = new SAMLAuthenticationToken(
            authentication.getPrincipal(), authentication.getAuthorities());
    newAuthenticationToken.setAuthenticated(true);
    newAuthenticationToken.setDetails(authentication.getDetails());
    return newAuthenticationToken;
}

From source file:com.example.securelogin.app.common.interceptor.PasswordExpirationCheckInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws IOException {
    Authentication authentication = (Authentication) request.getUserPrincipal();

    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            LoggedInUser userDetails = (LoggedInUser) principal;
            if ((userDetails.getAccount().getRoles().contains(Role.ADMIN)
                    && accountSharedService.isCurrentPasswordExpired(userDetails.getUsername()))
                    || accountSharedService.isInitialPassword(userDetails.getUsername())) {
                response.sendRedirect(request.getContextPath() + "/password?form");
                return false;
            }//from  w  ww  . j a  va2 s .c  o  m
        }
    }

    return true;
}

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

@RequestMapping(value = "/list/detailed", method = RequestMethod.GET)
public List<User> getUserObjectList(Authentication authentication, @RequestParam Long processId) {
    DatabaseUser currentUser = (DatabaseUser) authentication.getPrincipal();
    Long userId = currentUser.getUserId();

    ProcessManager proc = DMGame.get().getProcessManager(processId);
    List<User> list = new ArrayList<>();

    for (HProcessMember member : proc.getProcessMembers()) {
        User user = DMGame.get().getUser(member.getUserId(), currentUser.getTenantId(), currentUser.getToken());

        if (user == null) {
            throw new NotFoundException("Can't get details of process member with id " + member.getUserId()
                    + " because it does not exist");
        }/* ww w. j a  v  a2  s. co m*/

        list.add(user);
    }

    return list;
}

From source file:org.energyos.espi.thirdparty.web.ScopeSelectionControllerTests.java

@Test
@Ignore/*  w  w w.j a  v a  2  s.  c o  m*/
public void post_scopeAuthorization_createsAuthorization() throws Exception {
    ApplicationInformation applicationInformation = EspiFactory.newApplicationInformation();
    when(applicationInformationService
            .findByDataCustodianClientId(eq(applicationInformation.getDataCustodianId())))
                    .thenReturn(applicationInformation);

    AuthorizationService authorizationService = mock(AuthorizationService.class);
    controller.setAuthorizationService(authorizationService);

    Authentication principal = mock(Authentication.class);
    when(principal.getPrincipal()).thenReturn(EspiFactory.newRetailCustomer());

    controller.scopeAuthorization(applicationInformation.getScopeArray()[0],
            applicationInformation.getDataCustodianId(), principal);

    verify(authorizationService).persist(any(Authorization.class));
}

From source file:com.katropine.oauth.CustomUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    LOGGER.warning("!!!Authenticate: " + authentication.getPrincipal().toString() + ":"
            + authentication.getCredentials().toString());

    if (!supports(authentication.getClass())) {
        return null;
    }//from w ww .ja  v a 2  s  .co  m
    if (authentication.getCredentials() == null) {
        LOGGER.warning("No credentials found in request.");
        boolean throwExceptionWhenTokenRejected = false;
        if (throwExceptionWhenTokenRejected) {
            throw new BadCredentialsException("No pre-authenticated credentials found in request.");
        }
        return null;
    }

    User user = userDAO.getByEmail(authentication.getPrincipal().toString());

    BCryptPasswordEncoder enc = new BCryptPasswordEncoder();
    if (!enc.matches(authentication.getCredentials().toString(), user.getPassword())) {
        throw new BadCredentialsException("Bad User Credentials.");
    }

    List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);

    return auth;

}

From source file:org.jtalks.common.security.acl.sids.JtalksSidFactory.java

/**
 * {@inheritDoc}/*w w  w .j  a v  a 2 s  . co  m*/
 */
@Override
public Sid createPrincipal(Authentication authentication) {
    Object principal = authentication.getPrincipal();
    if (principal instanceof UserDetails) {
        return new UserSid((User) principal);
    } else if (UserSid.isAnonymous(principal.toString())) {
        return UserSid.createAnonymous();
    } else {
        return new UserSid(principal.toString());
    }
}

From source file:com.kabiliravi.kaman.web.AuthenticationProviderImpl.java

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    return new UsernamePasswordAuthenticationToken(auth.getPrincipal(), auth.getCredentials());
}

From source file:cn.org.once.cstack.utils.SecurityContextUtil.java

public UserDetails getPrincipal() {
    logger.debug("Getting principal from the security context");

    UserDetails principal = null;/*from ww  w  .j ava2s.  c  o m*/

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null) {
        Object currentPrincipal = authentication.getPrincipal();
        if (currentPrincipal instanceof UserDetails) {
            principal = (UserDetails) currentPrincipal;
        }
    }

    return principal;
}

From source file:org.energyos.espi.thirdparty.web.ScopeSelectionControllerTests.java

@Test
@Ignore//from w  ww.  j  ava  2  s  .  c o  m
public void post_scopeAuthorization_redirects() throws Exception {
    ApplicationInformation applicationInformation = EspiFactory.newApplicationInformation();
    when(applicationInformationService
            .findByDataCustodianClientId(eq(applicationInformation.getDataCustodianId())))
                    .thenReturn(applicationInformation);

    String expectedRedirectURL = String.format(
            "redirect:%s?client_id=%s&redirect_uri=%s&response_type=%s&scope=%s&state=",
            applicationInformation.getAuthorizationServerAuthorizationEndpoint(),
            applicationInformation.getClientId(), applicationInformation.getRedirectUri(), "code",
            applicationInformation.getScopeArray()[0]);

    Authentication principal = mock(Authentication.class);
    when(principal.getPrincipal()).thenReturn(EspiFactory.newRetailCustomer());

    assertTrue(
            controller
                    .scopeAuthorization(applicationInformation.getScopeArray()[0],
                            applicationInformation.getDataCustodianId(), principal)
                    .startsWith(expectedRedirectURL));
}