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:com.vaadinspring.presenter.LoginPresenterImpl.java

public boolean hasRole(String role) {
    if (role.equals(SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator().next()
            .getAuthority()))/*  w ww  . j a  v a 2s  .  c  o  m*/
        return true;
    else
        return false;
}

From source file:de.itsvs.cwtrpc.sample1.server.service.LoginServiceImpl.java

public String login(String userName, String password) throws AuthenticationException {
    final StringBuilder roleNames = new StringBuilder();
    final Authentication auth;

    auth = SecurityContextHolder.getContext().getAuthentication();
    log.info("Login of user '" + auth.getName() + "' (session ID "
            + RemoteServiceContextHolder.getContext().getServletRequest().getSession().getId() + ")");

    for (GrantedAuthority ga : auth.getAuthorities()) {
        if (roleNames.length() > 0) {
            roleNames.append(", ");
        }//from   w ww  .j  av  a2 s.c o  m
        roleNames.append(ga.getAuthority());
    }

    return roleNames.toString();
}

From source file:com.greglturnquist.spring.social.ecobee.HomeController.java

@RequestMapping("/")
public String home(Principal currentUser, Model model) {

    model.addAttribute("connectionsToProviders", getConnectionRepository().findAllConnections());
    model.addAttribute("authentication", SecurityContextHolder.getContext().getAuthentication());
    return "home";
}

From source file:com.kodgemisi.web.sample.service.ItemService.java

public Long createAndAttachToUser(Item item) {

    Long retval = super.create(item);

    // Get the current user and add the item among its items
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    // User instance is detached from session when grabbed via SecurityContextHolder
    // So we need to bind the user instance to session again to make lazy loading of
    // `items` to work
    sessionFactory.getCurrentSession().refresh(user);

    user.getItems().add(item);//  w  ww .jav  a  2s  .c  o  m

    return retval;
}

From source file:ru.org.linux.auth.AuthUtil.java

public static boolean hasAuthority(String authName) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return false;
    }//from   ww  w .  j a va  2  s  .com

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

        if (auth.getAuthority().equals(authName)) {
            return true;
        }
    }
    return false;
}

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

public static String getPrincipal() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication.getName();
}

From source file:hu.petabyte.redflags.web.svc.AccountSvc.java

public Account getCurrent() {
    try {/*  w w  w. ja  v  a 2  s .c om*/
        WebUser user = (WebUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getAccount();
    } catch (Exception e) {
        return null;
    }
}

From source file:com.sasav.blackjack.controller.GameController.java

@RequestMapping(value = { "/game", "/" }, method = RequestMethod.GET)
public ModelAndView gamePage() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String login = auth.getName();
    ModelAndView mv = new ModelAndView("game");
    Game userGame = gameCore.getUserGame(login);
    if (userGame == null) {
        return new ModelAndView("error");
    }//from  w w w  .  j a  v a 2 s .  c om
    Account account = accountMaster.getAccountByUsername(login);
    if (account != null) {
        mv.addObject("accountAmount", account.getAmount());
    }
    mv.addObject("userGame", userGame);

    return mv;
}

From source file:de.qucosa.spring.RoleParameterAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String role = request.getParameter("role");
    if (role != null) {
        SecurityContext sctx = SecurityContextHolder.getContext();
        if (sctx.getAuthentication() == null) {
            sctx.setAuthentication(new UsernamePasswordAuthenticationToken(role, role));
        }//from   www .  j  av a  2 s.  co  m
    }
    chain.doFilter(request, response);
}

From source file:org.jrecruiter.web.actions.LogoutController.java

/**
 * Let's log out - Invalidate the session as well as the ACEGI security
 * context.//  w  w w.j  a v a2  s .  c om
 */
@RequestMapping({ "/admin/logout.html" })
public String execute() {

    final SecurityContext context = SecurityContextHolder.getContext();

    if (context.getAuthentication() != null) {
        LOGGER.info("Logging out user..." + context.getAuthentication().getName());
    } else {
        LOGGER.warn("User not logged in.");
    }

    context.setAuthentication(null);

    super.addActionMessage("You logged out successfully.");
    return "redirect:/";
}