@Test
public void testFormLogin() throws Exception {
UsernamePasswordAuthenticationFilterConfig config = new UsernamePasswordAuthenticationFilterConfig();
config.setClassName(GeoServerUserNamePasswordAuthenticationFilter.class.getName());
config.setUsernameParameterName("username");
config.setPasswordParameterName("password");
config.setName(testFilterName6);
getSecurityManager().saveFilter(config);
// LogoutFilterConfig loConfig = new LogoutFilterConfig();
// loConfig.setClassName(GeoServerLogoutFilter.class.getName());
// loConfig.setName(testFilterName9);
// getSecurityManager().saveFilter(loConfig);
prepareFilterChain(pattern,
GeoServerSecurityFilterChain.FORM_LOGIN_FILTER);
modifyChain(pattern, false, true,null);
prepareFilterChain(ConstantFilterChain.class,"/j_spring_security_check_foo/",
testFilterName6);
modifyChain("/j_spring_security_check_foo/", false, true,null);
// prepareFilterChain(LogoutFilterChain.class,"/j_spring_security_logout_foo",
// GeoServerSecurityFilterChain.SECURITY_CONTEXT_ASC_FILTER,
// testFilterName9);
SecurityContextHolder.getContext().setAuthentication(null);
// Test entry point
MockHttpServletRequest request= createRequest("/foo/bar");
MockHttpServletResponse response= new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
String tmp = response.getHeader("Location");
assertTrue(tmp.endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_FORM));
SecurityContext ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check success
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), testUserName);
request.setupAddParameter(config.getPasswordParameterName(), testPassword);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_SUCCCESS));
ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNotNull(ctx);
Authentication auth = ctx.getAuthentication();
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(auth);
assertEquals(testUserName, ((UserDetails) auth.getPrincipal()).getUsername());
assertTrue(auth.getAuthorities().contains(new GeoServerRole(rootRole)));
assertTrue(auth.getAuthorities().contains(new GeoServerRole(derivedRole)));
// Test logout
GeoServerLogoutFilter logoutFilter= (GeoServerLogoutFilter) getSecurityManager().loadFilter(GeoServerSecurityFilterChain.FORM_LOGOUT_FILTER);
request= createRequest("/j_spring_security_logout_foo");
HttpSession session = request.getSession(true);
session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, ctx);
SecurityContextHolder.getContext().setAuthentication(auth);
response= new MockHttpServletResponse();
chain = new MockFilterChain();
//getProxy().doFilter(request, response, chain);
logoutFilter.doFilter(request, response,chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
tmp = response.getHeader("Location");
assertNotNull(tmp);
assertTrue(tmp.endsWith(GeoServerLogoutFilter.URL_AFTER_LOGOUT));
assertNull(SecurityContextHolder.getContext().getAuthentication());
// test invalid password
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), testUserName);
request.setupAddParameter(config.getPasswordParameterName(), "wrongpass");
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_FAILURE));
ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check unknown user
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), "unknwon");
request.setupAddParameter(config.getPasswordParameterName(), testPassword);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_FAILURE));
ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check root user
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), GeoServerUser.ROOT_USERNAME);
request.setupAddParameter(config.getPasswordParameterName(), getMasterPassword());
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_SUCCCESS));
ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
auth = ctx.getAuthentication();
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
//checkForAuthenticatedRole(auth);
assertEquals(GeoServerUser.ROOT_USERNAME, auth.getPrincipal());
assertTrue(auth.getAuthorities().size()==1);
assertTrue(auth.getAuthorities().contains(GeoServerRole.ADMIN_ROLE));
// check root user with wrong password
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), GeoServerUser.ROOT_USERNAME);
request.setupAddParameter(config.getPasswordParameterName(), "geoserver1");
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_FAILURE));
ctx = (SecurityContext)request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check disabled user
updateUser("ug1", testUserName, false);
request= createRequest("/j_spring_security_check_foo");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
request.setMethod("POST");
request.setupAddParameter(config.getUsernameParameterName(), testUserName);
request.setupAddParameter(config.getPasswordParameterName(), testPassword);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
assertTrue(response.getHeader("Location").endsWith(GeoServerUserNamePasswordAuthenticationFilter.URL_LOGIN_FAILURE));
ctx = (SecurityContext)request.getSession(true).getAttribute(