我的spring安全代码没有使用像css,js和images文件夹这样的静态资源

时间:2014-02-19 11:59:12

标签: java css spring spring-mvc spring-security

我正在尝试访问我在spring security中使用的jsp中的静态资源...但是它不是访问那些静态资源需要你的...宝贵的建议..我是Spring安全新的....

我的dispacher-servlet是......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.nufame" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>


<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}    
</prop>
        </props>
    </property>
</bean>

我的security.xml是..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/security
                       http://www.springframework.org/schema/security/spring-security-3.1.xsd
                       http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
 <!-- Non secure URLs -->
<security:http auto-config="true">
    <security:intercept-url pattern="/index*" access="ROLE_USER" />
    <security:form-login login-page="/login" default-target-url="/index" 
    authentication-failure-url="/fail2login" />
    <security:logout invalidate-session="true"
        logout-success-url="/logout" />


    <security:access-denied-handler
        error-page="/403" />
</security:http>
<security:http pattern="/css/**" security="none" />


<security:authentication-manager>
    <security:authentication-provider>

        <!-- <security:user-service> <security:user name="dineshonjava" password="sweety" 
            authorities="ROLE_USER" /> </security:user-service> -->
        <security:jdbc-user-service
            data-source-ref="dataSource"
            users-by-username-query="select username, password, active from users where username=?"
            authorities-by-username-query="select us.username, ur.authority from users us, user_roles ur 
          where us.user_id = ur.user_id and us.username =?  " />
    </security:authentication-provider>
</security:authentication-manager>

</beans>

我的web.xml是......

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 <servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
              org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
        /WEB-INF/sdnext-*.xml,
    </param-value>
</context-param>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>

 <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>
<welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

请帮助我成为春季安全新人..

提前感谢....

2 个答案:

答案 0 :(得分:0)

在dispatcher-servlet中,使用<mvc:resources>添加资源,例如:
<mvc:resources mapping="/css/**" location="/css/">

并且不要忘记在<bean: ...>顶部添加这些行:
的xmlns:MVC = “http://www.springframework.org/schema/mvc”
...
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd

答案 1 :(得分:0)