日期:2014-05-19  浏览次数:20650 次

struts2框架 拦截器防止未登录用户访问内部信息问题

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="bqyf" extends="struts-default">

<interceptors>

<!-- 定义权限拦截器 -->
<interceptor name="sessionInUser" class="com.pb.bqyf.command.SessionInUserInterceptor"></interceptor>

<!-- 定义拦截器栈 -->
<interceptor-stack name="myStack">

<interceptor-ref name="defaultStack"></interceptor-ref>//默认的拦截器引用
<interceptor-ref name="sessionInUser"></interceptor-ref>自己定义的拦截器引用
</interceptor-stack>

</interceptors>


<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-results>//定义的全局拦截器
<result name="againIndex">index.jsp</result>
</global-results>

                //登陆验证
<action name="loginAction" class="com.pb.bqyf.user.web.LoginAction">
<result name="main">/WEB-INF/jsp/main/LoginInfo.jsp</result>
<result name="index">index.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

                //跳转到main页面 里面将各个页面分别通过<frameset></frameset>分页显示
<action name="mainAction" class="com.pb.bqyf.user.web.MainAction">
<result name="top">/WEB-INF/jsp/main/top.jsp</result>
<result name="left">/WEB-INF/jsp/main/left.jsp</result>
<result name="right">/WEB-INF/jsp/main/right.jsp</result>
</action>
</package>
</struts>    




下面的定义的拦截器对应的类

public class SessionInUserInterceptor extends AbstractInterceptor {

@Override
public String intercept(ActionInvocation arg0) throws Exception {
Map<String, Object> session = arg0.getInvocationContext().getSession();
User u = (User) session.get("user");
System.out.println(u.getUsername()+"  "+u.getPassword());
String forword = "";
if(u!=null){
arg0.invoke();
}else{
forword = "againIndex";
}
return forword;
}

}



但是每次登陆后 讲地址复制 然后重新打开IE后 将地址粘贴 就报错 而不是通过拦截器验证未得到用户而让其跳转到登陆页面 下面的是报错的代码 请问是怎么回事儿??
HTTP Status 500 - 

--------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

java.lang.NullPointerException
com.pb.bqyf.user.web.LoginAction.login(LoginAction.java:18)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccesso