Example usage for org.springframework.security.core.context SecurityContextHolder getContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContextHolder getContext.

Prototype

public static SecurityContext getContext() 

Source Link

Document

Obtain the current SecurityContext.

Usage

From source file:org.woofenterprise.dogs.web.controllers.WelcomeController.java

@RequestMapping("/")
public String welcomePage(Model model) {
    List<AppointmentDTO> appointments = new ArrayList<>(facade.findAllAppointmentsForToday());
    Collections.sort(appointments, new Comparator<AppointmentDTO>() {

        @Override// ww  w .j a va  2s . c  o m
        public int compare(AppointmentDTO o1, AppointmentDTO o2) {
            return o1.getStartTime().compareTo(o2.getStartTime());
        }
    });
    model.addAttribute("appointments", appointments);

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

    for (GrantedAuthority auth : authentication.getAuthorities()) {

        log.warn("SECURITY PRINCIPLES ROLES: {}", auth.getAuthority());
    }
    return "welcome";
}

From source file:com.auditbucket.helper.SecurityHelper.java

public String getUserName(boolean exceptionOnNull, boolean isSysUser) {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    if (a == null)
        if (exceptionOnNull)
            throw new SecurityException("User is not authenticated");
        else/*from   ww w .j  a v  a2  s  .  c  om*/
            return null;

    if (isSysUser) {
        SystemUser su = getSysUser(a.getName());
        if (su == null)
            throw new IllegalArgumentException("Not authorised");
    }
    return a.getName();
}

From source file:com.epam.ipodromproject.controller.ArchivedBetsController.java

@RequestMapping(value = "getTotalArchiveBetsPages", method = RequestMethod.GET)
@ResponseBody//from   w  w  w. j a v a2  s. com
public int getTotalArchiveBetsPages(@RequestParam(value = "resultsPerPage") Integer resultsPerPage) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    return (int) betService.getTotalArchiveBetsPagesMadeByUser(username, resultsPerPage);
}

From source file:ch.javaee.basicMvc.web.controller.AccessController.java

@RequestMapping("/login/success")
public String loginSuccess() {
    userSessionComponent.setCurrentUser(userRepository.findUserByUsername(
            ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
                    .getUsername()));//  www  . j  ava  2s.  c o  m
    return "view/user/profile";
}

From source file:tds.tdsadmin.web.backingbean.UserBean.java

public SbacUser getUser() {
    if (user == null)
        user = (SbacUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return user;
}

From source file:oauth2.services.ChangePasswordService.java

@PreAuthorize("hasAnyRole('ROLE_AUTHENTICATED', 'ROLE_MUST_CHANGE_PASSWORD')")
public void changePassword(String userId, String oldPassword, String newPassword) {

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

    // Step 1: Check user id against authenticated user
    if (!Objects.equal(userId, authentication.getName())) {
        throw new ChangePasswordException("Invalid user id");
    }/*from ww  w  .  j a va  2 s.  c om*/

    // Step 2: Search user in repository
    User user = userRepository.findByUserId(userId);
    if (user == null) {
        throw new ChangePasswordException("User not found");
    }

    // Step 3: Validate if old password is correct.
    authenticationStrategy.authenticate(user, oldPassword);

    // Step 4: Validate new password.
    changePassword(user, newPassword);

    SecurityContextHolder.getContext().setAuthentication(null);
}

From source file:com.epam.ipodromproject.controller.MakeABetControllec.java

@RequestMapping(value = "makeABet", method = RequestMethod.POST)
@ResponseBody/*from www  .j a v a  2 s  .  co  m*/
public String tryToMakeABet(@RequestParam("competitionID") Long competitionID,
        @RequestParam("moneyToBet") String moneyToBet, @RequestParam("horseID") String horseID,
        @RequestParam("typeOfBet") String typeOfBet, Model model) {
    User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    if (user.getEnabled() == false) {
        return "this_user_is_blocked";
    }
    Competition competitionToBet = competitionService.getCompetitionByID(competitionID);
    if (!mainService.makeBet(user, competitionToBet, moneyToBet, BetType.valueOf(typeOfBet), horseID)) {
        return "could_not_make_bet";
    } else {
        return "bet_was_successfully_made";
    }
}

From source file:org.taverna.server.master.identity.AuthorityDerivedIDMapper.java

@Override
public String getUsernameForPrincipal(UsernamePrincipal user) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null || !auth.isAuthenticated())
        return null;
    for (GrantedAuthority authority : auth.getAuthorities()) {
        String token = authority.getAuthority();
        if (token == null)
            continue;
        if (token.startsWith(prefix))
            return token.substring(prefix.length());
    }//from ww  w.j  a  va2  s. com
    return null;
}

From source file:org.unidle.service.UserServiceImpl.java

@Override
@Transactional(readOnly = true)/*w ww  .j a v  a2 s  .c om*/
public User currentUser() {

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

    if (authentication == null) {
        return null;
    }

    final String uuid = authentication.getName();

    return userRepository.findOne(UUID.fromString(uuid));
}