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.ligoj.app.http.security.RestAuthenticationProviderTest.java

@Test
public void authenticateOverrideDifferentUser() {
    httpServer.stubFor(post(urlPathEqualTo("/"))
            .willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT).withHeader("X-Real-User", "other")));
    httpServer.start();/*  w  w w. j a v a  2  s . c  om*/
    final Authentication authentication = authenticate("http://localhost");
    Assertions.assertNotNull(authentication);
    Assertions.assertEquals("other", authentication.getName());
    Assertions.assertEquals("other", authentication.getPrincipal().toString());
}

From source file:org.ligoj.app.http.security.RestAuthenticationProviderTest.java

@Test
public void authenticate() {
    httpServer.stubFor(post(urlPathEqualTo("/")).willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT)));
    httpServer.start();//from w  w w . jav a2  s  .  c  om
    final Authentication authentication = authenticate("http://localhost");
    Assertions.assertNotNull(authentication);
    Assertions.assertEquals("junit", authentication.getName());
    Assertions.assertEquals("junit", authentication.getPrincipal().toString());

    Assertions.assertTrue(authenticationProvider.supports(Object.class));
}

From source file:de.iew.services.impl.UserDetailsServiceImpl.java

public Account getAuthenticatedUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    // TODO: Prfen ob wirklich eingeloggt. siehe dafr SecurityContextHolderAwareRequestWrapper Klasse

    Object principal = auth.getPrincipal();
    if (principal instanceof Account) {
        return (Account) principal;
    }//from  w  ww  . ja  v  a 2  s. com

    return null;
}

From source file:nz.net.orcon.kanban.security.GravityPermissionEvaluator.java

public boolean isAuthorised(Authentication authentication, Map<String, String> roles, List<String> filter) {

    if (authentication == null) {
        LOG.warn("No Authentication");
        return false;
    }//w w  w.j av  a 2 s .  c  om

    String username = (String) authentication.getPrincipal();

    Set<String> teams = new HashSet<String>();
    for (GrantedAuthority authority : authentication.getAuthorities()) {
        teams.add(authority.getAuthority());
    }

    for (Entry<String, String> entry : roles.entrySet()) {
        if (filter == null || filter.contains(entry.getValue())) {
            if (username.equals(entry.getKey())) {
                return true;
            }
            if (teams.contains(entry.getKey())) {
                return true;
            }
        }
    }
    LOG.warn("Unauthorized: " + username);
    return false;
}

From source file:org.jasig.portlet.blackboardvcportlet.service.impl.ConferenceUserServiceImpl.java

@Override
public ConferenceUser getConferenceUser(Authentication authentication) {
    final ConferenceSecurityUser principal = (ConferenceSecurityUser) authentication.getPrincipal();
    if (principal == null) {
        return null;
    }/*from ww w  . ja v a2 s . co m*/

    final String uniqueId = principal.getUniqueId();
    return this.conferenceUserDao.getUserByUniqueId(uniqueId);
}

From source file:base.security.permission.FlowchartPermissionEvaluator.java

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    if (authentication == null) {
        return false;
    }/*from   w  ww  .ja  v a2 s.  co  m*/
    Flowchart flowchart = (Flowchart) targetDomainObject;
    if (flowchart == null) {
        return true;
    }
    User currentUser = (User) authentication.getPrincipal();
    return currentUser.getId().equals(flowchart.getUser().getId());
}

From source file:eu.supersede.gr.rest.PlayerMoveRest.java

/**
 * Set the vote for of a player in his/her PlayerMove.
 * @param authentication//from w ww. j a  v a  2  s .  c  o  m
 * @param playerMoveId
 * @param vote
 * @return
 */
@RequestMapping(method = RequestMethod.PUT, value = "/{playerMoveId}/vote/{vote}")
public Long setPlayerMoveVote(Authentication authentication, @PathVariable Long playerMoveId,
        @PathVariable Long vote) {
    DatabaseUser currentUser = (DatabaseUser) authentication.getPrincipal();
    User user = users.findOne(currentUser.getUserId());

    HAHPPlayerMove playerMove = playerMoves.findOne(playerMoveId);
    playerMove.setValue(vote);
    playerMove.setPlayed(true);
    playerMove.setPlayedTime(new Date());
    playerMoves.save(playerMove);

    HAHPRequirementsMatrixData rmd = requirementsMatricesData
            .findOne(playerMove.getRequirementsMatrixData().getRequirementsMatrixDataId());

    Long criteriaId = rmd.getCriteria().getCriteriaId();

    // add points for player vote
    pointsLogic.addPoint(user, -1l, criteriaId);

    return rmd.getGame().getGameId();
}

From source file:org.ligoj.app.http.security.RestAuthenticationProviderTest.java

@Test
public void authenticateMixedCase() {
    httpServer.stubFor(post(urlPathEqualTo("/")).willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT)));
    httpServer.start();/* w w w  .  jav a  2 s .  co  m*/
    final Authentication authentication = authenticate("http://localhost", "jUniT");
    Assertions.assertNotNull(authentication);
    Assertions.assertEquals("junit", authentication.getName());
    Assertions.assertEquals("junit", authentication.getPrincipal().toString());
}

From source file:ch.silviowangler.dox.web.DocumentController.java

@RequestMapping(method = GET, value = "/document/{id}")
public void getDocument(@PathVariable("id") Long id, HttpServletResponse response) {
    try {//from ww w .  j  av a 2  s.c  o  m
        PhysicalDocument document = documentService.findPhysicalDocument(id);

        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = (authentication != null) ? ((User) authentication.getPrincipal()).getUsername()
                : "anonymous";
        statisticsService.registerDocumentReferenceClick(String.valueOf(id), username);

        response.setStatus(SC_OK);
        response.addHeader("Content-Type", document.getMimeType());
        response.addHeader("Content-Disposition", "inline; filename=\"" + document.getFileName() + "\"");
        response.getOutputStream().write(document.getContent());
        response.getOutputStream().flush();
    } catch (DocumentNotFoundException | DocumentNotInStoreException e) {
        logger.warn("Document with id '{}' not found", id, e);
        response.setStatus(SC_NOT_FOUND);
    } catch (AccessDeniedException ade) {
        logger.warn("Access denied on document {}", id, ade);
        response.setStatus(SC_FORBIDDEN);
    } catch (IOException e) {
        logger.error("Could not write document to output stream", e);
        response.setStatus(SC_INTERNAL_SERVER_ERROR);
    }
}