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.olegchir.wicket_spring_security_example.init.UserAuthenticatedWebSession.java

@Override
public Roles getRoles() {
    Roles roles = new Roles();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    for (GrantedAuthority authority : authentication.getAuthorities()) {
        roles.add(authority.getAuthority());
    }//from  ww w . ja v  a2  s .c  om
    return roles;
}

From source file:com.cfs.backingbean.AutenticacaoBacking.java

public void init() {
    if (SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString()
            .equals("[ADMINISTRADOR]")) {
        validaAdministrador = true;//from www  .  j av a  2s  .c om
    }
}

From source file:org.bisen.blog.model.AuditAwareImpl.java

@Override
@Transactional//from ww w .j  a  v a2 s  . c o  m
public BlogUser getCurrentAuditor() {
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null) {
        String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
        BlogUser user = blogService.findUser(currentUser);
        return user;
    }
    return null;
}

From source file:com.utils.SecurityContextReader.java

public String getUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        return authentication.getName();
    } else {/* w w  w  . java2 s  . co m*/
        return "";
    }
}

From source file:com.banco.controller.HomeController.java

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logoutPage(HttpServletRequest request, HttpServletResponse response) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        new SecurityContextLogoutHandler().logout(request, response, auth);
    }//from www  .  j  av a  2s  . c o  m
    return "redirect:/login?logout";//You can redirect wherever you want, but generally it's a good practice to show login screen again.
}

From source file:by.bsuir.finance.controllers.UserInfoController.java

@RequestMapping(value = "userinfo")
public ModelAndView getUserInfo() {
    ModelAndView model = new ModelAndView("userinfo");
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username
    mdoel.addObject("userinfo", infoService.getCurrentUserInfo(name));
    return userinfo;
}

From source file:io.galeb.core.entity.security.SpringSecurityAuditorAware.java

@Override
public String getCurrentAuditor() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String currentUser = "anonymousUser";

    if (authentication != null && authentication.isAuthenticated()) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            currentUser = ((UserDetails) principal).getUsername();
        } else {/*ww  w  . j a  va  2s . co m*/
            currentUser = principal.toString();
        }
    }

    return currentUser;
}

From source file:net.nan21.dnet.core.web.controller.ui.extjs.UiExtjsDashboardController.java

@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
protected ModelAndView home(HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {/*from   ww  w.  j  a va  2s  .  c o  m*/
        @SuppressWarnings("unused")
        ISessionUser su = (ISessionUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    } catch (java.lang.ClassCastException e) {
        // anonymous user
        throw new NotAuthorizedRequestException("Not authenticated");
    }
    Map<String, Object> model = new HashMap<String, Object>();
    this._prepare(model, request, response);

    /* ========== extensions =========== */

    model.put("extensions", getExtensionFiles(IExtensions.UI_EXTJS_DASHBOARD, uiExtjsSettings.getUrlCore()));
    return new ModelAndView(this.jspName, model);
}

From source file:com.isalnikov.config.web.listener.UserServletContextListener.java

private Object getPrincipal() {
    Object principal = null;/*from   w  w w.  j av  a2  s. co  m*/
    String username = null;
    try {
        principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            username = ((UserDetails) principal).getUsername();
        } else {
            username = principal.toString();
        }
    } catch (Exception e) {
    }

    System.out.println(principal);

    return username;

}

From source file:it.geosolutions.geobatch.ui.security.CurrentUser.java

public Object getUser() {
    return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}