Spring 3 Security J_Spring_Security_Check

Spring 3 Security J_Spring_Security_Check

I'm trying to learn, how spring security works, so I've downloaded some sample project and then I tried to implement that solution to my project. But when I try to login, I get 404 error and in an address bar I have . I tried to look at similar questions here, but I wasn't able to realize, how to apply it to my project. I'd be really thankful, if somebody, who is more experienced, could help me.

My app structure looks like this:

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
   xmlns:xsi=""
   xmlns:context=""
   xmlns:security=""
   xsi:schemaLocation="
     
     
     ">

<context:annotation-config/>

<context:component-scan base-package="cz.cvut.fit"/>

<import resource="classpath:applicationContext-security.xml"/>

</beans>

applicationContext-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
   xmlns:xsi=""
   xmlns:mvc=""
   xmlns:context=""
   xmlns:security=""
   xsi:schemaLocation="
     
     
     
     ">

<context:annotation-config/>

<context:component-scan base-package="cz.cvut.fit" />

<mvc:annotation-driven />

<security:global-method-security jsr250-annotations="enabled"
                                 proxy-target-class="true"/>
</beans>

applicationContext-security.xml:

<beans xmlns:security=""
   xmlns=""
   xmlns:xsi=""
   xsi:schemaLocation="
             
             
             ">

<security:http pattern="/css/**" security="none"/>
<security:http pattern="/views/login.jsp*" security="none"/>
<security:http pattern="/views/denied.jsp" security="none"/>

<security:http auto-config="true" access-denied-page="/denied.jsp" servlet-api-provision="false">
    <security:intercept-url pattern="/views/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <security:intercept-url pattern="/views/edit/**" access="ROLE_EDIT"/>
    <security:intercept-url pattern="/views/admin/**" access="ROLE_ADMIN"/>
    <security:intercept-url pattern="/**" access="ROLE_USER"/>
    <security:form-login login-page="/views/login.jsp" authentication-failure-url="/denied.jsp"
                         default-target-url="/home.jsp"/>
    <security:logout/>
</security:http>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="adam" password="adampassword" authorities="ROLE_USER"/>
            <security:user name="jane" password="janepassword" authorities="ROLE_USER, ROLE_ADMIN"/>
            <security:user name="sue" password="suepassword" authorities="ROLE_USER, ROLE_EDIT"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

</beans>
8

3 Answers

You are trying to validate to a uri based on the current context path of the web page. the JSTL tag lib can be used to ensure you easily generate the correct urls based on the context of the application. You can do this by using a tag library if you want to get it implemented quickly. To do this you can add the jstl tag library to the top of the jsp:

<%@ taglib prefix="c" uri="" %>

Then you can use the following to post to the login servlet.

<form action="<c:url value="/j_spring_security_check"></c:url>" method="post" role="form">

This ensures you alway post to <your_application_context>/j_spring_security_check.

reference for jstl:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Add your web.xml File .Its Create Bean of Your springSecurityFilterChain .Then you Got Response

The path /j_spring_security_check has changed to /login in spring 4 and it is handled in UsernamePasswordAuthenticationFilter

You can find it's source here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.