java - 我的 spring 安全代码没有使用静态资源,如 css、js 和图像文件夹

标签 java css spring spring-mvc spring-security

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

我的 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>

请帮助我是 Spring 安全新手..

提前致谢....

最佳答案

在 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

关于java - 我的 spring 安全代码没有使用静态资源,如 css、js 和图像文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879824/

相关文章:

java - 如何在不使用 vector 的情况下在JTable中显示MySQL数据?

css - 绝对定位页面上的元素,即使父元素的位置为 :relative or absolute

java - Spring aop、cglib增强类丢失通用信息

java - 带 @Secured 和 @PreAuthorize 注释的类不保护 super 方法

java - 如何禁用View的所有子元素捕获鼠标触摸事件

java - 如何在单击另一个 Jbutton 时更改 Jbutton 的功能?

java - 如何让 Android 按钮阵列的 onClickListener 正常工作

html - 左、右和中间内容位置

javascript - jQuery动画批量图像

java - hibernate 分页导致选择和更新调用