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:eu.trentorise.smartcampus.permissionprovider.controller.AbstractController.java

/**
 * Get the user from the Spring Security Context
 * @return/*w w  w  .  j  ava  2s  .co m*/
 */
protected UserDetails getUser() {
    return (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

From source file:nz.net.orcon.kanban.controllers.HomeController.java

/**
 * Selects the home page and populates the model with a message
 *///from w ww .  jav a 2  s  . c o  m
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
    log.info("Home Sweet Home");
    log.info(springSecurityFilterChain.getClass().getName());

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        String principle = (String) auth.getPrincipal();
        log.info(principle);
    }

    model.addAttribute("message", "Hello");
    return "home";
}

From source file:org.ff4j.security.SpringSecurityAuthorisationManager.java

/** {@inheritDoc} */
public String getCurrentUserName() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        return auth.getName();
    }// ww w . j a  va2s.  c o m
    return "anonymous";
}

From source file:com.tasktop.c2c.server.profile.tests.service.WebDavServiceClientTest.java

@Before
public void setup() {
    client.setRestTemplate(restTemplate);

    SecurityContextHolder.getContext()
            .setAuthentication(new UsernamePasswordAuthenticationToken("jdoe", "Welcome1"));
    client.setBaseUrl("http://localhost:8081/s/test/maven/");
}

From source file:com.epam.cme.storefront.interceptors.beforecontroller.SecurityUserCheckBeforeControllerHandler.java

@Override
public boolean beforeController(final HttpServletRequest request, final HttpServletResponse response)
        throws IOException {
    // Skip this security check when run from within the WCMS Cockpit
    if (isPreviewDataModelValid(request)) {
        return true;
    }//ww w.  j  av a 2  s  . c om
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        final Object principal = authentication.getPrincipal();
        if (principal instanceof String) {
            final String springSecurityUserId = (String) principal;

            final String hybrisUserId = userService.getCurrentUser().getUid();
            if (!springSecurityUserId.equals(hybrisUserId)) {
                LOG.error("User miss-match springSecurityUserId [" + springSecurityUserId
                        + "] hybris session user [" + hybrisUserId + "]. Invalidating session.");

                // Invalidate session and redirect to the root page
                request.getSession().invalidate();

                final String encodedRedirectUrl = response.encodeRedirectURL(request.getContextPath() + "/");
                response.sendRedirect(encodedRedirectUrl);
                return false;
            }
        }
    }
    return true;
}

From source file:org.kamranzafar.xmpp.template.rest.XmppConfigResource.java

@RequestMapping(method = RequestMethod.GET)
@ResponseBody//from   ww w  .  ja v a  2s .com
public Map<String, Object> config() {
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(PREBIND_URL, xmppConfig.getPrebindUrl());
    config.put(BIND_URL,
            "http://" + xmppConfig.getHost() + ":" + xmppConfig.getPort() + "/" + xmppConfig.getHttpBind());
    config.put(JID,
            ((XmppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getJid());

    return config;
}

From source file:com.nec.harvest.servlet.listener.HarvestSessionListener.java

@Override
public void sessionDestroyed(HttpSessionEvent event) {
    activeSessions--;/*from w  ww.j  ava2s  .  co m*/

    // ?
    HttpSession session = event.getSession();
    Assert.notNull(session, "No HttpSession Specified");

    ServletContext ctx = session.getServletContext();
    ctx.removeAttribute(Constants.USER_LOGGED_IN_LASTTIME);
    ctx.removeAttribute(Constants.SESS_ORGANIZATION_CODE);
    ctx.removeAttribute(Constants.SESS_BUSINESS_DAY);

    if (SecurityContextHolder.getContext().getAuthentication() != null) {
        // Remove from LRU Cache
        AuthenticatedUserDetails.removeUserPrincipal();

        // Empty authentication
        SecurityContextHolder.getContext().setAuthentication(null);
    }

    // ??????
    logger.info("A HttpSession [{}] is going to be destroyed", session.getId());
}

From source file:com.ttech.cordovabuild.web.UserResource.java

@GET
public User currentUser() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    return userRepository.findUserByUserName(username);
}

From source file:org.shredzone.cilla.ws.cxf.SecurityContextCallback.java

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback callback : callbacks) {
        WSPasswordCallback pc = (WSPasswordCallback) callback;
        if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null && auth.getPrincipal() != null
                    && auth.getPrincipal() instanceof RemoteUserDetails) {
                RemoteUserDetails rud = (RemoteUserDetails) auth.getPrincipal();
                pc.setIdentifier(rud.getUsername());
                pc.setPassword(rud.getPassword());
                log.debug("Sent credentials for user {}", rud.getUsername());
            } else {
                log.error("No useable authentication found");
            }/*w w w.j  a va 2 s  .  c  o m*/
        }
    }
}

From source file:no.dusken.momus.authentication.UserLoginServiceImpl.java

@Override
public boolean login(LdapUserPwd token) {
    Authentication authentication = authenticationManager.authenticate(new Token(token));
    boolean isAuthenticated = isAuthenticated(authentication);
    if (isAuthenticated) {
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }/*from  www. j  a  v  a  2  s  .c o m*/

    return isAuthenticated;
}