【发布时间】:2012-09-26 23:29:48
【问题描述】:
我有基本的 Spring MVC + Hibernate 应用程序。这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
一切都很好。
然后我尝试通过将以下内容添加到web.xml,为应用程序添加基本的Spring Security支持:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我的/WEB-INF/security-context.xml 如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<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.0.xsd">
<http>
<intercept-url pattern="/index*" access="ROLE_USER"/>
<form-login login-page="/login.jsp" default-target-url="/index"
authentication-failure-url="/login.jsp?error=true"/>
<logout logout-url="/logout" logout-success-url="/index"/>
<remember-me/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user" password="pass" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
添加这些内容后,应用程序崩溃了。它只是在 Chrome 中显示“链接不起作用。尝试在 Google 中搜索它。”。 我错过了什么?有任何想法吗?提前致谢。
【问题讨论】:
-
你在 web.xml 中哪里添加 spring 安全过滤器?
-
日志中是否有任何异常/错误?控制台?
-
当显示“链接不起作用”时,您尝试访问的 URL 是什么?
-
@GPS 1) 过滤器添加在 web.xml 的开头,紧跟在
之后; 2) 是的,抛出的异常告诉我,我的一些 xml 格式不正确(Digester 失败),但关键是这些异常甚至在我的应用程序正常运行之前就被抛出了。 -
@Xaerxess 我正在尝试访问 'localhost:8080/index' 因为我的 '/index' url 模式已映射到查看所需信息。没有弹簧安全它可以正常工作。
标签: java spring spring-mvc spring-security