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:th.co.geniustree.osgi.prototype.central.controller.AuthenController.java

public void reset() {
    sessionId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
            .get("sessionId");

    if (sessionId != null) {
        authentication = authenService.findAuthentication(sessionId);
        if (authentication != null) {
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }//w w  w  .j a v  a 2s.  c o m

        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("session.id", sessionId);
    }
}

From source file:org.opendatakit.security.spring.DigestAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        super.doFilter(request, response, chain);
    } else {//from w  w w . j  av a  2s . co  m
        chain.doFilter(request, response);
    }
}

From source file:org.openlmis.fulfillment.security.UserNameProvider.java

@Override
public String provide() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (null == auth) {
        return "unauthenticated user";
    }//from  w w w .  j av  a  2  s  . c  o  m

    Object principal = auth.getPrincipal();
    if (null == principal) {
        return "unknown user";
    }

    return principal.toString();
}

From source file:uk.co.caprica.bootlace.data.UsernameAuditor.java

@Override
public String getCurrentAuditor() {
    logger.debug("getCurrentAuditor()");
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String result;//from w  ww.j  a v  a  2s  .c om
    if (authentication != null && authentication.isAuthenticated()) {
        result = authentication.getName();
    } else {
        result = null;
    }
    logger.debug("result={}", result);
    return result;
}

From source file:cz.muni.fi.mir.wrappers.SecurityContextHolderFacade.java

@Override
public SecurityContext getContext() {
    return SecurityContextHolder.getContext();
}

From source file:eu.supersede.fe.multitenant.MultiTenancyInterceptor.java

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
    String multiTenantId = null;/*  www  . ja v a  2s .c om*/
    String tmp = null;

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

        if (user instanceof DatabaseUser) {
            tmp = ((DatabaseUser) user).getTenantId();
        }
    }

    if (tmp != null) {
        multiTenantId = tmp;
    } else {
        multiTenantId = req.getHeader("TenantId");
    }

    if (multiTenantId != null) {
        req.setAttribute("CURRENT_TENANT_IDENTIFIER", multiTenantId);
    }

    return true;
}

From source file:com.mastercard.test.spring.security.WithUserDetailsTests.java

@Test
@WithUserDetails
public void testWithDefaultWithUserDetails() {
    assertEquals("user", SecurityContextHolder.getContext().getAuthentication().getName());
}

From source file:co.com.ppit2.web.controller.HotelesController.java

@RequestMapping(value = "/hoteles.do", method = RequestMethod.GET)
public String hoteles(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)
            && authentication.isAuthenticated()) {
        return "hoteles/hoteles";
    }//from  w  w  w . j  a va2 s  .co m
    return "hoteles/hoteles";
}

From source file:Controller.UsuarioBean.java

public void pegarUsuarioSpringer() {
    usuario = new Usuario();
    SecurityContext context = SecurityContextHolder.getContext();
    if (context instanceof SecurityContext) {
        Authentication authentication = context.getAuthentication();
        if (authentication instanceof Authentication) {

            try {
                System.out/*from  ww  w .  j a  v  a 2  s. co m*/
                        .println("Teste de usurio: " + ((User) authentication.getPrincipal()).getUsername());
                usuario = UsuarioDAO.getInstance()
                        .buscarDadosUsuario(((User) authentication.getPrincipal()).getUsername());

            } catch (Exception ex) {
                Logger.getLogger(UsuarioBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:org.wicketopia.spring.security.SpringSecurityProvider.java

@Override
public boolean checkRoles(Set<String> roles) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext == null || securityContext.getAuthentication() == null
            || securityContext.getAuthentication().getAuthorities() == null) {
        return false;
    }//w  w  w. j  a v a  2 s  .  c o  m

    for (GrantedAuthority authority : securityContext.getAuthentication().getAuthorities()) {
        if (roles.contains(authority.getAuthority())) {
            return true;
        }
    }
    return false;
}