Results 1 to 4 of 4

Thread: Problem when testing my login controller using Spring security

  1. #1
    Join Date
    Apr 2010
    Posts
    104

    Default Problem when testing my login controller using Spring security

    Hi,

    I'm using Spring 3.1.0.RELEASE with spring security 3.1. I'm trying to test my login controller but running into some problems. Does anyone know what request URI I should be using? My JUnit class is

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "file:src/main/webapp/WEB-INF/dispatcher-servlet.xml",
    						"file:src/main/webapp/WEB-INF/spring-security.xml"})
    public class LoginControllerTest {
    
    	private MockHttpServletRequest request;
    	private MockHttpServletResponse response;
    	...
    
        @Autowired
        private RequestMappingHandlerAdapter handlerAdapter;
    
        @Autowired
        private RequestMappingHandlerMapping handlerMapping;
        
    	@Before
    	public void setUp() { 
    		...
    		request = new MockHttpServletRequest();
    		response = new MockHttpServletResponse();
    	}	// setUp
    	
    	@Test
    	public void loginSuccessfully() throws Exception { 
    		request.setRequestURI("j_spring_security_check");
    		request.setMethod("POST");
    		request.setParameter("username", user.getUsername());
    		request.setParameter("password", user.getPassword());
    		
    		final Object handler = handlerMapping.getHandler(request).getHandler();
            final ModelAndView mav = handlerAdapter.handle(request, response, handler);
    
    		assertViewName(mav, "welcome");
    	}	// loginSuccessfully
    The clause, "handlerMapping.getHandler(request)" is returning null, causing my test to crash. In the past, I have noticed this is because the request URI isn't correct. Here is my spring-security.xml file ...

    Code:
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:beans="http://www.springframework.org/schema/beans" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/security
    	http://www.springframework.org/schema/security/spring-security-3.1.xsd">
     
     	<http pattern="/user/**" security="none"/>
    	<http auto-config="true">
    		<intercept-url pattern="/welcome*" access="ROLE_USER" />
    		<form-login login-page="/login" default-target-url="/welcome"
    			authentication-failure-url="/loginfailed" />
    		<logout logout-success-url="/logout" />
    	</http>
     
    	<authentication-manager>
    		<authentication-provider user-service-ref="customUserDetailsService">
    			<password-encoder ref="encoder" />
    		</authentication-provider>
     	</authentication-manager>
     	
    </beans:beans>
    Everything works fine when I run my application in the container, so I know its something specific about my setup. Thanks for any insights, - Dave

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    That has nothing to do with your URL mapping. Spring Security uses filters not controllers you are similating a request inside the dispatcher servlet...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2010
    Posts
    104

    Default

    Ok, so what URL should I use or how can I figure it out? Thanks, - Dave

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    As I already stated it has NOTHING to do with your URL... You cannot test your login this way, you will need to start a server (embedded Jetty or Tomcat for instance) and fire a request.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •