2012-02-22 51 views
0

我正在使用sitemesh將我的web項目與spring安全集成。我能夠帶上登錄頁面,但是在認證之後它不會重定向到目標網址。Sitemesh與彈簧安全不重定向到目標網址

下面是我的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是在web應用\ WEB-INF \看法,這是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"; 
} 

所以這裏的問題是訪問網址 - http://localhost:8080/acct/app,它顯示登錄頁面。成功驗證後,它試圖重定向到http://localhost:8080/acct/app。不知道爲什麼它會發生,而不是 /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 [[email protected]] 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項目(不包含sitemesh)集成,則相同的AppSecurity項目會正常工作。 不知道我在這裏失蹤sitemesh項目。任何幫助在這裏都會很棒。

回答

1

"default-target-url=/index.html"這將在成功登錄後重定向頁面。 例如,您可以重定向到/home.html或在控制器中使用/home.do來執行一些邏輯操作,然後再重定向到所需的頁面。

@RequestMapping(value = "/home.do", method = RequestMethod.POST) 
public String login(ModelMap model) { 

    //TODO logic ... 
    return "/home.html"; 
}