Search in sources :

Example 56 with SecurityContextImpl

use of org.springframework.security.core.context.SecurityContextImpl in project spring-security by spring-projects.

the class SecurityContextPersistenceFilterTests method loadedContextContextIsCopiedToSecurityContextHolderAndUpdatedContextIsStored.

@Test
public void loadedContextContextIsCopiedToSecurityContextHolderAndUpdatedContextIsStored() throws Exception {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final TestingAuthenticationToken beforeAuth = new TestingAuthenticationToken("someoneelse", "passwd", "ROLE_B");
    final SecurityContext scBefore = new SecurityContextImpl();
    final SecurityContext scExpectedAfter = new SecurityContextImpl();
    scExpectedAfter.setAuthentication(testToken);
    scBefore.setAuthentication(beforeAuth);
    final SecurityContextRepository repo = mock(SecurityContextRepository.class);
    SecurityContextPersistenceFilter filter = new SecurityContextPersistenceFilter(repo);
    when(repo.loadContext(any(HttpRequestResponseHolder.class))).thenReturn(scBefore);
    final FilterChain chain = new FilterChain() {

        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertThat(SecurityContextHolder.getContext().getAuthentication()).isEqualTo(beforeAuth);
            // Change the context here
            SecurityContextHolder.setContext(scExpectedAfter);
        }
    };
    filter.doFilter(request, response, chain);
    verify(repo).saveContext(scExpectedAfter, request, response);
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) SecurityContext(org.springframework.security.core.context.SecurityContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with SecurityContextImpl

use of org.springframework.security.core.context.SecurityContextImpl in project spring-security by spring-projects.

the class HttpSessionDestroyedEventTests method setUp.

@Before
public void setUp() {
    session = new MockHttpSession();
    session.setAttribute("notcontext", "notcontext");
    session.setAttribute("null", null);
    session.setAttribute("context", new SecurityContextImpl());
    destroyedEvent = new HttpSessionDestroyedEvent(session);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Example 58 with SecurityContextImpl

use of org.springframework.security.core.context.SecurityContextImpl in project spring-security by spring-projects.

the class AuthorizeRequestsTests method roleHiearchy.

// gh-3394
@Test
public void roleHiearchy() throws Exception {
    loadConfig(RoleHiearchyConfig.class);
    SecurityContext securityContext = new SecurityContextImpl();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("test", "notused", AuthorityUtils.createAuthorityList("ROLE_USER")));
    this.request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext);
    this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
    assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 59 with SecurityContextImpl

use of org.springframework.security.core.context.SecurityContextImpl in project spring-security by spring-projects.

the class SessionManagementConfigurerServlet31Tests method login.

private void login(Authentication auth) {
    HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
    HttpRequestResponseHolder requestResponseHolder = new HttpRequestResponseHolder(request, response);
    repo.loadContext(requestResponseHolder);
    SecurityContextImpl securityContextImpl = new SecurityContextImpl();
    securityContextImpl.setAuthentication(auth);
    repo.saveContext(securityContextImpl, requestResponseHolder.getRequest(), requestResponseHolder.getResponse());
}
Also used : HttpRequestResponseHolder(org.springframework.security.web.context.HttpRequestResponseHolder) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) HttpSessionSecurityContextRepository(org.springframework.security.web.context.HttpSessionSecurityContextRepository)

Example 60 with SecurityContextImpl

use of org.springframework.security.core.context.SecurityContextImpl in project spring-security by spring-projects.

the class SecurityContextMixinTests method securityContextSerializeTest.

// @formatter:on
@Test
public void securityContextSerializeTest() throws JsonProcessingException, JSONException {
    SecurityContext context = new SecurityContextImpl();
    context.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "1234", Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"))));
    String actualJson = mapper.writeValueAsString(context);
    JSONAssert.assertEquals(SECURITY_CONTEXT_JSON, actualJson, true);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)69 Authentication (org.springframework.security.core.Authentication)48 SecurityContext (org.springframework.security.core.context.SecurityContext)46 MifosUser (org.mifos.security.MifosUser)38 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)38 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)29 Test (org.junit.Test)18 Before (org.junit.Before)16 GrantedAuthority (org.springframework.security.core.GrantedAuthority)15 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)11 ArrayList (java.util.ArrayList)8 TgolUserDetails (org.asqatasun.webapp.security.userdetails.TgolUserDetails)5 MeetingBO (org.mifos.application.meeting.business.MeetingBO)5 OrcidOAuth2Authentication (org.orcid.core.oauth.OrcidOAuth2Authentication)5 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)5 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)5 Date (java.util.Date)4 LocalDate (org.joda.time.LocalDate)4 Money (org.mifos.framework.util.helpers.Money)4 BigDecimal (java.math.BigDecimal)3