📄 30c36c8a8a33001d1343bd2c684759cb
字号:
package com.qrsx.qrsxcrm.web;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
import com.qrsx.qrsxcrm.dao.RoleDAO;
import com.qrsx.qrsxcrm.dao.UserDAO;
import com.qrsx.qrsxcrm.model.Power;
import com.qrsx.qrsxcrm.model.Role;
import com.qrsx.qrsxcrm.model.User;
public class PurviewRequestProcesscor extends RequestProcessor {
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
String url=request.getServletPath();
//得到动作
String action=request.getParameter("p");
// if(action==null&&action.trim().length()==0){
// action="list";
// }
System.out.println("-------------------------------------------------------"+action);
//得到资源
int point_place=url.indexOf('.');//得到.的位置
String resources=url.substring(1, point_place);
System.out.println("-------------------------------------------------------"+resources);
System.out.println("********************************************************"+url+"?p="+action);
if("logon".equals(resources)){
return true;
}
/**
* 得到登陆用户,并找到其角色
* 用户id存在session中
*/
String userId=(String) request.getSession().getAttribute("userId");
if( userId == null || userId.trim().equals(""))
{
String js = "<Script>alert('对不起,操作有误,请重新登录!');location.href='/qrsxcrm/logon.jsp' main='_parent';</Script>";
try {
response.getWriter().print(js);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
System.out.println( "!!!!!!!!!!!!!!!!!"+userId+"**************" );
if(userId.trim().equals("1") || userId.trim().equals("2")){//超级用户admin
return true;
}
UserDAO udao=new UserDAO(User.class);
RoleDAO rdao=new RoleDAO(Role.class);
User user=null;
String roleId=null;
Power power=null;
if(userId!=null&&userId.trim().length()>0){
user=(User) udao.findById(User.class, userId);
System.out.println("========================================="+user.getRole().getRoleName());
roleId=user.getRole().getId();
System.out.println("==============================="+roleId);
Role role=(Role) rdao.findById(Role.class, roleId); //得到角色
System.out.println("========================================="+role.getRoleName());
List powers=rdao.findPowers(role); //获得此角色的权限
Iterator it=powers.iterator();
boolean flag=false;
while(it.hasNext()){ //根据请求的动作和资源作出判断
power=(Power) it.next();
System.out.println(power.getPowerName().toString()); //打印权限的名字
System.out.println(power.getAction().toString());
if((action.equals(power.getAction().toString())&&resources.equals(power.getResources().toString()))){
System.out.println("flag 为 true....");
flag=true;
}
// else{
// System.out.println("flag 为 false....");
// flag=false;
// }
}
try {
request.getRequestDispatcher("/index.jsp").forward(request,response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
return flag;
}else{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -