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.schedoscope.metascope.service.MetascopeUserService.java

/**
 * Checks if the user browsing the application is logged in
 *
 * @return true if the user is logged in, false otherwise
 *//* w  w w .  j  av  a  2 s .c om*/
public boolean isAuthenticated() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        return (principal instanceof String) == false;
    }
    return false;
}

From source file:org.messic.server.facade.security.SecurityLoginSuccessHandler.java

@ApiMethod(path = "/messiclogin", verb = ApiVerb.POST, description = "process login", produces = {
        MediaType.APPLICATION_JSON_VALUE })
@ApiErrors(apierrors = { @ApiError(code = UnknownMessicRESTException.VALUE, description = "Unknown error") })
@ApiResponseObject//from  w ww .  j a  v a 2  s  . com
public void onAuthenticationSuccess(
        @ApiParam(name = "j_username", description = "username to login", paramType = ApiParamType.QUERY, required = true) HttpServletRequest request,
        @ApiParam(name = "j_password", description = "password to login", paramType = ApiParamType.QUERY, required = true) HttpServletResponse response,
        Authentication auth) throws IOException, ServletException {

    //        if ( "XMLHttpRequest".equals( request.getHeader( "X-Requested-With" ) )
    //            || ( request.getParameter( "json" ) != null && request.getParameter( "json" ).length() > 0 ) )
    //        {
    Login login = Login.sucessLogin(this.getTargetUrlParameter(), (String) request.getAttribute("messic_token"),
            ((User) auth.getPrincipal()).getUsername());
    response.getWriter().print(login.getJSON());
    response.setContentType("application/json");
    response.getWriter().flush();
    //        }
    //        else
    //        {
    //            super.onAuthenticationSuccess( request, response, auth );
    //        }

}

From source file:com.searchbox.framework.web.FavoriteController.java

@RequestMapping(value = "/{searchbox}/{preset}/{process}/mark_favorite", method = RequestMethod.GET)
public String markFavorite(@RequestParam String id, @RequestParam String title, @RequestParam String field,
        HttpServletRequest request, ModelAndView model, @PathVariable String process,
        @PathVariable PresetEntity preset, @ModelAttribute("collector") SearchCollector collector)
        throws ServletException, IOException {
    //        ModelAndView redirect = new ModelAndView(new RedirectView("/"
    //                + searchbox.getSlug() + "/" + preset.getSlug() + "/search" , true));
    //        String redirect = "redirect:/" + preset.getSlug() + "/search";
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (!(authentication instanceof AnonymousAuthenticationToken)) {
        UserEntity user = (UserEntity) authentication.getPrincipal();
        List<UserFavoriteEntity> list = service.findFavoriteByUserId(user);
        for (int i = 0; i < list.size(); i++) {
            UserFavoriteEntity u = list.get(i);
            if (u.getFavoriteId().equals(id)) {
                String referer = request.getHeader("Referer");
                return "redirect:" + referer;
            }// ww  w.  j a  v a2 s. com
        }

        service.markFavorite(user, id, title, field);
    }
    String referer = request.getHeader("Referer");
    return "redirect:" + referer;
}

From source file:com.stormpath.spring.security.provider.StormpathAuthenticationProvider.java

protected AuthenticationRequest createAuthenticationRequest(Authentication authentication) {
    String username = (String) authentication.getPrincipal();
    String password = (String) authentication.getCredentials();
    return new UsernamePasswordRequest(username, password);
}

From source file:com.searchbox.framework.web.FavoriteController.java

@RequestMapping(value = "/favorite/list_favorite", method = RequestMethod.GET)
public ModelAndView listFavorite(@RequestParam PresetEntity preset, @RequestParam SearchboxEntity searchbox,
        ModelAndView model) {/*from   w  w  w .  j a va 2  s  .  co  m*/
    List<String> idFields = new ArrayList<String>();
    String presetSlug = "all";
    if (preset != null) {
        presetSlug = preset.getSlug();
    }
    if ("funding".equals(presetSlug)) {
        idFields.add("topicIdentifier");
    } else if ("cooperations".equals(presetSlug)) {
        idFields.add("eenReferenceExternal");
        idFields.add("uid");
    } else if ("funded".equals(presetSlug)) {
        idFields.add("cordisId");
    }
    List<UserFavoriteEntity> list = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (!(authentication instanceof AnonymousAuthenticationToken)) {
        UserEntity user = (UserEntity) authentication.getPrincipal();

        if ("all".equals(presetSlug)) {
            list = service.findFavoriteByUserId(user);
        } else {
            list = service.findByPreset(user, idFields);
        }
    }
    model.addObject("preset", preset);
    model.addObject("searchbox", searchbox);
    model.addObject("favorites", list);
    return model;
}

From source file:eu.freme.common.persistence.model.OwnedResource.java

public void setCurrentUserAsOwner() throws AccessDeniedException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof AnonymousAuthenticationToken)
        throw new AccessDeniedException(
                "Could not create resource: The anonymous user can not own any resource. You have to be logged in to create a resource.");
    this.owner = (User) authentication.getPrincipal();
}

From source file:org.fracturedatlas.athena.helper.lock.manager.AthenaLockManager.java

private String getCurrentUsername() {
    Authentication authentication = contextHolderStrategy.getContext().getAuthentication();
    if (authentication != null && authentication.getPrincipal() != null
            && User.class.isAssignableFrom(authentication.getPrincipal().getClass())) {
        User user = (User) authentication.getPrincipal();
        return user.getUsername();
    }/*  w  w  w.j  av a 2s .  co m*/

    return NO_USER;
}

From source file:com.searchbox.framework.web.SearchboxController.java

public List<String> getFavorites() {
    List<String> param = new ArrayList();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (!(authentication instanceof AnonymousAuthenticationToken)) {
        UserEntity user = (UserEntity) authentication.getPrincipal();
        List<UserFavoriteEntity> list = service.findFavoriteByUserId(user);
        for (int i = 0; i < list.size(); i++) {
            UserFavoriteEntity u = list.get(i);
            String uid = u.getFavoriteId();
            param.add(uid);//from   w  w w  .  ja  va2 s  .  c  o m
        }

    }
    return param;
}

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

/**
 * Get all the playerMoves of the logged user.
 * @param authentication/*  ww w. ja  va2 s .  com*/
 * @param gameId
 * @param criteriaId
 * @param gameNotFinished
 */
@RequestMapping(value = "", method = RequestMethod.GET)
@Transactional
public List<HAHPPlayerMove> getPlayerMoves(Authentication authentication,
        @RequestParam(required = false) Long gameId, @RequestParam(required = false) Long criteriaId,
        @RequestParam(defaultValue = "false") Boolean gameNotFinished) {

    List<HAHPPlayerMove> moves;

    DatabaseUser currentUser = (DatabaseUser) authentication.getPrincipal();
    User player = users.findOne(currentUser.getUserId());

    if (gameNotFinished) {
        if (gameId != null && criteriaId != null) {
            HAHPGame game = games.getOne(gameId);
            ValutationCriteria criteria = criterias.getOne(criteriaId);
            moves = playerMoves.findByPlayerAndGameAndCriteriaAndGameNotFinished(player, game, criteria);
        } else if (gameId != null) {
            HAHPGame game = games.getOne(gameId);
            moves = playerMoves.findByPlayerAndGameAndGameNotFinished(player, game);
        } else if (criteriaId != null) {
            ValutationCriteria criteria = criterias.getOne(criteriaId);
            moves = playerMoves.findByPlayerAndCriteriaAndGameNotFinished(player, criteria);
        } else {
            moves = playerMoves.findByPlayerAndGameNotFinished(player);
        }
    } else {
        if (gameId != null && criteriaId != null) {
            HAHPGame game = games.getOne(gameId);
            ValutationCriteria criteria = criterias.getOne(criteriaId);
            moves = playerMoves.findByPlayerAndGameAndCriteria(player, game, criteria);
        } else if (gameId != null) {
            HAHPGame game = games.getOne(gameId);
            moves = playerMoves.findByPlayerAndGame(player, game);
        } else if (criteriaId != null) {
            ValutationCriteria criteria = criterias.getOne(criteriaId);
            moves = playerMoves.findByPlayerAndCriteria(player, criteria);
        } else {
            moves = playerMoves.findByPlayer(player);
        }
    }

    return moves;
}