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:edu.uiowa.icts.bluebutton.controller.AbstractBluebuttonController.java

@ModelAttribute("username")
public String getUsername() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        return auth.getName();
    }/*w  ww  .j  a v  a  2s. co  m*/
    return "anonymousUser";
}

From source file:io.dacopancm.jfee.managedController.EditarFuncionarioBean.java

@PostConstruct
public void postConstruct() {
    try {/* w  w  w.j ava 2  s.com*/
        User userDetails = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        selectedPersonal = personalService.getPersonalByCi(userDetails.getUsername());
    } catch (Exception ex) {

    }
}

From source file:org.consultjr.mvc.core.base.ApplicationController.java

public ApplicationController() {
    this.logger = LoggerFactory.getLogger(this.getClass());
    SecurityContext secutiryContext = SecurityContextHolder.getContext();
    this.auth = secutiryContext.getAuthentication();
}

From source file:ru.mystamps.web.support.spring.security.CustomUserDetailsArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {

    SecurityContext ctx = SecurityContextHolder.getContext();
    Validate.validState(ctx != null, "Security context must be non null");

    Authentication auth = ctx.getAuthentication();
    if (auth == null) {
        return null;
    }/*  w w w .  j  a va  2s .com*/

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

    if (isSupportedType(principal.getClass())) {
        return principal;
    }

    return null;
}

From source file:org.meruvian.yama.web.DefaultCredentialsService.java

@Override
public void registerAuthentication(String userId) {
    User user = userRepository.findById(userId);
    UserDetails userDetails = userDetailsService.loadUserByUsername(user.getUsername());

    Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, null,
            userDetails.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
}

From source file:gov.gtas.controller.CaseDispositionController.java

@RequestMapping(value = "/getAllCaseDispositions", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CasePageDto getAll(@RequestBody CaseRequestDto request, HttpServletRequest hsr) {
    hsr.getSession(true).setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
    return caseDispositionService.findAll(request);
}

From source file:org.meruvian.yama.service.DefaultSessionCredential.java

@Override
public org.meruvian.yama.repository.user.User getCurrentUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()) {
        return null;
    }//  w ww.j  a  va  2 s  . com

    if (authentication.getPrincipal() instanceof DefaultUserDetails) {
        DefaultUserDetails user = (DefaultUserDetails) authentication.getPrincipal();
        return user.getUser();
    }

    return null;
}

From source file:nl.surfnet.coin.selfservice.filter.SpringSecurityUtil.java

public static void setAuthentication(CoinUser coinUser) {

    final TestingAuthenticationToken token = new TestingAuthenticationToken(coinUser, "");
    token.setAuthenticated(true);/*from   ww  w  .  j a  va 2 s  . co  m*/

    SecurityContextHolder.getContext().setAuthentication(token);

}

From source file:com.innovaciones.reporte.controller.LoginController.java

public void controlSessionSpringSecurity() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        redireccionar("paginas/menu/menu-principal.jsf");
    }/*from  w ww  .  j  a va2 s . com*/

}

From source file:com.mtt.myapp.user.service.UserContextTest.java

@Test
public void testGetUser() {
    UserContext userCtx = new UserContext();

    //in super.beforeSetSecurity(), there is an admin user is set, but the auth is invalid
    try {//from  ww w .ja v  a 2s. com
        userCtx.getCurrentUser();
        assertTrue(false);
    } catch (AuthenticationCredentialsNotFoundException e) {
        assertTrue(true);
    }

    UserDetails user = securityUserDetailService.loadUserByUsername(getTestUser().getUserId());

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

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "123");
    SecurityContextHolder.getContext().setAuthentication(token);
    userCtx.getCurrentUser();
    assertTrue(true);

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