struts2用户登录和拦截器的问题

slivefox 2008-01-29 06:24:37
我在struts。xml里加入了拦截器,如下:
<interceptors>
<interceptor name="auth" class="com.aa.common.interceptor.AuthorizationInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="authStack" />
<global-results>
<result name="login" type="redirect">/index.jsp</result>
</global-results>
我的类AuthorizationInterceptor如下:
public class AuthorizationInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = -1689526894183598040L;

@Override
public String intercept(ActionInvocation ai) throws Exception {
Map session = ai.getInvocationContext().getSession();
SessionInfo sessionInfo = (SessionInfo) session.get("LoginInfoKey");
if (sessionInfo != null) {
return ai.invoke();
} else {
return Action.LOGIN;
}
}
}
我的目的就是如果用户没有登录,但他想访问系统里的页面,让他跳转到登录页,如果登录了,就继续执行。但是按这样配置了,连登录了也给拦截了(因为登录时,在session里肯定没有数据)。请大家给一个解决方案,谢谢。最好说的细点。
...全文
1533 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
吴冬冬 2008-03-10
  • 打赏
  • 举报
回复
呵呵 你代码写错了
你仔细看下
if (sessionInfo != null) {
return ai.invoke();
} 在你登陆前当然为空了

所以得修改啊
Object o = ai.getAction(); 获得Action的名称
if (o instanceof LoginAction) 如果是登陆的Action那么跳过 让其登陆 {
return ai.invoke();
}
andrew830314 2008-02-08
  • 打赏
  • 举报
回复
你登陆的页面应该是知道的吧?你可以在这个拦截器里从request.getRequestURI(),获取当前的URI,如果恰巧是你的登陆的JSP页面,就跳过if else条件分支语句!
daiyefei 2008-02-02
  • 打赏
  • 举报
回复
public String intercept(ActionInvocation ai) throws Exception {
// セッションを取得する。
Map session = ai.getInvocationContext().getSession();
Object role = session.get(CodeConstant.SESSION_CURRENT_USER);

if (null != role) {
// 登録して場合
return ai.invoke();
} else {
// 登録しなっかた場合
return BaseAction.ERROR_SESSION;
}
}

在其他画面的struts-xxx.xml上加上<default-interceptor-ref name="authStack" />
这句
daiyefei 2008-02-02
  • 打赏
  • 举报
回复
<interceptors>
<interceptor name="auth" class="cn.com.softwise.dailynet.interceptor.AuthorizationInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="auth"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
在struts.xml中 <include file=" struts-login.xml" />
struts-login.xml 不加<default-interceptor-ref name="authStack" /> 这句.
例如:
<!-- ########## ログイン画面 ############ -->
<action name="l000000Action" class="cn.com.softwise.dailynet.action.login.L000000Action" method="init">
<result name="success_init">/login/L000000.jsp</result>
<result name="success" type="redirect">l001000Action.action</result>
<result name="input">/login/L000000.jsp</result>
</action>

<!-- ########## メインメニュー画面 ############ -->
<action name="l001000Action" class="cn.com.softwise.dailynet.action.login.L001000Action">
<interceptor-ref name ="myStack" />
<result name="login">/login/L000000.jsp</result>
<result>/login/L001000.jsp</result>
</action>
yzj520hyy 2008-02-02
  • 打赏
  • 举报
回复
可以设置不拦截某些方法的呀,你不拦截跳转到登陆页的方法不就完了吗?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧