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.ff4j.security.SpringSecurityAuthorisationManager.java

/** {@inheritDoc} */
public Set<String> getCurrentUserPermissions() {
    Set<String> listOfRoles = new LinkedHashSet<String>();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
            listOfRoles.add(grantedAuthority.getAuthority());
        }/*from  w  w  w . jav a 2 s  .  c  om*/
    }
    return listOfRoles;
}

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

@RequestMapping(value = "getActiveBetsPage", method = RequestMethod.GET)
public String getActiveBetsPage(@RequestParam(value = "page") Integer page,
        @RequestParam(value = "resultsPerPage") Integer resultsPerPage, Model model) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    int totalPages = (int) betService.getTotalActiveBetsPagesMadeByUser(username, resultsPerPage);
    int pageToShow;
    if ((page == null) || (page <= 0)) {
        pageToShow = 1;//from   w  w  w. j  ava  2s .  c  om
    } else if (page > totalPages) {
        pageToShow = totalPages;
    } else {
        pageToShow = page;
    }
    model.addAttribute("betsMade", betService.getActiveBetsMadeByUser(username, pageToShow, resultsPerPage));
    return "lists/activeBetsDropList";
}

From source file:org.echocat.marquardt.service.spring.SpringSecurityCertificateAuthenticationFilter.java

@Override
protected void authenticateUser(final Certificate<SIGNABLE> certificate) {
    SecurityContextHolder.getContext().setAuthentication(
            new CertificateAuthenticationWrapper(getIdentifier(certificate.getPayload()), certificate));
}

From source file:com.mothsoft.alexis.web.security.StoreUsernameInSessionFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    final HttpSession session = request.getSession(false);
    if (session != null && SecurityContextHolder.getContext().getAuthentication() != null) {
        session.setAttribute(USERNAME, SecurityContextHolder.getContext().getAuthentication().getName());
    }/*from  w w  w.  j a  va2  s .  c o  m*/
    chain.doFilter(request, response);
}

From source file:eu.supersede.fe.locale.UserPreferredOrHeaderLocaleResolver.java

/**
 * Return the locale to be used for the given request.
 *//*from w  w  w .j a  v  a 2s.  co  m*/
@Override
public Locale resolveLocale(HttpServletRequest request) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Locale loc = request.getLocale();

    if ((authentication == null) || (authentication instanceof AnonymousAuthenticationToken)) {
        return loc;
    }

    if (SecurityContextHolder.getContext().getAuthentication() != null
            && SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
        Object user = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (user instanceof DatabaseUser) {
            DatabaseUser dbUser = (DatabaseUser) user;
            String l = dbUser.getLocale();

            if (l != null && !l.equals("")) {
                loc = new Locale.Builder().setLanguage(l).build();
            }
        }
    }

    return loc;
}

From source file:rights.HasAllRightsTag.java

private boolean hasCommonRights() {
    WebInvocationPrivilegeEvaluator wipe = (WebInvocationPrivilegeEvaluator) WebApplicationContextUtils
            .getWebApplicationContext(pageContext.getServletContext())
            .getBean(WebInvocationPrivilegeEvaluator.class);
    return wipe.isAllowed(url, SecurityContextHolder.getContext().getAuthentication());
}

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

@RequestMapping(value = "registerHorse", method = RequestMethod.POST)
@ResponseBody/*from w  ww  . j a va 2 s  .  c o  m*/
public String registerHorse(@RequestParam("horseName") String horseName, Model model) {
    User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    if (user.getEnabled() == false) {
        return "this_user_is_blocked";
    }
    horseName = horseName.trim();
    if (horseName.isEmpty()) {
        return "name_of_horse_cannot_be_empty";
    }
    if (horseService.registerHorse(horseName)) {
        return "horse_successfully_added";
    } else {
        return "horse_with_this_name_already_exists";
    }
}

From source file:com.pamarin.income.security.DefaultBasicAuthenImpl.java

/**
 * @param username//  www .j a v  a2s  . c o  m
 * @param password
 */
@Override
public void login(String username, String password) throws UsernameNotFoundException {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    //check authen from user details service
    UserDetails userDetails = detailsService.loadUserByUsername(username);
    Authentication authentication = manager
            .authenticate(new UsernamePasswordAuthenticationToken(userDetails, password));
    //keep authentication to security context 
    securityContext.setAuthentication(authentication);
}

From source file:se.omegapoint.facepalm.client.config.ControllerConfig.java

@ModelAttribute("user")
public User user() {
    if (SecurityContextHolder.getContext().getAuthentication() instanceof UsernamePasswordAuthenticationToken) {
        final AuthenticatedUser authenticatedUser = (AuthenticatedUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        return new User(authenticatedUser);
    }/*  ww w .  j  a  va  2 s . c  om*/
    return null;
}