我正在将 spring 安全性与使用 sitemesh 的 Web 项目集成。我能够带来登录页面,但经过身份验证后它不会重定向到目标 URL。
下面是我的 web 项目中的 web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-persistance.xml
classpath*:applicationContext.xml
classpath:spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*.app</url-pattern>
</servlet-mapping>
-->
<!-- <servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
-->
<!-- Sitemesh -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<!-- <filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping> -->
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>-- >
</web-app>
下面是来自 AppSecurity 项目的 spring-security.xml。
<security:http auto-config="true" use-expressions="true">
<security:form-login login-page="/login"
default-target-url="/index.html" always-use-default-target="true"
authentication-failure-url="/loginfailed"
authentication-success-handler-ref="postSuccessAuthHandler" />
<security:logout invalidate-session="true" logout-success-url="/app" />
<!-- <security:remember-me /> -->
<security:intercept-url pattern="/app" access="isAuthenticated()" />
<security:intercept-url pattern="/app/**" access="isAuthenticated()" />
<!-- <security:intercept-url pattern="/acct/app"
access="isAuthenticated()" /> -->
</security:http>
<!--<bean id="postSuccessAuthHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthentication SuccessHandler">
<property name="defaultTargetUrl" value="/index.html" />-->
我的 loginpage.jsp 在 webapp\WEB-INF\views 中,由 sitemesh 装饰 -
<div id="mainNav"><div class="navWrapper">
<ul>
<li class="${fn:startsWith(menuPath, 'M')? 'selected':'first'}"><a
href="${pageContext.request.contextPath}/index.html"><spring:message
code="mnu.home" /></a></li>
处理此问题的 LoginController.java 是 -
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(ModelMap model) {
return "loginpage";
}
所以这里的问题是访问 url - http://localhost:8080/acct/app,它显示登录页面。成功验证后,它会尝试重定向到http://localhost:8080/acct/app。不知道为什么会发生这种情况,而不是像default-target-url中提到的那样发生在/index.html中。
来自 tomcat 的日志行显示 -
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for
[/acct/login]
DEBUG: org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/login] to HandlerExecutionCh
ain with handler [com.mycomp.security.controller.LoginController@1e5348f] and 2 interceptors
DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/acct/login] is: -1
DEBUG: org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public java.lan
g.String com.mycomp.security.controller.LoginController.login(org.springframework.ui.ModelMap)
DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name
'loginpage'; URL [/WEB-INF/views/loginpage.jsp]] in DispatcherServlet with name 'appServlet'
DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/loginpage.jsp] in InternalResour
ceView 'loginpage'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for
[/acct/app]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/acct/app] in DispatcherServ
let with name 'appServlet'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for
[/acct/app]
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/acct/app] in DispatcherServ
let with name 'appServlet'
DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
通过将 DispatcherServlet 映射更改为 / 而不是 /index.html 尝试了很多调试。将 sitemesh 过滤器映射更改为 /* 而不是 /index.html。如果与示例 Web 项目(没有站点网格)集成,相同的 AppSecurity 项目可以正常工作。不确定我在 sitemesh 项目中缺少什么。任何帮助在这里都会很棒。