310
技術社區[雲棲]
struts中用攔截器實現登錄驗證功能AuthorizationInterceptor
用攔截器實現登錄驗證功能AuthorizationInterceptor
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*;
import com.opensymphony.xwork2.*;
import java.util.*;
public class AuthorizationInterceptor extends AbstractInterceptor {
private String ignoreActions;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
String user = (String) session.get("username");
boolean ignore = false;
String currentAction = invocation.getProxy().getActionName();
String[] actions = ignoreActions.split(",");
for (String action : actions) {
if (currentAction.matches(action.trim())) {
ignore = true;
break;
}
}
if (user != null || ignore == true) {
return invocation.invoke();
} else {
return Action.LOGIN;
}
}
public String getIgnoreActios() {
return ignoreActions;
}
public void setIgnoreActions(String ignoreActions) {
this.ignoreActions = ignoreActions;
}
}
在struts.xml文件裏麵配置該攔截器
<interceptors>
<interceptor name="authorization" />
<interceptor-stack name="myStack">
<interceptor-ref name="authorization">
<param name="ignoreActions"> validate_code,register.*,.*login.*,upload,connector</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
原帖地址:https://www.cnblogs.com/jobscn/archive/2011/11/22/2258343.html
最後更新:2017-04-02 16:48:17