Example usage for org.springframework.security.core.context SecurityContext getAuthentication

List of usage examples for org.springframework.security.core.context SecurityContext getAuthentication

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContext getAuthentication.

Prototype

Authentication getAuthentication();

Source Link

Document

Obtains the currently authenticated principal, or an authentication request token.

Usage

From source file:org.vaadin.spring.security.config.VaadinSecurityConfiguration.java

@Bean
Authentication currentUser() {//from www .  j  a  va 2s  .  co m
    return ProxyFactory.getProxy(Authentication.class, new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            Authentication authentication = securityContext.getAuthentication();
            if (authentication == null) {
                throw new AuthenticationCredentialsNotFoundException(
                        "No authentication found in current security context");
            }
            return invocation.getMethod().invoke(authentication, invocation.getArguments());
        }
    });
}

From source file:org.zkybase.kite.guard.RateLimitingThrottleTemplate.java

private Object getPrincipal() {
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication auth = context.getAuthentication();

    // FIXME There's probably a better way to detect anonymous auth.
    if (auth == null || auth instanceof AnonymousAuthenticationToken) {
        log.debug("Authentication required");
        throw new UnauthenticatedException();
    }//ww  w.j  a  va2s. c om

    return auth.getPrincipal();
}

From source file:it.scoppelletti.programmerpower.security.DefaultUserManager.java

@Transactional(readOnly = true)
public User loadLoggedUser() {
    User principal;/*from   ww w .ja  v a 2 s.c om*/
    Authentication auth;
    SecurityContext secCtx = SecurityContextHolder.getContext();

    auth = secCtx.getAuthentication();
    if (auth == null || !auth.isAuthenticated() || auth instanceof AnonymousAuthenticationToken) {
        return null;
    }

    principal = (User) auth.getPrincipal();
    return loadUser(principal.getId());
}

From source file:org.geonode.security.GeoNodeAnonymousProcessingFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    final SecurityContext securityContext = SecurityContextHolder.getContext();
    final Authentication existingAuth = securityContext.getAuthentication();

    final boolean authenticationRequired = existingAuth == null || !existingAuth.isAuthenticated();

    if (authenticationRequired) {
        try {/* w w  w .j  a  v  a  2s  .  c o m*/
            Object principal = existingAuth == null ? null : existingAuth.getPrincipal();
            Collection<? extends GrantedAuthority> authorities = existingAuth == null ? null
                    : existingAuth.getAuthorities();
            Authentication authRequest = new AnonymousGeoNodeAuthenticationToken(principal, authorities);
            final Authentication authResult = getSecurityManager().authenticate(authRequest);
            securityContext.setAuthentication(authResult);
            LOGGER.finer("GeoNode Anonymous filter kicked in.");
        } catch (AuthenticationException e) {
            // we just go ahead and fall back on basic authentication
            LOGGER.log(Level.WARNING, "Error connecting to the GeoNode server for authentication purposes", e);
        }
    }

    // move forward along the chain
    chain.doFilter(request, response);
}

From source file:br.com.sescacre.controleAcesso.bean.UsuarioBean.java

public UsuarioBean() {
    usuario = new Usuarios();
    SecurityContext context = SecurityContextHolder.getContext();
    if (context instanceof SecurityContext) {
        Authentication authentication = context.getAuthentication();
        if (authentication instanceof Authentication) {
            usuario.setLogin(((User) authentication.getPrincipal()).getUsername());
        }//from  w w  w .j  a va 2 s .c  o  m
    }
}

From source file:com.emc.cto.ridagent.rid.RIDRequestHandler.java

@RequestMapping(method = RequestMethod.POST)
public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model)
        throws XProcException, IOException, URISyntaxException, TransformerException,
        NoSuchRequestHandlingMethodException {
    try {/*  w  w w.  java 2s  .c  o  m*/

        // check the white list to see if we want to talk to the counter-party
        logger.info("If we got this far, the Spring Security whitelist checking has already passed OK...");

        SecurityContext ssc = SecurityContextHolder.getContext();
        String ridPeerName = ssc.getAuthentication().getName().toString();

        if (ridPeerName != null) {
            logger.info("RID Peer Name is: " + ridPeerName);
            if (!whiteList.contains(ridPeerName)) {
                logger.info("RID Peer name not found in whitelist.");
                logger.info(
                        "Edit RIDSystem-servlet.xml if and as appropriate, in order to authorize this peer.");
                throw new NoSuchRequestHandlingMethodException(request); // results in a 404 Not Found.
            }
            logger.info("Local whitelist Checking completed.");
            logger.info("Incoming WatchList.   " + request.getInputStream().toString());
        }

        PipelineInputCache pi = new PipelineInputCache();

        // supply HTTP body as the source for the resource Create pipeline
        pi.setInputPort("source", request.getInputStream());

        PipelineOutput output = m_requestHandler.executeOn(pi);

        model.addAttribute("pipelineOutput", output);
        return "pipelineOutput";
    } finally {
        ; //TODO add finally handler
    }
}

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

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

    if (securityContext == null) {
        return null;
    }/*w  w w.  j  a v  a  2s .c om*/

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

From source file:com.orange.clara.tool.controllers.AbstractDefaultController.java

protected User getCurrentUser() {
    SecurityContext securityContext = this.getSecurityContext();
    if (securityContext == null || securityContext.getAuthentication() == null
            || securityContext.getAuthentication().getPrincipal() == null) {
        return null;
    }/*from w  w  w . j  a  va2s  .  co m*/
    Principal principal = (Principal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    return this.userRepo.findOne(principal.getName());
}

From source file:architecture.user.security.authentication.impl.DefaultAuthenticationProvider.java

public Authentication getAuthentication() {
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authen = context.getAuthentication();
    return authen;
}

From source file:edu.byu.mpn.rest.BaseController.java

private JwtUserDetails getJwtUserDetails() {
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication auth = context.getAuthentication();
        if (auth != null) {
            return (JwtUserDetails) auth.getPrincipal();
        }/*from   www .  j  av  a 2 s  .  c om*/
    }
    return null;
}