今天修復一個以前的問題登陸的問題。
1、一個驗證的 filter 攔截所有沒鑑權的請求,鑑權的方式是:判斷session中是否純在user對象。(當然,如果是向loginAction 發出的請求,不用鑑權就允許其通過的,否則就死循環了,對吧。)
2、loginAction 鑑權通過後 Dispatch 到 index.jsp。
public String login() throws Exception
{
//一些業務邏輯 ... 如果鑑權通過,會把user 放到session中去 ActionContext.getContext().getSession().put('user', guser);
url = config.getValue("HomePage"); //讀取配置文件中的首頁路徑
return "redirect";
}
<action name="login" class="loginAction" method="login">
<result name="dispatcher" type="dispatcher">${url}</result>
<result name="redirect" type="redirect">${url}</result>
<result name="httpheader" type="httpheader">${url}</result>
<result name="success" type="redirect" >/admin/Main.jsp</result>
<result name="error" >/admin/common/error.jsp</result>
</action>
看似沒有問題,可以當我的配置文件是這樣的時候
HomePage=http://172.0.0.1:8080/index.jsp
你在跳轉上是完全沒有問題的,程序會向${HomePage} 去Dispatch。
在程序會向${HomePage} 去Dispatch的時候,依然進入到 鑑權的filter 中來了。這次進來,你覺得filter 應該不會難爲你了,讓你奔向${HomePage} 對吧。。。
sorry,不行! 原因是 filter 說 session 中沒有user 對象!!!
怎麼回事?! 當我 在filter 中 System.out.println(request.getSession().getId()); 以及 在 loginAction 中 System.out.println(request.getSession().getId()); 後,我傻眼了,兩個sessionid 不一致。那爲毛不一致呢?在這其間我什麼都沒幹啊,程序只進行了個 dispatch 呀。
或許癥結是這個 dispatch 了,我猜。 然後我試着改。
HomePage=/index.jsp
咦好了。
bug 算 OK了,可是這到底是爲什麼!!!dispatch 帶上http 難道就是 相當於 我關掉瀏覽器 再打開瀏覽器的效果了?!望大蝦指教。