Announcement Announcement Module
Collapse
No announcement yet.
Problem when testing my login controller using Spring security Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    That has nothing to do with your URL mapping. Spring Security uses filters not controllers you are similating a request inside the dispatcher servlet...

    Comment


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

      Comment


      • #4
        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.

        Comment

        Working...
        X