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.orchestra.portale.controller.UserEditController.java

@RequestMapping("userEditProfile")
@Secured("ROLE_USER")
public ModelAndView editUser(HttpServletRequest request) {
    ModelAndView model = new ModelAndView("userEditProfile", "command", new User());
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = pm.findUserByUsername(auth.getName());

    HttpSession session = request.getSession();
    ServletContext sc = session.getServletContext();
    File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
            + File.separator + user.getId() + File.separator + "avatar.jpg");
    if (dir.exists()) {
        model.addObject("avatar", "./dist/user/img/" + user.getId() + "/avatar.jpg");
    } else {/*w ww . j  ava2 s .co  m*/

        model.addObject("avatar", "./dist/img/default_avatar.png");
    }

    model.addObject("user", user);

    return model;

}

From source file:org.mzd.shap.spring.web.AbstractControllerSupport.java

/**
 * Get the authenticated user from the SecurityContext.
 * /*from  w w w  .  j ava2  s  .  co  m*/
 * @return user
 * @throws NotFoundException
 */
protected User getSessionUser() throws NotFoundException {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        User user = (User) auth.getPrincipal();
        if (user == null) {
            throw new NotFoundException("Authentication principle was null");
        }
        logger.debug(user);
        return user;
    }
    throw new NotFoundException("SecurityContext contained no authentication instance");
}

From source file:de.knightsoftnet.validators.server.security.LoggedInChecker.java

/**
 * get logged in user.//from w ww  . ja  v  a2 s.  c o  m
 *
 * @return UserData or null if no one is logged in
 */
public User getLoggedInUser() {
    User user = null;

    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();

        // principal can be "anonymousUser" (String)
        if (principal instanceof UserDetails) {
            user = this.userDetailsConverter.convert((UserDetails) principal);
        }
    }
    return user;
}

From source file:com.dnn.controller.LoginController.java

@RequestMapping(value = { "/sair" }, method = RequestMethod.GET)
public String sair(HttpServletRequest req, HttpServletResponse res) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        new SecurityContextLogoutHandler().logout(req, res, auth);
    }//from  w ww. j  av a  2s  . c  o m
    return "redirect:/acesso?sair";
}

From source file:org.slc.sli.dashboard.util.SecurityUtil.java

public static boolean isAdmin() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(Constants.ROLE_IT_ADMINISTRATOR)) {
                    return true;
                }//from ww w .  j  av  a 2 s  . co m
            }
        }
    }
    return false;
}

From source file:com.github.inspektr.audit.spi.support.SpringSecurityAuditablePrincipalResolver.java

private String getFromSecurityContext() {
    final SecurityContext securityContext = SecurityContextHolder.getContext();

    if (securityContext == null) {
        return null;
    }/*  ww  w  .  java 2  s  .  c o m*/

    return securityContext.getAuthentication().getName();
}

From source file:de.hska.ld.core.util.Core.java

public static boolean isAuthenticated() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    return securityContext != null && securityContext.getAuthentication() != null
            && securityContext.getAuthentication().isAuthenticated();
}

From source file:ch.ralscha.extdirectspring_itest.LoginController.java

@RequestMapping("/login")
@ResponseBody// ww w.  j  a  va  2  s .c  o m
public void login() {
    UserDetails ud = userManager.loadUserByUsername("jimi");
    if (ud != null) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(ud, null,
                ud.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(token);
    }
}

From source file:com.greglturnquist.payroll.DatabaseLoader.java

@Override
public void run(String... strings) throws Exception {

    Manager greg = this.managers.save(new Manager("greg", "turnquist", "ROLE_MANAGER"));
    Manager oliver = this.managers.save(new Manager("oliver", "gierke", "ROLE_MANAGER"));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("greg",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Frodo", "Baggins", "ring bearer", greg));
    this.employees.save(new Employee("Bilbo", "Baggins", "burglar", greg));
    this.employees.save(new Employee("Gandalf", "the Grey", "wizard", greg));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("oliver",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Samwise", "Gamgee", "gardener", oliver));
    this.employees.save(new Employee("Merry", "Brandybuck", "pony rider", oliver));
    this.employees.save(new Employee("Peregrin", "Took", "pipe smoker", oliver));

    SecurityContextHolder.clearContext();
}

From source file:com.example.server.user.CustomUserDetailsService.java

public void signin(User account) {
    SecurityContextHolder.getContext().setAuthentication(authenticate(account));
}