Search in sources :

Example 16 with SecurityContext

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

the class GrantedAuthorityDefaultsJcTests method doFilter.

@Test
public void doFilter() throws Exception {
    SecurityContext context = SecurityContextHolder.getContext();
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 17 with SecurityContext

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

the class GrantedAuthorityDefaultsXmlTests method doFilter.

@Test
public void doFilter() throws Exception {
    SecurityContext context = SecurityContextHolder.getContext();
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 18 with SecurityContext

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

the class GrantedAuthorityDefaultsXmlTests method doFilterIsUserInRole.

// SEC-2926
@Test
public void doFilterIsUserInRole() throws Exception {
    SecurityContext context = SecurityContextHolder.getContext();
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            HttpServletRequest httpRequest = (HttpServletRequest) request;
            assertThat(httpRequest.isUserInRole("USER")).isTrue();
            assertThat(httpRequest.isUserInRole("INVALID")).isFalse();
            super.doFilter(request, response);
        }
    };
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(chain.getRequest()).isNotNull();
}
Also used : ServletException(javax.servlet.ServletException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SecurityContext(org.springframework.security.core.context.SecurityContext) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.Test)

Example 19 with SecurityContext

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

the class GrantedAuthorityDefaultsXmlTests method doFilterDenied.

@Test
public void doFilterDenied() throws Exception {
    setup("DENIED");
    SecurityContext context = SecurityContextHolder.getContext();
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN);
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) Test(org.junit.Test)

Example 20 with SecurityContext

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

the class HttpSecurityLogoutTests method clearAuthenticationFalse.

// SEC-2848
@Test
public void clearAuthenticationFalse() throws Exception {
    loadConfig(ClearAuthenticationFalseConfig.class);
    SecurityContext currentContext = SecurityContextHolder.createEmptyContext();
    currentContext.setAuthentication(new TestingAuthenticationToken("user", "password", "ROLE_USER"));
    request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, currentContext);
    request.setMethod("POST");
    request.setServletPath("/logout");
    springSecurityFilterChain.doFilter(request, response, chain);
    assertThat(currentContext.getAuthentication()).isNotNull();
}
Also used : SecurityContext(org.springframework.security.core.context.SecurityContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Aggregations

SecurityContext (org.springframework.security.core.context.SecurityContext)147 Test (org.junit.Test)65 Authentication (org.springframework.security.core.Authentication)63 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)46 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)46 MifosUser (org.mifos.security.MifosUser)38 MifosUserBuilder (org.mifos.builders.MifosUserBuilder)29 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 HttpServletResponse (javax.servlet.http.HttpServletResponse)15 Before (org.junit.Before)15 GrantedAuthority (org.springframework.security.core.GrantedAuthority)13 ArrayList (java.util.ArrayList)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)7 SessionDestroyedEvent (org.springframework.security.core.session.SessionDestroyedEvent)7 HttpSession (javax.servlet.http.HttpSession)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6