我想做一个登录验证,未登录的其他的所有的action都拦截跳转到登录页面,不知道哪里出错,其他的action总是直接跳转到登录页面,不管是否登陆成功
贴上我的登录action和自定义拦截器,以及struts.xml
登录action
public String login(){
userInfo = new UserInfo();
userInfo.setName(userName);
userInfo.setPassword(userPassword);
if(userService.checkPassword(userInfo)){
status = STATUS_SUCCESS;
}else {
status = STATUS_FAIL;
}
log.info(status+"");
if(status==STATUS_SUCCESS){
ActionContext.getContext().put(Constants.USER_SESSION, userName);
ActionContext.getContext().put(Constants.USER_PASS, userPassword);
Log.info(userName+" log in success!");
return SUCCESS;
}else {
return ERROR;
}
}
拦截器class
public class LoginInterceptor extends AbstractInterceptor {
private static Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
String user = (String) session.get(Constants.USER_SESSION);
log.info(user);
if (!StringUtils.isEmpty(user)) {
log.info(user+" has log in!");
return invocation.invoke();
}else {
log.info("未登录!");
}
ctx.put("tip", "你还没有登录");
return Action.LOGIN;
}
struts.xml
<package name="all" extends="json-default">
<interceptors>
<interceptor name="authority"
class="chris.action.LoginInterceptor"/>
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefault" />
<global-results>
<result name="login">/admin/login.ftl</result>
</global-results>
</package>
<package name="admin" namespace="/admin" extends="all">
<action name="addUser" class="chris.action.AdminAction" method="addUser">
<result name="success" type="json">
<param name="includeProperties">status</param>
</result>
</action>
<action name="login" class="chris.action.AdminAction" method="login">
<result name="success" type="freemarker">/admin/welcome.ftl</result>
<result name="error" type="freemarker">/admin/login.ftl</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
<action name="welcome" class="chris.action.AdminAction" method="welcome">
<result name="success" type="freemarker">/admin/welcome.ftl</result>
</action>
</package>
------解决思路----------------------
<action name="login" class="chris.action.AdminAction" method="login">
<result name="success" type="freemarker">/admin/welcome.ftl</result>
<result name="error" type="freemarker">/admin/login.ftl</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
把name="defaultStack"改成你自定义的拦截器名试一下
------解决思路----------------------
配置文件的12行name=“error”。还有24行的拦截器name="mydefault"
------解决思路----------------------
那你就在拦截器里排除登录行为的方法啊!有个<param name="excludeMethods">login</param>这个参数。
------解决思路----------------------
<action name="login" class="chris.action.AdminAction" method="login">
<result name="success" type="freemarker">/admin/welcome.ftl</result>
<result name="error" type="freemarker">/admin/login.ftl</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
把name="defaultStack"改成你自定义的拦截器名试一下
改成我的就直接把登陆的动作都拦截了,那就没法在session里面put了
确实不能这样配置,没详细看题目。
你登录的时候没有put进去session吧,getContext以后是不是还需要调用getSession再put