List of usage examples for org.springframework.security.core.context SecurityContextHolder setContext
public static void setContext(SecurityContext context)
SecurityContext
with the current thread of execution. From source file:test.pl.chilldev.facelets.taglib.spring.security.AuthenticationTagTest.java
@Test public void apply() throws FacesException { String property = "principal.username"; String var = "bar"; String username = "baz"; Map<String, Object> config = new HashMap<>(); config.put(AuthenticationTag.ATTRIBUTE_PROPERTY, property); config.put(AuthenticationTag.ATTRIBUTE_VAR, var); AuthenticationTag tag = new AuthenticationTag(MockTagConfig.factory(config)); // set up context FaceletContext context = new MockFaceletContext(); SecurityContextHolder.setContext(this.securityContext); when(this.securityContext.getAuthentication()).thenReturn(this.auth); when(this.auth.getPrincipal()).thenReturn(this.user); when(this.user.getUsername()).thenReturn(username); // run the tag tag.apply(context, this.parent); assertEquals("AuthenticationTag.apply() should evaluate specified expression as authentication property.", username, context.getAttribute(var)); }
From source file:com.utest.domain.service.BaseDomainServiceIntegrationTest.java
protected void loginUser(final User user) throws Exception { final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(); final AuthenticatedUserInfo authUser = new AuthenticatedUserInfo(user.getId(), user.getFullName()); final Authentication auth = new UsernamePasswordAuthenticationToken(authUser, null, grantedAuthorities); final SecurityContext ctx = new SecurityContextImpl(); ctx.setAuthentication(auth);/*from ww w . jav a 2 s . co m*/ SecurityContextHolder.setContext(ctx); }
From source file:com.mastercard.test.spring.security.LogPrincipalRuleTests.java
@Test public void ruleDoesNotBreakWhenUserIsProvided() throws Throwable { DefaultStatement statement = new DefaultStatement(); Description description = Description.createTestDescription(MockWithMockUserTest.class.getName(), "test"); LogPrincipalRule rule = new LogPrincipalRule(); Statement actual = rule.apply(statement, description); SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext()); SecurityContextHolder.getContext()/*from w ww . j a v a 2 s.c o m*/ .setAuthentication(new UsernamePasswordAuthenticationToken("user", "password")); actual.evaluate(); assertNotSame(statement, actual); assertTrue(statement.isEvaluated()); }
From source file:be.dnsbelgium.rate.spring.security.LeakyBucketVoterTest.java
@Before public void setup() { SecurityContext context = new SecurityContextImpl(); context.setAuthentication(new TestingAuthenticationToken(USERNAME, null)); SecurityContextHolder.setContext(context); keyFactory = new UsernameLeakyBucketKeyFactory(); }
From source file:org.apigw.appmanagement.revision.ApplicationManagementRevisionListenerTest.java
@Test public void testNewRevisionByNonAdmin() throws Exception { SecurityContextHolder.setContext(createSecurityContext("userA", "ROLE_USER", "ROLE_NOT_ADMIN")); ApplicationManagementRevisionListener revisionListener = new ApplicationManagementRevisionListener(); ApplicationManagementRevision revision = new ApplicationManagementRevision(); assertFalse(revision.isEditorAdmin()); assertNull(revision.getEditor());/* w ww.j a v a 2 s . co m*/ revisionListener.newRevision(revision); assertFalse(revision.isEditorAdmin()); assertEquals("userA", revision.getEditor()); }
From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelperTest.java
@Test public void testCurrentDelegateIsIneligibleDefault() { MockCalendarAccount ownerAccount = new MockCalendarAccount(); MockScheduleOwner owner = new MockScheduleOwner(ownerAccount, 1L); IDelegateCalendarAccount delegate = mock(IDelegateCalendarAccount.class); when(delegate.isEligible()).thenReturn(true); DelegateCalendarAccountUserDetailsImpl details = new DelegateCalendarAccountUserDetailsImpl(delegate, owner);/* ww w . java 2 s . co m*/ SecurityContext context = new SecurityContextImpl(); context.setAuthentication(new UsernamePasswordAuthenticationToken(details, "")); SecurityContextHolder.setContext(context); DelegateRegistrationHelper helper = new DelegateRegistrationHelper(); Assert.assertFalse(helper.currentDelegateIsIneligible()); }
From source file:org.openinfinity.core.aspect.MultiTenantAspectIntegrationTest.java
private void injectIdentityBasedSecurityContext() { SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); SecurityContextHolder.setContext(securityContext); Identity identity = new Identity(); Collection<RolePrincipal> rolePrincipals = new ArrayList<RolePrincipal>(); identity.setAuthenticated(true);/* www .j ava 2 s .c om*/ UserPrincipal userPrincipal = new UserPrincipal("test-name"); TenantPrincipal<String> tenantPrincipal = new TenantPrincipal<String>(UNIQUE_TENANT_ID); RolePrincipal rolePrincipal = new RolePrincipal("test-role"); rolePrincipals.add(rolePrincipal); identity.setUserPrincipal(userPrincipal); identity.setRolePrincipals(rolePrincipals); identity.setTenantPrincipal(tenantPrincipal); SecurityContextHolder.getContext().setAuthentication(identity); }
From source file:com.seyren.api.bean.SubscriptionsBeanTest.java
@Before public void setUp() { subscriptionsBean = new SubscriptionsBean(checkStore, subscriptionsStore, Arrays.asList(notificationService), permissionsStore, seyrenConfig); Authentication authentication = Mockito.mock(Authentication.class); SecurityContext securityContext = Mockito.mock(SecurityContext.class); when(securityContext.getAuthentication()).thenReturn(authentication); when(securityContext.getAuthentication().getName()).thenReturn("test"); SecurityContextHolder.setContext(securityContext); }
From source file:org.ff4j.security.test.FlipSecurityTests.java
@Before public void setUp() throws Exception { securityCtx = SecurityContextHolder.getContext(); // Init SpringSecurity Context SecurityContext context = new SecurityContextImpl(); List<GrantedAuthority> listOfRoles = new ArrayList<GrantedAuthority>(); listOfRoles.add(new SimpleGrantedAuthority("ROLE_USER")); User u1 = new User("user1", "user1", true, true, true, true, listOfRoles); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u1.getUsername(), u1.getPassword(), u1.getAuthorities()); token.setDetails(u1);/*from w w w .ja va 2 s .com*/ context.setAuthentication(token); SecurityContextHolder.setContext(context); // <-- ff4j = new FF4j("test-ff4j-security-spring.xml"); ff4j.setAuthorizationsManager(new SpringSecurityAuthorisationManager()); }
From source file:org.ngrinder.security.NGrinderAuthenticationProviderTest.java
@Test public void testAdditionalAuthenticationChecks() { UserDetails user = userDetailService.loadUserByUsername(getTestUser().getUserId()); //remove authentication temporally Authentication oriAuth = SecurityContextHolder.getContext().getAuthentication(); SecurityContextImpl context = new SecurityContextImpl(); SecurityContextHolder.setContext(context); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin", null); try {// ww w . ja va 2s . com provider.additionalAuthenticationChecks(user, token); assertTrue(false); } catch (BadCredentialsException e) { assertTrue(true); } token = new UsernamePasswordAuthenticationToken("TEST_USER", "123"); provider.additionalAuthenticationChecks(user, token); context.setAuthentication(oriAuth); }