Example usage for org.springframework.security.core Authentication getClass

List of usage examples for org.springframework.security.core Authentication getClass

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.foilen.smalltools.tools.TombstoneTools.java

/**
 * Add a warning log entry telling which user in Spring Security is using it.
 *
 * @param id/*from w  w w .  j av a2 s .c o  m*/
 *            the id to output in the log (should be unique to know which one was used)
 * @param time
 *            the time when you added that entry (could be a date or the version number)
 */
public static void logWithUser(String id, String time) {

    SecurityContext securityContext = null;
    try {
        securityContext = SecurityContextHolder.getContext();
    } catch (Exception e) {
    }

    String user = null;
    Class<? extends Authentication> authClass = null;
    if (securityContext != null) {
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            authClass = authentication.getClass();
            user = authentication.getName();
        }
    }

    logger.warn("{} - AuthClass: {} - User: {}", id, authClass, user);
}

From source file:net.cristcost.study.services.ServiceTestUtil.java

private static void dumpSecurityInformation(PrintWriter writer, AuthenticationManager authenticationManager) {
    writer.println("### General Security Information ###");

    writer.println("Security Strategy is " + SecurityContextHolder.getContextHolderStrategy().toString());

    writer.println("Current Thread is " + Thread.currentThread().getName() + " ("
            + Thread.currentThread().getId() + ")");

    writer.println();/*from  w ww  .  j  ava 2 s.  c o  m*/

    if (authenticationManager != null) {
        writer.println("I've been injected with the AuthenticationManager");
    } else {
        writer.println("I've not been injected with the AuthenticationManager");
    }
    writer.println();

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        writer.println("There is an Authentication of type: " + authentication.getClass().getName());
        writer.println("Principal is of type: " + authentication.getPrincipal().getClass().getName()
                + " and is value is: " + authentication.getPrincipal().toString());
        for (GrantedAuthority ga : authentication.getAuthorities()) {
            writer.println(" - you have " + ga.getAuthority() + " authority");
        }

    } else {
        writer.println("There is no Authentication!");
    }
    writer.println();
}

From source file:io.syndesis.core.Tokens.java

public static KeycloakSecurityContext getKeycloakSecurityContext() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new IllegalStateException(
                "Cannot set authorization header because there is no authenticated principal");
    }//from  w  w w  .j  a  va  2 s  .c  o  m
    if (!KeycloakAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
        throw new IllegalStateException(String.format(
                "Cannot set authorization header because Authentication is of type %s but %s is required",
                authentication.getClass(), KeycloakAuthenticationToken.class));
    }

    KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) authentication;
    return token.getAccount().getKeycloakSecurityContext();
}

From source file:grails.plugin.springsecurity.authentication.GrailsAnonymousAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    return supports(authentication.getClass()) ? authentication : null;
}

From source file:info.raack.appliancelabeler.security.OAuthAutomaticAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }//www  .j ava2 s.  c  o m

    // any OAuthAutomaticAuthenticationTokens which pass through here are automatically authenticated

    return authentication;
}

From source file:org.glassmaker.spring.oauth.OAuth2AuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        return null;
    }/*from   w w w.  j a v  a  2  s.  c o  m*/
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        UserDetails userDetails = userDetailsService.loadUserByUsername((String) token.getPrincipal());
        UsernamePasswordAuthenticationToken newToken = new UsernamePasswordAuthenticationToken(
                token.getPrincipal(), token.getCredentials(), userDetails.getAuthorities());
        newToken.setDetails(token.getDetails());
        return newToken;
    }
    return null;
}

From source file:com.googlecode.janrain4j.springframework.security.JanrainAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (supports(authentication.getClass())) {

        JanrainAuthenticationToken janrainAuthenticationToken = (JanrainAuthenticationToken) authentication;
        UserDataResponse userDataResponse = janrainAuthenticationToken.getUserDataResponse();

        UserDetails userDetails = null;//from   w ww  . j a  v  a2 s. c o  m

        if (authenticationUserDetailsService == null) {
            userDetails = new JanrainUserDetails(userDataResponse);
        } else {
            userDetails = authenticationUserDetailsService.loadUserDetails(janrainAuthenticationToken);
        }

        return new JanrainAuthenticationToken(userDetails,
                userDetails == null ? null : userDetails.getAuthorities(), userDataResponse);
    }

    return null;
}

From source file:com.wwpass.springsecurity.authentication.WwpassAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    if (!supports(authentication.getClass())) {
        return null;
    }// w  ww  .ja  v a2s. c  o  m

    if (authentication instanceof WwpassAuthenticationToken) {
        WwpassAuthenticationToken token = (WwpassAuthenticationToken) authentication;

        String puid = token.getPuid();

        UserDetails user = null;
        if (puid != null) {
            user = userDetailsService.loadUserDetails(token);
        }
        if (user == null) {
            throw new UsernameNotFoundException("PUID not found.");
        }
        Collection<? extends GrantedAuthority> authorities = AuthorityUtils
                .commaSeparatedStringToAuthorityList(getWwpassUserRole());

        return new WwpassAuthenticationToken(user, puid, authorities);
    }

    return null;
}

From source file:de.thm.arsnova.controller.LoginControllerTest.java

@Test
public void testReuseGuestLogin() throws Exception {
    mockMvc.perform(get("/doLogin").param("type", "guest").param("user", "Guest1234567890"))
            .andExpect(status().isOk());

    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
    assertEquals("Guest1234567890", auth.getName());

}

From source file:edu.utah.further.security.impl.authentication.PreAuthenticatedFederatedAuthenticationProviderImpl.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        throw new ApplicationException(
                "Unsupported authentication class " + authentication.getClass().getCanonicalName());
    }/*from w ww .  j  a v  a  2 s . co m*/
    final UserDetails userDetails = caUserDetailsService.loadUserDetails(authentication);
    final FederatedAuthenticationToken<UserDetails> authenticatedToken = new FederatedAuthenticationToken<>();
    authenticatedToken.addAllGrantedAuthorities(userDetails.getAuthorities());
    authenticatedToken.setPrincipal(userDetails);
    authenticatedToken.setName(userDetails.getUsername());
    return authenticatedToken;
}