用struts拦截器实现登录验证功能AuthorizationInterceptor
2011-11-06 20:51
459 查看
前段时间在项目中曾经用Struts的拦截器实现登录验证功能,AuthorizationInterceptor.java,现在分享下,哈
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*;
import com.opensymphony.xwork2.*;
import java.util.*;
/**
* @author http://www.cnblogs.com/jobscn/ */
public class AuthorizationInterceptor extends AbstractInterceptor {
private String ignoreActions;
// ignoreActions属性的getter方法
public String getIgnoreActios() {
return ignoreActions;
}
// ignoreActions属性的setter方法
public void setIgnoreActions(String ignoreActions) {
this.ignoreActions = 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;
}
}
}
在struts.xml文件里面配置该拦截器:
<interceptors>
<interceptor name="authorization" class="com.interceptor.AuthorizationInterceptor" />
<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"/>
package com.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*;
import com.opensymphony.xwork2.*;
import java.util.*;
/**
* @author http://www.cnblogs.com/jobscn/ */
public class AuthorizationInterceptor extends AbstractInterceptor {
private String ignoreActions;
// ignoreActions属性的getter方法
public String getIgnoreActios() {
return ignoreActions;
}
// ignoreActions属性的setter方法
public void setIgnoreActions(String ignoreActions) {
this.ignoreActions = 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;
}
}
}
在struts.xml文件里面配置该拦截器:
<interceptors>
<interceptor name="authorization" class="com.interceptor.AuthorizationInterceptor" />
<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"/>
相关文章推荐
- struts中用拦截器实现登录验证功能AuthorizationInterceptor
- 用拦截器实现登录验证功能AuthorizationInterceptor
- node.js学习笔记(3)-node.js结合mysql数据库实现的web项目中常见功能--登录验证、session传值、拦截器、ajax传值等
- myeclipse+struts登录界面 之 实现客户端验证
- SpringBoot + SpringSecurity 短信验证码登录功能实现
- 拦截器实现对用户是否登录及登陆超时的验证
- 学习淘淘商城第一百零三课(利用拦截器实现登录功能及订单确认页面展示)
- ASP.NET jQuery 实例11 通过使用jQuery validation插件简单实现用户登录页面验证功能
- JavaWeb学习记录(六)——用户登录功能之Session与验证码验证功能的实现
- SpringMVC学习笔记2_拦截器实现登录验证
- 登录注册功能的实现,核心的验证代码
- shiro框架---关于用户登录和权限验证功能的实现步骤(二)
- struts实战--登录功能实现
- SpringMVC拦截器――实现登录验证拦截器的示例代码
- Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】
- Struts中用Filter实现每个页面的登录验证(转)
- Python登录注册验证功能实现
- springboot实现拦截器之验证登录示例
- 自学php,用php服务端和swift客户端实现验证登录和注册功能 1
- Struts开发一个权限验证拦截器来判断用户是否登录