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:cz.fi.muni.pa165.mushroomhunter.service.SecurityServiceImpl.java

public HunterDto getCurrentlyLoggedUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username
    return hunterService.findByNick(name); //get hunter by login name
}

From source file:tds.tdsadmin.web.backingbean.UserBean.java

public UserBean() {
    user = (SbacUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    _logger.info(String.format("Logged in user is:%s", user.getEmail()));
}

From source file:org.chtijbug.drools.platform.web.UserResource.java

@RequestMapping(value = "/username", produces = TEXT_PLAIN_VALUE)
@ResponseBody// w  w w.  j a v a2s .  co  m
public String username() {
    return SecurityContextHolder.getContext().getAuthentication().getName();
}

From source file:psiprobe.controllers.apps.StopContextController.java

@Override
protected void executeAction(String contextName) throws Exception {
    getContainerWrapper().getTomcatContainer().stop(contextName);

    // Logging action
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); // get username logger
    logger.info(getMessageSourceAccessor().getMessage("probe.src.log.stop"), name, contextName);
}

From source file:com.vdenotaris.spring.boot.security.saml.web.controllers.LandingController.java

@RequestMapping("/landing")
public String landing(Model model) {
    JWTAuthenticatedUser user = (JWTAuthenticatedUser) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();/*from  w  w w.ja  va  2s . c o m*/
    final String loggedInUserName = user.getUsername();
    model.addAttribute("username", loggedInUserName);
    return "landing";
}

From source file:org.runway.utils.AuthenticationUtils.java

public static void autoLogin(User user, HttpServletRequest request,
        AuthenticationManager authenticationManager) {

    //           GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
    //             user.getAuthority()) };

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
            user.getPassword(), user.getAuthorities());

    // generate session if one doesn't exist
    HttpSession session = request.getSession();

    token.setDetails(new WebAuthenticationDetails(request));
    Authentication authenticatedUser = authenticationManager.authenticate(token);

    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    // setting role to the session
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());

}

From source file:psiprobe.controllers.apps.StartContextController.java

@Override
protected void executeAction(String contextName) throws Exception {
    getContainerWrapper().getTomcatContainer().start(contextName);

    // Logging action
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); // get username logger
    logger.info(getMessageSourceAccessor().getMessage("probe.src.log.start"), name, contextName);
}

From source file:net.maritimecloud.identityregistry.utils.AccessControlUtil.java

public static boolean hasAccessToOrg(String orgMrn) {
    if (orgMrn == null || orgMrn.trim().isEmpty()) {
        log.debug("The orgMrn was empty!");
        return false;
    }//from w ww . j a  v  a 2  s. c o  m
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    // First check if the user is a SITE_ADMIN, in which case he gets access.
    for (GrantedAuthority authority : auth.getAuthorities()) {
        String role = authority.getAuthority();
        log.debug("User has role: " + role);
        if ("ROLE_SITE_ADMIN".equals(role)) {
            return true;
        }
    }
    log.debug("User not a SITE_ADMIN");
    // Check if the user is part of the organization
    if (auth instanceof KeycloakAuthenticationToken) {
        log.debug("OIDC authentication in process");
        // Keycloak authentication
        KeycloakAuthenticationToken kat = (KeycloakAuthenticationToken) auth;
        KeycloakSecurityContext ksc = (KeycloakSecurityContext) kat.getCredentials();
        Map<String, Object> otherClaims = ksc.getToken().getOtherClaims();
        if (otherClaims.containsKey(AccessControlUtil.ORG_PROPERTY_NAME)
                && ((String) otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME)).toLowerCase()
                        .equals(orgMrn.toLowerCase())) {
            log.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is in "
                    + orgMrn);
            return true;
        }
        log.debug("Entity from org: " + otherClaims.get(AccessControlUtil.ORG_PROPERTY_NAME) + " is not in "
                + orgMrn);
    } else if (auth instanceof PreAuthenticatedAuthenticationToken) {
        log.debug("Certificate authentication in process");
        // Certificate authentication
        PreAuthenticatedAuthenticationToken token = (PreAuthenticatedAuthenticationToken) auth;
        // Check that the Organization name of the accessed organization and the organization in the certificate is equal
        InetOrgPerson person = ((InetOrgPerson) token.getPrincipal());
        // The O(rganization) value in the certificate is an MRN
        String certOrgMrn = person.getO();
        if (orgMrn.equals(certOrgMrn)) {
            log.debug("Entity with O=" + certOrgMrn + " is in " + orgMrn);
            return true;
        }
        log.debug("Entity with O=" + certOrgMrn + " is not in " + orgMrn);
    } else {
        log.debug("Unknown authentication method: " + auth.getClass());
    }
    return false;
}

From source file:com.github.javarch.persistence.orm.hibernate.envers.CustomRevisionEntityInforListener.java

public void newRevision(Object revisionEntity) {
    CustomRevisionEntityInfo revision = (CustomRevisionEntityInfo) revisionEntity;

    Object princial = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (princial instanceof UserDetails) {
        revision.setUsername(((UserDetails) princial).getUsername());
    } else {/*www  .j a  v a2 s.com*/
        revision.setUsername(princial.toString()); // anonimo.
    }

}

From source file:com.pluralsight.controller.test.AbstractSecurityTest.java

protected void login(String customUsername) {
    UserDetails ud = userDetailsService.loadUserByUsername(customUsername);
    Authentication auth = new UsernamePasswordAuthenticationToken(ud, ud.getPassword(), ud.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
}