权限校验

本文详细阐述了权限校验的实现方式,通过继承自RequestProcessor的自定义类ShyRequestProcessor来确保用户操作符合其权限。介绍了如何通过反射获取方法上的注解,以及如何根据用户权限列表判断操作是否合法。

package com.newer.liu.system.comment;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.actions.DispatchAction;

import com.newer.liu.system.bean.OperatorBean;
import com.newer.liu.system.bean.PrivilegeBean;
import com.newer.liu.system.servce.OperatorServce;
import com.newer.liu.system.servce.PrivilegeServce;
import com.newer.liu.system.servce.impl.OperatorServceImpl;
import com.newer.liu.system.servce.impl.PrivilegeServceImpl;
import com.sun.swing.internal.plaf.metal.resources.metal;
/**
* 权限校验

*继承RequestProcessor
* @author liu
*
*/
public class ShyRequestProcessor extends RequestProcessor {

private PrivilegeServce dao = new PrivilegeServceImpl();

@Override
protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response, Action action, ActionForm form,
ActionMapping mapping) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("-----------权限------------------");
if (isValiue(request, action, mapping)) {
return super.processActionPerform(request, response, action, form,
mapping);
} else {
return mapping.findForward("error");
}
}

 

//=========================================

 

// 调用方法
private boolean isValiue(HttpServletRequest request, Action action,
ActionMapping mapping) {
Method method = this.getMethod(request, action, mapping);
MyInterface my = this.getAnnotation(method);
if (my != null) {
//得到该用户的所有权限
Object str=request.getSession().getAttribute("name");
List<PrivilegeBean> list = dao.getAll(str.toString());
System.out.println(str+"============"+list);
PrivilegeBean p = new PrivilegeBean();
p.setModel(my.mode());
p.setPrivilegeName(my.limit());
p.setOperatorId(str.toString());
if (list.contains(p)) {
return true;
} else {
return false;
}

}
return true;
}

//--------------------------------

 

 

// method 根据反射到到方法的全部值
private Method getMethod(HttpServletRequest request, Action action,
ActionMapping mapping) {
Method method = null;

String methodName = "execute";
if (action instanceof DispatchAction) {
String parme = mapping.getParameter();
methodName = request.getParameter(parme);

}
Class[] cls = { ActionMapping.class, ActionForm.class,
HttpServletRequest.class, HttpServletResponse.class };

try {
method = action.getClass().getDeclaredMethod(methodName, cls);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return method;
}

//注解
public MyInterface getAnnotation(Method name) {
MyInterface mf = null;
if (name.isAnnotationPresent(MyInterface.class)) {
mf = name.getAnnotation(MyInterface.class);

}
return mf;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值