本文整理汇总了Java中org.springframework.security.authentication.TestingAuthenticationToken.setAuthenticated方法的典型用法代码示例。如果您正苦于以下问题:Java TestingAuthenticationToken.setAuthenticated方法的具体用法?Java TestingAuthenticationToken.setAuthenticated怎么用?Java TestingAuthenticationToken.setAuthenticated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.authentication.TestingAuthenticationToken
的用法示例。
在下文中一共展示了TestingAuthenticationToken.setAuthenticated方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAuthentication
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
/**
* Provide the mock user information to be used
*
* @param withMockOAuth2Token
* @return
*/
private Authentication getAuthentication(WithMockOAuth2Token withMockOAuth2Token) {
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(withMockOAuth2Token.authorities());
User userPrincipal = new User(withMockOAuth2Token.userName(), withMockOAuth2Token.password(), true, true, true,
true, authorities);
HashMap<String, String> details = new HashMap<String, String>();
details.put("user_name", withMockOAuth2Token.userName());
details.put("email", "[email protected]");
details.put("name", "Anil Allewar");
TestingAuthenticationToken token = new TestingAuthenticationToken(userPrincipal, null, authorities);
token.setAuthenticated(true);
token.setDetails(details);
return token;
}
开发者ID:anilallewar,项目名称:microservices-basics-spring-boot,代码行数:24,代码来源:WithOAuth2MockAccessTokenSecurityContextFactory.java
示例2: addStreamExtendsOwned
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void addStreamExtendsOwned()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Entity entity0 = mock(Entity.class);
when(entity0.getIdValue()).thenReturn("0");
Entity entity1 = mock(Entity.class);
when(entity1.getIdValue()).thenReturn("0");
Stream<Entity> entities = Stream.of(entity0, entity1);
ownedEntityRepositoryDecorator.add(entities);
@SuppressWarnings({ "unchecked", "rawtypes" })
ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass(Stream.class);
verify(delegateRepository, times(1)).add(captor.capture());
List<Entity> myEntities = captor.getValue().collect(Collectors.toList());
assertEquals(myEntities, asList(entity0, entity1));
verify(entity0, times(1)).set(OwnedEntityType.OWNER_USERNAME, "username");
verify(entity1, times(1)).set(OwnedEntityType.OWNER_USERNAME, "username");
}
示例3: findAllPermission
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void findAllPermission()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null,
"ROLE_ENTITY_READ_" + entityId);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
Stream<Object> ids = Stream.of(0, 1);
Fetch fetch = new Fetch();
Entity entity0 = mock(Entity.class);
Entity entity1 = mock(Entity.class);
Stream<Entity> entities = Stream.of(entity0, entity1);
when(delegateRepository.findAll(ids, fetch)).thenReturn(Stream.of(entity0, entity1));
assertEquals(entities.collect(toList()), repositorySecurityDecorator.findAll(ids, fetch).collect(toList()));
verify(delegateRepository, times(1)).findAll(ids, fetch);
}
示例4: findAllStreamFetchExtendsOwned
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void findAllStreamFetchExtendsOwned()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Fetch fetch = new Fetch();
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Entity entity1 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Stream<Object> entityIds = Stream.of(id0, id1);
Fetch decoratedFetch = new Fetch().field(OWNER_USERNAME);
when(delegateRepository.findAll(entityIds, decoratedFetch)).thenReturn(Stream.of(entity0, entity1));
Stream<Entity> expectedEntities = ownedEntityRepositoryDecorator.findAll(entityIds, fetch);
assertEquals(expectedEntities.collect(Collectors.toList()), asList(entity0, entity1));
}
示例5: consumeBatchedExtendsOwned
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void consumeBatchedExtendsOwned()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Fetch fetch = new Fetch();
Entity entity0 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Entity entity1 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Fetch decoratedFetch = new Fetch().field(OWNER_USERNAME);
@SuppressWarnings("unchecked")
Consumer<List<Entity>> consumer = mock(Consumer.class);
ownedEntityRepositoryDecorator.forEachBatched(fetch, consumer, 123);
verify(delegateRepository).forEachBatched(eq(decoratedFetch), consumerCaptor.capture(), eq(123));
consumerCaptor.getValue().accept(asList(entity0, entity1));
verify(consumer).accept(asList(entity0, entity1));
}
示例6: updateStreamNoPermission
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test(expectedExceptions = MolgenisDataAccessException.class)
public void updateStreamNoPermission()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null,
"ROLE_ENTITY_READ_" + entityId);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
Stream<Entity> entities = Stream.empty();
try
{
repositorySecurityDecorator.update(entities);
}
catch (MolgenisDataAccessException e)
{
verify(delegateRepository, times(1)).getEntityType();
verifyNoMoreInteractions(delegateRepository);
throw e;
}
}
示例7: deleteStreamEntityExtendsOwned
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void deleteStreamEntityExtendsOwned()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Entity myEntity = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Entity notMyEntity = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("notme").getMock();
ownedEntityRepositoryDecorator.delete(Stream.of(myEntity, notMyEntity));
@SuppressWarnings("unchecked")
ArgumentCaptor<Stream<Entity>> captor = ArgumentCaptor.forClass(Stream.class);
verify(delegateRepository, times(1)).delete(captor.capture());
List<Entity> myEntities = captor.getValue().collect(Collectors.toList());
assertEquals(myEntities, asList(myEntity));
}
示例8: findAllStreamFetchNoPermission
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test(expectedExceptions = MolgenisDataAccessException.class)
public void findAllStreamFetchNoPermission()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
Fetch fetch = new Fetch();
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = mock(Entity.class);
Entity entity1 = mock(Entity.class);
Stream<Object> entityIds = Stream.of(id0, id1);
when(delegateRepository.findAll(entityIds, fetch)).thenReturn(Stream.of(entity0, entity1));
repositorySecurityDecorator.findAll(entityIds, fetch);
}
示例9: findAllStreamExtendsOwned
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void findAllStreamExtendsOwned()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Entity entity1 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("username").getMock();
Stream<Object> entityIds = Stream.of(id0, id1);
when(delegateRepository.findAll(entityIds)).thenReturn(Stream.of(entity0, entity1));
Stream<Entity> expectedEntities = ownedEntityRepositoryDecorator.findAll(entityIds);
assertEquals(expectedEntities.collect(Collectors.toList()), asList(entity0, entity1));
}
示例10: findAllStreamExtendsOwnedBySomeoneElse
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void findAllStreamExtendsOwnedBySomeoneElse()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("notme").getMock();
Entity entity1 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("notme").getMock();
Stream<Object> entityIds = Stream.of(id0, id1);
when(delegateRepository.findAll(entityIds)).thenReturn(Stream.of(entity0, entity1));
Stream<Entity> expectedEntities = ownedEntityRepositoryDecorator.findAll(entityIds);
assertEquals(expectedEntities.collect(Collectors.toList()), emptyList());
}
示例11: findAllStreamFetchExtendsOwnedBySomeoneElse
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void findAllStreamFetchExtendsOwnedBySomeoneElse()
{
TestingAuthenticationToken authentication = new TestingAuthenticationToken("username", null);
authentication.setAuthenticated(false);
SecurityContextHolder.getContext().setAuthentication(authentication);
when(entityType.getExtends()).thenReturn(new OwnedEntityType(mock(SecurityPackage.class)));
Fetch fetch = new Fetch();
Object id0 = "id0";
Object id1 = "id1";
Entity entity0 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("notme").getMock();
Entity entity1 = when(mock(Entity.class).getString(OWNER_USERNAME)).thenReturn("notme").getMock();
Stream<Object> entityIds = Stream.of(id0, id1);
Fetch decoratedFetch = new Fetch().field(OWNER_USERNAME);
when(delegateRepository.findAll(entityIds, decoratedFetch)).thenReturn(Stream.of(entity0, entity1));
Stream<Entity> expectedEntities = ownedEntityRepositoryDecorator.findAll(entityIds, fetch);
assertEquals(expectedEntities.collect(Collectors.toList()), emptyList());
}
示例12: createAuthentication
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
public static Authentication createAuthentication(final String username,
final String plaintextPassword,
final Long userId,
final SystemUser.Role role,
final boolean authenticated) {
final SystemUser user = new SystemUser();
user.setUsername(username);
user.setId(userId);
user.setRole(role);
user.setPasswordAsPlaintext(plaintextPassword, NoOpPasswordEncoder.getInstance());
final List<GrantedAuthority> grantedAuthorities = (role != null)
? AuthorityUtils.createAuthorityList(role.name())
: new LinkedList<>();
final UserInfo.UserInfoBuilder builder =
new UserInfo.UserInfoBuilder(user);
final UserInfo principal = builder.createUserInfo();
final TestingAuthenticationToken authenticationToken =
new TestingAuthenticationToken(principal, plaintextPassword, grantedAuthorities);
authenticationToken.setAuthenticated(authenticated);
return authenticationToken;
}
示例13: shouldGetIfCurrentUserIsLoggedIn
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void shouldGetIfCurrentUserIsLoggedIn() {
//given
TestingAuthenticationToken token = new TestingAuthenticationToken("1", "2");
token.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(token);
//when
boolean loggedIn = new CurrentUserWidget().isLoggedIn();
//then
assertTrue(loggedIn);
}
示例14: shouldReturnCurrentUserName
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void shouldReturnCurrentUserName() {
//given
TestingAuthenticationToken token = new TestingAuthenticationToken("test", "2");
token.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(token);
//when
String name = new CurrentUserWidget().getUserName();
//then
assertEquals("test", name);
}
示例15: shouldReturnCurrentUserNameIfUserProfilePrincipalIsSet
import org.springframework.security.authentication.TestingAuthenticationToken; //导入方法依赖的package包/类
@Test
public void shouldReturnCurrentUserNameIfUserProfilePrincipalIsSet() {
//given
UserProfile userProfile = mock(UserProfile.class);
when(userProfile.getDisplayUsername()).thenReturn("test_simple");
TestingAuthenticationToken token = new TestingAuthenticationToken(userProfile, "2");
token.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(token);
//when
String name = new CurrentUserWidget().getUserName();
//then
assertEquals("test_simple", name);
}