org.springframework.security.core.Authentication.getDetails()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(137)

本文整理了Java中org.springframework.security.core.Authentication.getDetails()方法的一些代码示例,展示了Authentication.getDetails()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Authentication.getDetails()方法的具体详情如下:
包路径:org.springframework.security.core.Authentication
类名称:Authentication
方法名:getDetails

Authentication.getDetails介绍

[英]Stores additional details about the authentication request. These might be an IP address, certificate serial number etc.
[中]存储有关身份验证请求的其他详细信息。这些可能是IP地址、证书序列号等。

代码示例

代码示例来源:origin: spring-projects/spring-security

/**
 * Copies the authentication details from a source Authentication object to a
 * destination one, provided the latter does not already have one set.
 *
 * @param source source authentication
 * @param dest the destination authentication object
 */
private void copyDetails(Authentication source, Authentication dest) {
  if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {
    AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;
    token.setDetails(source.getDetails());
  }
}

代码示例来源:origin: cloudfoundry/uaa

protected UaaLoginHint extractLoginHint(Authentication authentication) {
  UaaLoginHint loginHint = null;
  if (authentication != null && authentication.getDetails() instanceof UaaAuthenticationDetails) {
    UaaAuthenticationDetails uaaAuthenticationDetails = (UaaAuthenticationDetails) authentication.getDetails();
    loginHint = uaaAuthenticationDetails.getLoginHint();
  }
  return loginHint;
}

代码示例来源:origin: cloudfoundry/uaa

protected boolean setLoginHint(Authentication authentication, UaaLoginHint loginHint) {
  if (authentication != null && authentication.getDetails() instanceof UaaAuthenticationDetails) {
    UaaAuthenticationDetails uaaAuthenticationDetails = (UaaAuthenticationDetails) authentication.getDetails();
    uaaAuthenticationDetails.setLoginHint(loginHint);
    return true;
  }
  return false;
}

代码示例来源:origin: org.springframework.security/spring-security-core

/**
 * Copies the authentication details from a source Authentication object to a
 * destination one, provided the latter does not already have one set.
 *
 * @param source source authentication
 * @param dest the destination authentication object
 */
private void copyDetails(Authentication source, Authentication dest) {
  if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {
    AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;
    token.setDetails(source.getDetails());
  }
}

代码示例来源:origin: spring-projects/spring-security-oauth

public static boolean isOAuthConsumerAuth(SecurityExpressionRoot root) {
  Authentication authentication = root.getAuthentication();
  if (authentication.getDetails() instanceof OAuthAuthenticationDetails) {
    return true;
  }
  return false;
}

代码示例来源:origin: apache/kylin

@RequestMapping(value = "/authentication", method = RequestMethod.GET, produces = { "application/json" })
  public UserDetails authenticatedUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication == null) {
      logger.debug("authentication is null.");
      return null;
    }

    if (authentication.getPrincipal() instanceof UserDetails) {
      return (UserDetails) authentication.getPrincipal();
    }

    if (authentication.getDetails() instanceof UserDetails) {
      return (UserDetails) authentication.getDetails();
    }

    return null;
  }
}

代码示例来源:origin: cloudfoundry/uaa

UaaAuthenticationDetails getAuthenticationDetails() {
  return (UaaAuthenticationDetails) getAuthentication().getDetails();
}

代码示例来源:origin: geoserver/geoserver

/** A proper {@link GeoServerWebAuthenticationDetails} object must be present */
protected String retrieveUserName(Authentication authentication) {
  if (authentication.getDetails() instanceof GeoServerWebAuthenticationDetails) {
    String userGroupServiceName =
        ((GeoServerWebAuthenticationDetails) authentication.getDetails())
            .getUserGroupServiceName();
    if (userGroupServiceName == null || userGroupServiceName.trim().length() == 0)
      return ""; // no service specified --> no remember me
    return encode(super.retrieveUserName(authentication), userGroupServiceName);
  } else return ""; // no remember me feature without a user group service name
};

代码示例来源:origin: spring-projects/spring-security

protected Authentication createNewAuthentication(Authentication currentAuth,
    String newPassword) {
  UserDetails user = loadUserByUsername(currentAuth.getName());
  UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
      user, null, user.getAuthorities());
  newAuthentication.setDetails(currentAuth.getDetails());
  return newAuthentication;
}

代码示例来源:origin: cloudfoundry/uaa

public String getAuthenticationZoneId() {
  if (authentication.getPrincipal() instanceof UaaPrincipal) {
    return ((UaaPrincipal) authentication.getPrincipal()).getZoneId();
  } else if (authentication instanceof UaaOauth2Authentication) {
    return ((UaaOauth2Authentication)authentication).getZoneId();
  } else if (authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
    String tokenValue = ((OAuth2AuthenticationDetails)authentication.getDetails()).getTokenValue();
    return getZoneIdFromToken(tokenValue);
  } else {
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-security-oauth

public static boolean consumerHasAnyRole(SecurityExpressionRoot root, String... roles) {
  Authentication authentication = root.getAuthentication();
  if (authentication.getDetails() instanceof OAuthAuthenticationDetails) {
    OAuthAuthenticationDetails details = (OAuthAuthenticationDetails) authentication.getDetails();
    List<GrantedAuthority> consumerAuthorities = details.getConsumerDetails().getAuthorities();
    if (consumerAuthorities != null) {
      Set<String> roleSet = AuthorityUtils.authorityListToSet(consumerAuthorities);
      for (String role : roles) {
        if (roleSet.contains(role)) {
          return true;
        }
      }
    }
  }
  return false;
}

代码示例来源:origin: spring-projects/spring-security

public void onApplicationEvent(AbstractAuthenticationEvent event) {
  if (!logInteractiveAuthenticationSuccessEvents
      && event instanceof InteractiveAuthenticationSuccessEvent) {
    return;
  }
  if (logger.isWarnEnabled()) {
    final StringBuilder builder = new StringBuilder();
    builder.append("Authentication event ");
    builder.append(ClassUtils.getShortName(event.getClass()));
    builder.append(": ");
    builder.append(event.getAuthentication().getName());
    builder.append("; details: ");
    builder.append(event.getAuthentication().getDetails());
    if (event instanceof AbstractAuthenticationFailureEvent) {
      builder.append("; exception: ");
      builder.append(((AbstractAuthenticationFailureEvent) event)
          .getException().getMessage());
    }
    logger.warn(builder.toString());
  }
}

代码示例来源:origin: org.springframework.security/spring-security-core

protected Authentication createNewAuthentication(Authentication currentAuth,
    String newPassword) {
  UserDetails user = loadUserByUsername(currentAuth.getName());
  UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(
      user, null, user.getAuthorities());
  newAuthentication.setDetails(currentAuth.getDetails());
  return newAuthentication;
}

代码示例来源:origin: spring-projects/spring-security

@GetMapping("/details")
  public String details(@AuthenticationPrincipal Authentication authentication) {
    return authentication.getDetails().getClass().getName();
  }
}

代码示例来源:origin: cloudfoundry/uaa

@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
  AuthenticationFailureBadCredentialsEvent bce = event;
  String principal = bce.getAuthentication().getName();
  UaaAuthenticationDetails details = (UaaAuthenticationDetails) bce.getAuthentication().getDetails();
  if (bce.getException() instanceof UsernameNotFoundException) {
    publisher.publishEvent(new PrincipalNotFoundEvent(principal, details));
  }
  else {
    publisher.publishEvent(new PrincipalAuthenticationFailureEvent(principal, details));
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void changePasswordSucceedsWithIfReAuthenticationSucceeds() {
  insertJoe();
  Authentication currentAuth = authenticateJoe();
  AuthenticationManager am = mock(AuthenticationManager.class);
  when(am.authenticate(currentAuth)).thenReturn(currentAuth);
  manager.setAuthenticationManager(am);
  manager.changePassword("password", "newPassword");
  UserDetails newJoe = manager.loadUserByUsername("joe");
  assertThat(newJoe.getPassword()).isEqualTo("newPassword");
  // The password in the context should also be altered
  Authentication newAuth = SecurityContextHolder.getContext().getAuthentication();
  assertThat(newAuth.getName()).isEqualTo("joe");
  assertThat(newAuth.getDetails()).isEqualTo(currentAuth.getDetails());
  assertThat(newAuth.getCredentials()).isNull();
  assertThat(cache.getUserMap().containsKey("joe")).isFalse();
}

代码示例来源:origin: cloudfoundry/uaa

@Test(expected = BadCredentialsException.class)
public void testNoUsernameNoEmail() throws Exception {
  UaaAuthenticationDetails uaaAuthenticationDetails = mock(UaaAuthenticationDetails.class);
  when(uaaAuthenticationDetails.getOrigin()).thenReturn(origin);
  when(uaaAuthenticationDetails.getClientId()).thenReturn(null);
  when(uaaAuthenticationDetails.getSessionId()).thenReturn(new RandomValueStringGenerator().generate());
  when(inputAuth.getDetails()).thenReturn(uaaAuthenticationDetails);
  when(uaaUserDatabase.retrieveUserByName(anyString(), eq(origin))).thenReturn(null);
  when(userDetails.getUsername()).thenReturn(null);
  manager.authenticate(inputAuth);
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider()
    throws Exception {
  Object details = new Object();
  ProviderManager authMgr = makeProviderManager();
  TestingAuthenticationToken request = createAuthenticationToken();
  request.setDetails(details);
  Authentication result = authMgr.authenticate(request);
  assertThat(result.getCredentials()).isNotNull();
  assertThat(result.getDetails()).isSameAs(details);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testExtractLoginHint() {
  DynamicZoneAwareAuthenticationManager manager = getDynamicZoneAwareAuthenticationManager(true);
  UaaAuthenticationDetails mockDetails = mock(UaaAuthenticationDetails.class);
  UaaLoginHint loginHint = mock(UaaLoginHint.class);
  when(loginHint.getOrigin()).thenReturn("uaa");
  when(success.getDetails()).thenReturn(mockDetails);
  assertNull(manager.extractLoginHint(null));
  assertNull(manager.extractLoginHint(success));
  when(mockDetails.getLoginHint()).thenReturn(loginHint);
  assertEquals(loginHint, manager.extractLoginHint(success));
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider()
    throws Exception {
  Object requestDetails = "(Request Details)";
  final Object resultDetails = "(Result Details)";
  // A provider which sets the details object
  AuthenticationProvider provider = new AuthenticationProvider() {
    public Authentication authenticate(Authentication authentication)
        throws AuthenticationException {
      ((TestingAuthenticationToken) authentication).setDetails(resultDetails);
      return authentication;
    }
    public boolean supports(Class<?> authentication) {
      return true;
    }
  };
  ProviderManager authMgr = new ProviderManager(Arrays.asList(provider));
  TestingAuthenticationToken request = createAuthenticationToken();
  request.setDetails(requestDetails);
  Authentication result = authMgr.authenticate(request);
  assertThat(result.getDetails()).isEqualTo(resultDetails);
}

相关文章