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.springmodules.validation.valang.functions.InRoleFunction.java

protected Object doGetResult(Object target) {

    Object role = getArguments()[0].getResult(target);

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return Boolean.FALSE;
    }/*from  www .j a  v a  2  s  .  c o m*/

    Collection<GrantedAuthority> authorities = authentication.getAuthorities();

    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.equals(role)) {
            return Boolean.TRUE;
        }
    }

    return Boolean.FALSE;
}

From source file:org.apigw.appmanagement.revision.ApplicationManagementRevisionListener.java

@Override
public void newRevision(Object revisionEntity) {
    boolean isAdmin = false;
    String editor = "unknown";
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        logger.debug("changed by: {}", authentication);
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) principal;
            for (GrantedAuthority grantedAuthority : userDetails.getAuthorities()) {
                if (adminPattern.matcher(grantedAuthority.getAuthority()).find()) {
                    isAdmin = true;//w w w.  j  av  a2 s .  c  om
                    break;
                }
            }
            editor = userDetails.getUsername();
        }
    }
    ApplicationManagementRevision applicationManagementRevision = (ApplicationManagementRevision) revisionEntity;
    applicationManagementRevision.setEditor(editor);
    applicationManagementRevision.setEditorAdmin(isAdmin);
}

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

@WithUserDetails(value = "testuser", userDetailsServiceBeanName = "getUserDetailsService1")
@Test/*from   ww  w. j a v a  2s. c  om*/
public void testWithNameOverridenWithUserDetails() {
    assertEquals("testuser", SecurityContextHolder.getContext().getAuthentication().getName());
}

From source file:hu.bme.iit.quiz.controller.FileController.java

/**
 * *************************************************
 * URL: /file/upload upload(): Upload a single file
 * **************************************************
 *//*w  w w.j a  v a  2  s . c o m*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String uploadFileHandler(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {

    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return quizService.validateAndCreateQuizForUserFromFile(file, name, user.getUsername());
}

From source file:org.cloudfoundry.identity.uaa.security.DefaultSecurityContextAccessorTests.java

@Test
public void uaaUserIsUser() throws Exception {
    SecurityContextHolder.getContext()
            .setAuthentication(UaaAuthenticationTestFactory.getAuthentication("1234", "user", "user@test.org"));

    assertTrue(new DefaultSecurityContextAccessor().isUser());
}

From source file:org.socialsignin.exfmproxy.mvc.workaround.auth.WorkaroundExFmUserPasswordService.java

public String getAuthenticatedUserPassword() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication == null || authentication.getName().equals("anonymousUser") ? null
            : ((String) ((User) authentication.getPrincipal()).getPassword());
}

From source file:com.seajas.search.attender.controller.HomePageController.java

/**
 * User attribute.//from  ww  w .ja  v a  2 s .  com
 */
@ModelAttribute("user")
public User populateUser() {
    return ((ExtendedUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
            .getUser();
}

From source file:de.whs.poodle.controllers.LoginController.java

@RequestMapping(method = RequestMethod.GET)
public String login(HttpServletRequest request, RedirectAttributes redirectAttributes, Model model,
        @RequestParam(defaultValue = "0") boolean switchUserFailed) {
    if (switchUserFailed)
        redirectAttributes.addFlashAttribute("errorMessageCode", "userDoesntExist");

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    boolean isLoggedIn = !(auth instanceof AnonymousAuthenticationToken);

    if (!isLoggedIn) // not logged in yet, show login page
        return "login";
    else if (request.isUserInRole("ROLE_STUDENT")) // user is logged in, redirect to start page
        return "redirect:/student";
    else if (request.isUserInRole("ROLE_INSTRUCTOR"))
        return "redirect:/instructor";
    else { // user is logged in, but he is neither student nor instructor (no matching group in LDAP?)
        model.addAttribute("errorMessageCode", "noValidRole");
        return "login";
    }// w  w  w .j  a v a2  s . c  o m
}

From source file:de.randi2.aspects.LogAspects.java

/**
 * Log create new object./*from w w w  . j  a  va2 s . c  om*/
 * 
 * @param pjp
 *            the pjp
 * 
 * @throws Throwable
 *             the throwable
 */
@Around("execution(public void de.randi2.services.*.create*(..))")
public void logCreateNewObject(ProceedingJoinPoint pjp) throws Throwable {
    pjp.proceed();
    logService.logChange(ActionType.CREATE, SecurityContextHolder.getContext().getAuthentication().getName(),
            ((AbstractDomainObject) pjp.getArgs()[0]));
}

From source file:org.ng200.openolympus.SpringSecurityAuditorAware.java

@Override
public User getCurrentAuditor() {

    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication == null || !authentication.isAuthenticated()) {
        return null;
    }//w  w  w . ja v  a 2s  .c  o  m

    return this.userService.getUserByUsername(((Principal) authentication.getPrincipal()).getName());
}