简体   繁体   中英

Spring Security Failed to load resource: the server responded with a status of 404

When ever i navigate to authenticated url, all the static contents even the url's inside href has been attached with the route's url because of that i'm getting 404 for all the resources in that page and all the urls inside href element

for ex

    <security:http auto-config="true" use-expressions="false"> 
            <security:intercept-url pattern="/reports/**" access="ROLE_USER"/>
            <security:intercept-url pattern="/**" access="ROLE_ANONYMOUS"/>

            <security:form-login login-page="/login.do" login-processing-url="/login.do" username-parameter="custom_username" 
                password-parameter="custom_password"
                 authentication-success-handler-ref="authSuccessHandler"
                default-target-url="/"
                always-use-default-target="true"
                authentication-failure-url="/login.do?error=true"/>
                <security:logout logout-url="/logout.do" logout-success-url="/login.do?logout=true"/>
            <security:csrf disabled="true"/>
        </security:http >

        <security:authentication-manager>
            <security:authentication-provider>
                <security:user-service>
                    <security:user name="user1" password="password" authorities="ROLE_USER"/>
                    <security:user name="admin" password="password" authorities="ROLE_USER,ROLE_FOO"/>
                </security:user-service>
            </security:authentication-provider>
        </security:authentication-manager>
<security:http  pattern="**/styles/**"  security="none" />
<security:http  pattern="**/js/**"  security="none" />

When i navigate to myapp/reports/ my static contents have been changed to

在此处输入图片说明

normally in html it looks like

 <link href="styles/jqueryUI.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" href="styles/Home.css"> <link rel="stylesheet" href="styles/metisMenu.min.css"> <link rel="stylesheet" href="styles/sideBar.css"> 

Something i missed here even i have enebled mvc:resources

<mvc:annotation-driven/>
<mvc:resources mapping="/WebContent/styles/**" location="css/"  />
<mvc:resources mapping="/WebContent/js/**" location="js/"  />
<mvc:resources mapping="/WebContent/images/**" location="images/"  />
<mvc:resources mapping="/WebContent/fonts/**" location="fonts/"  />

There are problems at your resource mapping and href attribute of link tag. As you have not posted your directory structure Let me explain how to do sample map: Here is my directory structure : 在此处输入图片说明

If I want to add customestyle.min.css to my html page I need the following settings :

<mvc:resources mapping="/contents/css/**" location="/contents/css/" />

and

<link type="text/css" rel="stylesheet" href='<c:url value="/contents/css/customestyle.min.css"/> '/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM