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

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

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

Authentication.setAuthenticated介绍

[英]See #isAuthenticated() for a full description.

Implementations should always allow this method to be called with a false parameter, as this is used by various classes to specify the authentication token should not be trusted. If an implementation wishes to reject an invocation with a true parameter (which would indicate the authentication token is trusted - a potential security risk) the implementation should throw an IllegalArgumentException.
[中]有关完整说明,请参见#isAuthenticated()。
实现应始终允许使用false参数调用此方法,因为各种类都使用此参数来指定不应信任的身份验证令牌。如果实现希望拒绝使用true参数的调用(这将表明身份验证令牌是可信的-存在潜在的安全风险),则该实现应抛出IllegalArgumentException。

代码示例

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

@Override
@Transactional("blTransactionManager")
public AdminUser changePassword(PasswordChange passwordChange) {
  AdminUser user = readAdminUserByUserName(passwordChange.getUsername());
  user.setUnencodedPassword(passwordChange.getNewPassword());
  user = saveAdminUser(user);
  Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(passwordChange.getUsername(), passwordChange.getNewPassword(), auth.getAuthorities());
  SecurityContextHolder.getContext().setAuthentication(authRequest);
  auth.setAuthenticated(false);
  return user;
}

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

request.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(request);

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

@Before
public void setUp() throws Exception {
  SecurityContextHolder.getContext().setAuthentication(auth);
  authzStrategy = mock(AclAuthorizationStrategy.class);
  mockAuditLogger = mock(AuditLogger.class);
  pgs = new DefaultPermissionGrantingStrategy(mockAuditLogger);
  auth.setAuthenticated(true);
}

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

@Test
public void putInCacheAclWithParent() throws Exception {
  Authentication auth = new TestingAuthenticationToken("user", "password",
      "ROLE_GENERAL");
  auth.setAuthenticated(true);
  SecurityContextHolder.getContext().setAuthentication(auth);
  ObjectIdentity identityParent = new ObjectIdentityImpl(TARGET_CLASS,
      Long.valueOf(2));
  AclAuthorizationStrategy aclAuthorizationStrategy = new AclAuthorizationStrategyImpl(
      new SimpleGrantedAuthority("ROLE_OWNERSHIP"), new SimpleGrantedAuthority(
          "ROLE_AUDITING"), new SimpleGrantedAuthority("ROLE_GENERAL"));
  MutableAcl parentAcl = new AclImpl(identityParent, Long.valueOf(2),
      aclAuthorizationStrategy, new ConsoleAuditLogger());
  acl.setParent(parentAcl);
  myCache.putInCache(acl);
  verify(cache, times(4)).put(element.capture());
  List<Element> allValues = element.getAllValues();
  assertThat(allValues.get(0).getKey()).isEqualTo(parentAcl.getObjectIdentity());
  assertThat(allValues.get(0).getObjectValue()).isEqualTo(parentAcl);
  assertThat(allValues.get(1).getKey()).isEqualTo(parentAcl.getId());
  assertThat(allValues.get(1).getObjectValue()).isEqualTo(parentAcl);
  assertThat(allValues.get(2).getKey()).isEqualTo(acl.getObjectIdentity());
  assertThat(allValues.get(2).getObjectValue()).isEqualTo(acl);
  assertThat(allValues.get(3).getKey()).isEqualTo(acl.getId());
  assertThat(allValues.get(3).getObjectValue()).isEqualTo(acl);
}

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

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  if (authentication instanceof OAuth2Authentication) {
    OAuth2Request creq = ((OAuth2Authentication) authentication).getOAuth2Request();
    List<String> scopes = dedup(creq.getScope());
    int matches = 0;
    int requiredMatches = getRequiredScopes().size();
    for (String scope : scopes) {
      if (requiredScopes.contains(scope)) {
        matches++;
      }
    }
    if (matches==requiredMatches) {
      authentication.setAuthenticated(true);
      return authentication;
    } else if (isThrowOnNotAuthenticated()) {
      throw new InsufficientScopeException("Insufficient scopes");
    }
  } else if (isThrowOnNotAuthenticated()) {
    throw new InvalidTokenException("Missing Oauth 2 authentication.");
  }
  return authentication;
}

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

@Test
@Transactional
public void cumulativePermissions() {
  Authentication auth = new TestingAuthenticationToken("ben", "ignored",
      "ROLE_ADMINISTRATOR");
  auth.setAuthenticated(true);
  SecurityContextHolder.getContext().setAuthentication(auth);
  ObjectIdentity topParentOid = new ObjectIdentityImpl(TARGET_CLASS,
      Long.valueOf(110));
  MutableAcl topParent = jdbcMutableAclService.createAcl(topParentOid);
  // Add an ACE permission entry
  Permission cm = new CumulativePermission().set(BasePermission.READ).set(
      BasePermission.ADMINISTRATION);
  assertThat(cm.getMask()).isEqualTo(17);
  Sid benSid = new PrincipalSid(auth);
  topParent.insertAce(0, cm, benSid, true);
  assertThat(topParent.getEntries()).hasSize(1);
  // Explicitly save the changed ACL
  topParent = jdbcMutableAclService.updateAcl(topParent);
  // Check the mask was retrieved correctly
  assertThat(topParent.getEntries().get(0).getPermission().getMask()).isEqualTo(17);
  assertThat(topParent.isGranted(Arrays.asList(cm), Arrays.asList(benSid), true)).isTrue();
  SecurityContextHolder.clearContext();
}

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

auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

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

@Test
public void updatedAceValuesAreCorrectlyReflectedInAcl() throws Exception {
  Authentication auth = new TestingAuthenticationToken("ben", "ignored",
      "ROLE_GENERAL");
  auth.setAuthenticated(true);
  SecurityContextHolder.getContext().setAuthentication(auth);
  MutableAcl acl = new AclImpl(objectIdentity, 1, authzStrategy, pgs, null, null,
      false, new PrincipalSid("joe"));
  MockAclService service = new MockAclService();
  acl.insertAce(0, BasePermission.READ, new GrantedAuthoritySid("ROLE_USER_READ"),
      true);
  acl.insertAce(1, BasePermission.WRITE, new GrantedAuthoritySid("ROLE_USER_READ"),
      true);
  acl.insertAce(2, BasePermission.CREATE, new PrincipalSid("ben"), true);
  service.updateAcl(acl);
  assertThat(BasePermission.READ).isEqualTo(acl.getEntries().get(0).getPermission());
  assertThat(BasePermission.WRITE).isEqualTo(acl.getEntries().get(1).getPermission());
  assertThat(BasePermission.CREATE).isEqualTo(acl.getEntries().get(2).getPermission());
  // Change each permission
  acl.updateAce(0, BasePermission.CREATE);
  acl.updateAce(1, BasePermission.DELETE);
  acl.updateAce(2, BasePermission.READ);
  // Check the change was successfully made
  assertThat(BasePermission.CREATE).isEqualTo(acl.getEntries().get(0).getPermission());
  assertThat(BasePermission.DELETE).isEqualTo(acl.getEntries().get(1).getPermission());
  assertThat(BasePermission.READ).isEqualTo(acl.getEntries().get(2).getPermission());
}

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

Authentication auth = new TestingAuthenticationToken("user", "password",
    "ROLE_GENERAL", "ROLE_AUDITING", "ROLE_OWNERSHIP");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

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

/**
 * SEC-655
 */
@Test
@Transactional
public void childrenAreClearedFromCacheWhenParentIsUpdated() throws Exception {
  Authentication auth = new TestingAuthenticationToken("ben", "ignored",
      "ROLE_ADMINISTRATOR");
  auth.setAuthenticated(true);
  SecurityContextHolder.getContext().setAuthentication(auth);
  ObjectIdentity parentOid = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(104));
  ObjectIdentity childOid = new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(105));
  MutableAcl parent = jdbcMutableAclService.createAcl(parentOid);
  MutableAcl child = jdbcMutableAclService.createAcl(childOid);
  child.setParent(parent);
  jdbcMutableAclService.updateAcl(child);
  parent = (AclImpl) jdbcMutableAclService.readAclById(parentOid);
  parent.insertAce(0, BasePermission.READ, new PrincipalSid("ben"), true);
  jdbcMutableAclService.updateAcl(parent);
  parent = (AclImpl) jdbcMutableAclService.readAclById(parentOid);
  parent.insertAce(1, BasePermission.READ, new PrincipalSid("scott"), true);
  jdbcMutableAclService.updateAcl(parent);
  child = (MutableAcl) jdbcMutableAclService.readAclById(childOid);
  parent = (MutableAcl) child.getParentAcl();
  assertThat(parent.getEntries()).hasSize(2).withFailMessage("Fails because child has a stale reference to its parent");
  assertThat(parent.getEntries().get(0).getPermission().getMask()).isEqualTo(1);
  assertThat(parent.getEntries().get(0).getSid()).isEqualTo(new PrincipalSid("ben"));
  assertThat(parent.getEntries().get(1).getPermission().getMask()).isEqualTo(1);
  assertThat(parent.getEntries().get(1).getSid()).isEqualTo(new PrincipalSid("scott"));
}

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

Authentication auth = new TestingAuthenticationToken("ben", "ignored",
    "ROLE_GENERAL");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
ObjectIdentity identity = new ObjectIdentityImpl(TARGET_CLASS, (100));

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

Authentication auth = new TestingAuthenticationToken("ben", "ignored",
    "ROLE_AUDITING", "ROLE_GENERAL");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
MutableAcl acl = new AclImpl(objectIdentity, 1, authzStrategy, pgs, null, null,

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

Authentication auth = new TestingAuthenticationToken("user", "password",
    "ROLE_ONE");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

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

Authentication auth = new TestingAuthenticationToken("ben", "ignored",
    "ROLE_GENERAL", "ROLE_GUEST");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
ObjectIdentity rootOid = new ObjectIdentityImpl(TARGET_CLASS, 100);

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

Authentication auth = new TestingAuthenticationToken("ben", "ignored",
    "ROLE_GENERAL");
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
ObjectIdentity grandParentOid = new ObjectIdentityImpl(TARGET_CLASS, 100);

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

auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

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

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  if (this.grantAccess) {
    authentication.setAuthenticated(true);
  }
  return authentication;
}

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

auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

代码示例来源:origin: eclipse/hawkbit

@Override
  public void setAuthenticated(final boolean isAuthenticated) {
    if (delegate == null) {
      return;
    }
    delegate.setAuthenticated(isAuthenticated);
  }
}

代码示例来源:origin: org.mule.runtime/mule-module-spring-security

@Override
public void setAuthenticated(boolean authenticated) {
 delegate.setAuthenticated(authenticated);
}

相关文章