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.shredzone.cilla.service.impl.SecurityServiceImpl.java

@Override
public CillaUserDetails getAuthenticatedUser() {
    Object details = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (details != null && details instanceof CillaUserDetails) {
        return (CillaUserDetails) details;
    }//from  w w  w.j  a  v a 2 s  .c  o  m
    return null;
}

From source file:com.orchestra.portale.controller.UserInfoController.java

@RequestMapping(value = "/userInfo")
@Secured("ROLE_USER")
public ModelAndView getUserInfo(HttpServletRequest request) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    ModelAndView model = new ModelAndView("userInfo");
    ModelAndView model2 = new ModelAndView("index");
    if (auth != null) {
        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  a  v  a2s.  com*/
            model.addObject("avatar", "./dist/img/default_avatar.png");
        }

        model.addObject("user", user);

        if (request.isUserInRole("ROLE_FB")) {
            Object categories = fbprofiler.getFBCategories();
            if (categories != null) {
                model.addObject("categories", categories);
            }
        }
    } else {
        return model2;
    }
    return model;
}

From source file:eu.supersede.fe.notification.NotificationUtil.java

private String getCurrentTenant() {
    String tenant = null;// ww w  . j  a  v  a2s. c o  m

    SecurityContext securityContext = SecurityContextHolder.getContext();

    if (securityContext != null) {
        Authentication authentication = securityContext.getAuthentication();

        if (authentication != null && authentication.getPrincipal() instanceof DatabaseUser) {
            DatabaseUser userDetails = (DatabaseUser) authentication.getPrincipal();
            tenant = userDetails.getTenantId();
        }
    }

    return tenant;
}

From source file:net.prasenjit.auth.controller.MeController.java

/**
 * <p>me.</p>/*from  w ww.ja v a2 s.  co  m*/
 *
 * @return a {@link net.prasenjit.auth.domain.User} object.
 */
@RequestMapping(value = "/api/me", method = RequestMethod.GET)
public User me() {
    return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

From source file:de.sainth.recipe.backend.rest.controller.SingleUserController.java

@RequestMapping()
User getCurrentUser() {/* w  w  w. j av  a2s  .co  m*/
    RecipeManagerAuthenticationToken token = (RecipeManagerAuthenticationToken) SecurityContextHolder
            .getContext().getAuthentication();
    return repository.findOne(token.getPrincipal());
}

From source file:org.bremersee.common.security.core.context.RunAsUtil.java

/**
 * Executes the callback with the specified authority (name and roles).
 *
 * @param name     the name of the executing authority
 * @param roles    the roles of the executing authority
 * @param callback the callback which should be executed
 *///  w  w w. jav  a 2  s  . co m
public static void runAs(final String name, final String[] roles, final RunAsCallbackWithoutResult callback) {

    Validate.notBlank(name, "Name mast not be null or blank");
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    try {
        SecurityContextHolder.getContext().setAuthentication(new RunAsAuthentication(name, roles));
        callback.run();

    } finally {
        SecurityContextHolder.getContext().setAuthentication(auth);
    }
}

From source file:org.openeos.services.security.internal.SecurityManagerServiceImpl.java

@Override
public Principal getAuthenticatedPrincipal() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null || !auth.isAuthenticated()) {
        // TODO/*www . j a  v a2  s. co  m*/
        throw new RuntimeException("Actually not user is logged in");
    }

    Object principal = auth.getPrincipal();
    if (principal instanceof InternalUser) {
        InternalUser userDetails = (InternalUser) principal;
        return new PrincipalImpl(userDetails.getId());
    } else {
        throw new RuntimeException("The class of principal is unknown");
    }
}

From source file:rashjz.info.com.az.util.UserLoginSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse, Authentication authentication)
        throws IOException, ServletException {

    HttpSession session = httpServletRequest.getSession();
    UserDetails authUser = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    //        AppUser localUser = new AppUser(null, authUser.getUsername(), authUser.getUsername(), true, true, true, true, authUser.getAuthorities());
    //        SecurityUtil.authenticateUser(localUser);
    session.setAttribute("username", authUser.getUsername());
    session.setAttribute("authorities", authentication.getAuthorities());
    //set our response to OK status
    httpServletResponse.setStatus(HttpServletResponse.SC_OK);
    httpServletResponse.sendRedirect("admin");

}

From source file:de.interseroh.report.test.security.DemoSecurityServiceImpl.java

@PreAuthorize("authenticated")
@Secured("ROLE_USER")
public String getMessage() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    logger.info("Security Context: {}", SecurityContextHolder.getContext());

    String helloStr = "Hello " + authentication;
    logger.info(helloStr);//from w w w .  j  av a  2s.c  om

    return helloStr;
}

From source file:com.github.cherimojava.orchidae.TestBase.java

protected void setAuthentication(Authentication auth) {
    SecurityContextHolder.getContext().setAuthentication(auth);
}