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

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

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

Authentication.getCredentials介绍

[英]The credentials that prove the principal is correct. This is usually a password, but could be anything relevant to the AuthenticationManager. Callers are expected to populate the credentials.
[中]证明委托人正确的凭证。这通常是一个密码,但可能是与AuthenticationManager相关的任何内容。呼叫者需要填充凭据。

代码示例

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

/**
   * @see org.springframework.ldap.core.AuthenticationSource#getCredentials()
   */
  public String getCredentials() {
    Authentication authentication = SecurityContextHolder.getContext()
        .getAuthentication();

    if (authentication == null) {
      log.warn("No Authentication object set in SecurityContext - returning empty String as Credentials");
      return "";
    }

    return (String) authentication.getCredentials();
  }
}

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

public Authentication authenticate(Authentication authentication)
    throws AuthenticationException {
  String username = authentication.getPrincipal().toString();
  Object credentials = authentication.getCredentials();
  String password = credentials == null ? null : credentials.toString();
  Collection<? extends GrantedAuthority> authorities = remoteAuthenticationManager
      .attemptAuthentication(username, password);
  return new UsernamePasswordAuthenticationToken(username, password, authorities);
}

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

public Authentication authenticate(Authentication authentication)
    throws AuthenticationException {
  String username = authentication.getPrincipal().toString();
  Object credentials = authentication.getCredentials();
  String password = credentials == null ? null : credentials.toString();
  Collection<? extends GrantedAuthority> authorities = remoteAuthenticationManager
      .attemptAuthentication(username, password);
  return new UsernamePasswordAuthenticationToken(username, password, authorities);
}

代码示例来源:origin: alibaba/nacos

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  String username = (String) authentication.getPrincipal();
  String password = (String) authentication.getCredentials();
  UserDetails userDetails = userDetailsService.loadUserByUsername(username);
  if (!password.equals(userDetails.getPassword())) {
    return new UsernamePasswordAuthenticationToken(username, null, null);
  }
  return null;
}

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

@GetMapping("/password")
public String password(@AuthenticationPrincipal Authentication authentication) {
  return (String) authentication.getCredentials();
}

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

private CasAuthenticationToken authenticateNow(final Authentication authentication)
    throws AuthenticationException {
  try {
    final Assertion assertion = this.ticketValidator.validate(authentication
        .getCredentials().toString(), getServiceUrl(authentication));
    final UserDetails userDetails = loadUserByAssertion(assertion);
    userDetailsChecker.check(userDetails);
    return new CasAuthenticationToken(this.key, userDetails,
        authentication.getCredentials(),
        authoritiesMapper.mapAuthorities(userDetails.getAuthorities()),
        userDetails, assertion);
  }
  catch (final TicketValidationException e) {
    throw new BadCredentialsException(e.getMessage(), e);
  }
}

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

@Override
protected Authentication createSuccessAuthentication(Object principal,
    Authentication authentication, UserDetails user) {
  boolean upgradeEncoding = this.userDetailsPasswordService != null
      && this.passwordEncoder.upgradeEncoding(user.getPassword());
  if (upgradeEncoding) {
    String presentedPassword = authentication.getCredentials().toString();
    String newPassword = this.passwordEncoder.encode(presentedPassword);
    user = this.userDetailsPasswordService.updatePassword(user, newPassword);
  }
  return super.createSuccessAuthentication(principal, authentication, user);
}

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

@Test
  public void securityContextDeserializeTest() throws IOException {
    SecurityContext context = mapper.readValue(SECURITY_CONTEXT_JSON, SecurityContextImpl.class);
    assertThat(context).isNotNull();
    assertThat(context.getAuthentication()).isNotNull().isInstanceOf(UsernamePasswordAuthenticationToken.class);
    assertThat(context.getAuthentication().getPrincipal()).isEqualTo("admin");
    assertThat(context.getAuthentication().getCredentials()).isEqualTo("1234");
    assertThat(context.getAuthentication().isAuthenticated()).isTrue();
    Collection authorities = context.getAuthentication().getAuthorities();
    assertThat(authorities).hasSize(1);
    assertThat(authorities).contains(new SimpleGrantedAuthority("ROLE_USER"));
  }
}

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

@Test
public void authenticateSuccess() throws Exception {
  Authentication auth = provider.authenticate(token);
  assertThat(auth.getPrincipal()).isEqualTo(token.getPrincipal());
  assertThat(auth.getCredentials()).isEqualTo(token.getCredentials());
  assertThat(auth.isAuthenticated()).isEqualTo(true);
  assertThat(auth.getAuthorities().isEmpty()).isEqualTo(false);
  verify(publisher).publishEvent(isA(JaasAuthenticationSuccessEvent.class));
  verifyNoMoreInteractions(publisher);
}

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

@Override
protected Authentication createSuccessAuthentication(Object principal,
    Authentication authentication, UserDetails user) {
  boolean upgradeEncoding = this.userDetailsPasswordService != null
      && this.passwordEncoder.upgradeEncoding(user.getPassword());
  if (upgradeEncoding) {
    String presentedPassword = authentication.getCredentials().toString();
    String newPassword = this.passwordEncoder.encode(presentedPassword);
    user = this.userDetailsPasswordService.updatePassword(user, newPassword);
  }
  return super.createSuccessAuthentication(principal, authentication, user);
}

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

public Authentication authenticate(Authentication authentication) {
    return new TestingAuthenticationToken(authentication.getPrincipal(),
        authentication.getCredentials(),
        AuthorityUtils.createAuthorityList("ROLE_USER"));
  }
}

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

@Test
public void credentialsAreClearedByDefault() throws Exception {
  UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
      "Test", "Password");
  ProviderManager mgr = makeProviderManager();
  Authentication result = mgr.authenticate(token);
  assertThat(result.getCredentials()).isNull();
  mgr.setEraseCredentialsAfterAuthentication(false);
  token = new UsernamePasswordAuthenticationToken("Test", "Password");
  result = mgr.authenticate(token);
  assertThat(result.getCredentials()).isNotNull();
}

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

public Authentication authenticate(Authentication authentication)
      throws AuthenticationException {
    Object principal = authentication.getPrincipal();
    String username = String.valueOf(principal);
    User user = myUserRepository.findByUsername(username);
    if (user == null) {
      throw new UsernameNotFoundException("No user for principal "
          + principal);
    }
    if (!authentication.getCredentials().equals(user.getPassword())) {
      throw new BadCredentialsException("Invalid password");
    }
    return new TestingAuthenticationToken(principal, null, "ROLE_USER");
  }
};

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

@Test
public void testSuccessfulAuthenticationCreatesObject() {
  RemoteAuthenticationProvider provider = new RemoteAuthenticationProvider();
  provider.setRemoteAuthenticationManager(
      new MockRemoteAuthenticationManager(true));
  Authentication result = provider
      .authenticate(new UsernamePasswordAuthenticationToken("rod", "password"));
  assertThat(result.getPrincipal()).isEqualTo("rod");
  assertThat(result.getCredentials()).isEqualTo("password");
  assertThat(AuthorityUtils.authorityListToSet(result.getAuthorities())).contains("foo");
}

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

@Test
public void createNewAuthenticationUsesNullPasswordToKeepPassordsSave() {
  insertJoe();
  UsernamePasswordAuthenticationToken currentAuth = new UsernamePasswordAuthenticationToken(
      "joe", null, AuthorityUtils.createAuthorityList("ROLE_USER"));
  Authentication updatedAuth = manager.createNewAuthentication(currentAuth, "new");
  assertThat(updatedAuth.getCredentials()).isNull();
}

代码示例来源: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: spring-projects/spring-security

@Test
public void authenticationMangerWhenEraseCredentialsIsFalseThenCredentialsNotNull() throws Exception {
  this.spring.register(EraseCredentialsFalseConfig.class).autowire();
  this.mockMvc.perform(formLogin())
    .andExpect(authenticated().withAuthentication(a-> assertThat(a.getCredentials()).isNotNull()));
  this.mockMvc.perform(formLogin())
    .andExpect(authenticated().withAuthentication(a-> assertThat(a.getCredentials()).isNotNull()));
  // no exception due to username being cleared out
}

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

@Test
public void passwordIsSetFromUserDataIfUseAuthenticationRequestCredentialsIsFalse() {
  LdapAuthenticationProvider ldapProvider = new LdapAuthenticationProvider(
      new MockAuthenticator(), new MockAuthoritiesPopulator());
  ldapProvider.setUseAuthenticationRequestCredentials(false);
  UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
      "ben", "benspassword");
  Authentication authResult = ldapProvider.authenticate(authRequest);
  assertThat(authResult.getCredentials()).isEqualTo("{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
}

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

@Test
// SEC-2533
public void authenticationManagerWhenGlobalAndEraseCredentialsIsFalseThenCredentialsNotNull() throws Exception {
  this.spring.register(GlobalEraseCredentialsFalseConfig.class).autowire();
  this.mockMvc.perform(formLogin())
    .andExpect(authenticated().withAuthentication(a-> assertThat(a.getCredentials()).isNotNull()));
}

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

@Test
public void authenticationMangerWhenDefaultThenEraseCredentialsIsTrue() throws Exception {
  this.spring.register(EraseCredentialsTrueDefaultConfig.class).autowire();
  this.mockMvc.perform(formLogin())
    .andExpect(authenticated().withAuthentication(a-> assertThat(a.getCredentials()).isNull()));
  this.mockMvc.perform(formLogin())
    .andExpect(authenticated().withAuthentication(a-> assertThat(a.getCredentials()).isNull()));
  // no exception due to username being cleared out
}

相关文章