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.socialhistoryservices.pid.security.NAAuthentication.java

/**
 * Retrieves the user.//from   w w  w  . j  a  v a  2  s .  c om
 *
 * @return
 */
public List<String> authenticate() {

    final SecurityContext context = SecurityContextHolder.getContext();
    final OAuth2Authentication authentication = (OAuth2Authentication) context.getAuthentication();
    return NamingAuthority.getNaRole(authentication.getUserAuthentication());
}

From source file:com.epam.ipodromproject.controller.AdminController.java

@RequestMapping(value = "blockUser", method = RequestMethod.POST)
@ResponseBody/*w w w. ja  v a 2 s .c  om*/
public String blockUser(@RequestParam("userID") Long userID, @RequestParam("block") Boolean block) {
    User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    if (user.getEnabled() == false) {
        return "this_user_is_blocked";
    }
    if (!userService.blockOrUnblockUser(userID, block)) {
        return (block == true) ? "Cant_block_this_user" : "Cant_unblock_this_user";
    } else {
        return (block == true) ? "User_successfully_blocked" : "User_successfully_unblocked";
    }
}

From source file:de.itsvs.cwtrpc.sample1.server.service.SampleServiceImpl.java

public String getInfo() {
    final StringBuilder info = new StringBuilder();
    final Authentication auth;
    boolean first;

    auth = SecurityContextHolder.getContext().getAuthentication();
    log.info("User '" + auth.getName() + "' is requesting info");

    info.append("Number of Requests: " + (++infoCount) + "\n");
    info.append("User Name: " + auth.getName() + "\n");
    info.append("Roles: ");

    first = true;//from  ww  w .j  a  va  2 s . c  o m
    for (GrantedAuthority ga : auth.getAuthorities()) {
        if (!first) {
            info.append(", ");
        }
        first = false;
        info.append(ga.getAuthority());
    }

    return info.toString();
}

From source file:nl.surfnet.coin.selfservice.control.LogoutController.java

@RequestMapping("/logout.shtml")
public ModelAndView logout(HttpServletRequest request, SessionStatus status) {
    status.setComplete();//from  w w  w  . j  av  a  2 s  .  co  m
    Map<String, Object> m = new HashMap<>();
    request.getSession().invalidate();
    SecurityContextHolder.getContext().setAuthentication(null);
    return new ModelAndView("logout", m);
}

From source file:com.github.javarch.jsf.tags.security.SpringSecurityELLibrary.java

private static GrantedAuthority[] getUserAuthorities() {
    if (SecurityContextHolder.getContext() == null) {
        System.out.println("security context is empty, this seems to be a bug/misconfiguration!");
        return new GrantedAuthority[0];
    }/*  www .j  a  v a 2s  . c o  m*/
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (currentUser == null)
        return new GrantedAuthority[0];

    if (currentUser.getAuthorities() == null)
        return new GrantedAuthority[0];

    return currentUser.getAuthorities().toArray(new GrantedAuthority[] {});
}

From source file:mb.MbKorisnik.java

public String koJeKorisnik() {
    return SecurityContextHolder.getContext().getAuthentication().getName();
}

From source file:eu.gyza.eap.facebook.FacebookProfileController.java

@RequestMapping(value = "/facebook", method = RequestMethod.GET)
public String home(Model model) {
    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken("deslos@yahoo.com", null, null));
    Connection<Facebook> connection = connectionRepository.findPrimaryConnection(Facebook.class);
    if (connection == null) {
        return "redirect:/connect/facebook";
    }/* w w w.  j  av  a2  s  .c  om*/
    model.addAttribute("profile", connection.getApi().userOperations().getUserProfile());
    return "facebook/profile";
}

From source file:fr.xebia.audit.Auditor.java

/**
 * <p>//from   w  w  w  .j  a  v a  2s.c  o m
 * Emmits the audit message : <code>
 * "$date{yyyy-MM-dd'T'HH:mm:ss.SSSZZ} ${message} by ${spring-security-user}|anonymous [coming from ${remote-address}]"</code>.
 * <p>
 * <p>
 * If the Spring Security authentication is <code>null</code>, 'anonymous'
 * is emmitted.
 * </p>
 * <p>
 * If the Spring Security authentication details are
 * {@link WebAuthenticationDetails}, the incoming
 * {@link WebAuthenticationDetails#getRemoteAddress()} is emmitted.
 * </p>
 * 
 * @param message
 *            message to audit
 * @see SecurityContextHolder#getContext()
 */
public static void audit(String message) {
    if (message == null) {
        message = "";
    }
    StringBuilder msg = new StringBuilder(40 + message.length());

    SimpleDateFormat simpleDateFormat = (SimpleDateFormat) dateFormatPrototype.clone();
    msg.append(simpleDateFormat.format(new Date()));

    msg.append(" ").append(message).append(" by ");

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        msg.append("anonymous");
    } else {
        msg.append(authentication.getName());
        if (authentication.getDetails() instanceof WebAuthenticationDetails) {
            WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
            msg.append(" coming from " + details.getRemoteAddress());
        }
    }
    auditLogger.info(msg.toString());
}

From source file:it.geosolutions.geobatch.ui.security.CurrentUser.java

public Collection<GrantedAuthority> getGrantedAuthorities() {
    return SecurityContextHolder.getContext().getAuthentication().getAuthorities();
}

From source file:ua.com.codefire.dao.ContactDAOImpl.java

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

    Object obj = auth.getPrincipal();

    String UserName = ((UserDetails) obj).getUsername();

    return UserName;
}