import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.shizongger.oa.domain.User;
public class CheckPrivilegeInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("---拦截器未拦截之前---");
String result = invocation.invoke();
System.out.println("---拦截器拦截之后---");
return result;
}
}
<!-- 配置拦截器 -->
<interceptors>
<!-- 声明拦截器 -->
<interceptor name="checkPrivilege" class="com.shizongger.oa.util.CheckPrivilegeInterceptor"></interceptor>
<!-- 重新定义默认的拦截器栈 -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="checkPrivilege"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
public class MyServletContextListener implements ServletContextListener {
Log log = LogFactory.getLog(this.getClass());
@Autowired
private PrivilegeService privilegeService;
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.debug("---销毁监听器---");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");
List<Privilege> topPrivilegeList = privilegeService.findTopList();
//将权限list放到比application作用域还大的ServletContext
sc.setAttribute("topPrivilegeList", topPrivilegeList);
// 准备数据:allPrivilegeUrls
Collection<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls();
sc.setAttribute("allPrivilegeUrls", allPrivilegeUrls);
}
}
public String intercept(ActionInvocation invocation) throws Exception {
//获取信息,从session中取出当前登录用户
User user = (User) ActionContext.getContext().getSession().get("user");
String nameSpace = invocation.getProxy().getNamespace();
String actionName = invocation.getProxy().getActionName();
//对应的权限地址
String privilegeUrl = nameSpace + actionName;
//如果未登录
if(user == null) {
//如果是去登录的页面和登录请求,就放行
if("/user_login".equals(privilegeUrl)) {
return invocation.invoke();
//否则跳转到登录页面
} else {
return "loginUI";
}
} else {
//如果已经登录则判断是否有权限
if(user.hasPrivilegeByUrl(privilegeUrl)) {
return invocation.invoke();
} else {
return "noPrivilegeError";
}
}
}
/**
* 用户实体
* @author shizongger
* @date 2017/03/24
*/
public class User {
private Log log = LogFactory.getLog(this.getClass());
private Long id;
private String loginName;
private String password;
private String name;
private String gender;
private String phoneNumber;
private String email;
private String description;
private Department department;
private Set<Role> roles;
//getter/settter方法
/**
* 判断用户是否用该权限
* @param privilegename 权限名称
* @return
*/
public boolean hasPrivilegeByName(String privilegeName) {
log.debug("权限名称:" + privilegeName);
//从本用户中取出所有角色
for(Role role : roles) {
//从角色遍历出所有权限
Set<Privilege> privilegeList = role.getPrivileges();
for(Privilege privilege : privilegeList) {
if(privilegeName.equals(privilege.getName())) {
log.debug(privilegeName + "---有权限---");
return true;
}
}
}
log.debug(privilegeName + "---没有权限---");
return false;
}
/**
* 判断本用户是否有指定URL的权限
*
* @param privUrl
* @return
*/
public boolean hasPrivilegeByUrl(String privUrl) {
// 超级管理有所有的权限
if (isAdmin()) {
return true;
}
// >> 去掉后面的参数
int pos = privUrl.indexOf("?");
if (pos > -1) {
privUrl = privUrl.substring(0, pos);
}
// >> 去掉UI后缀
if (privUrl.endsWith("UI")) {
privUrl = privUrl.substring(0, privUrl.length() - 2);
}
// 如果本URL不需要控制,则登录用户就可以使用
Collection<String> allPrivilegeUrls = (Collection<String>) ActionContext.getContext().getApplication().get("allPrivilegeUrls");
if (!allPrivilegeUrls.contains(privUrl)) {
return true;
} else {
// 普通用户要判断是否含有这个权限
for (Role role : roles) {
for (Privilege priv : role.getPrivileges()) {
if (privUrl.equals(priv.getUrl())) {
return true;
}
}
}
return false;
}
}
/**
* 判断本用户是否是超级管理员
*
* @return
*/
public boolean isAdmin() {
return "admin".equals(loginName);
}
}
@Service
public class PrivilegeServiceImpl extends DaoSupportImpl<Privilege> implements PrivilegeService {
@Override
@Transactional
public List<Privilege> findTopList() {
List<Privilege> topPrivletList = this.getSession()
.createQuery("FROM Privilege p WHERE p.parent IS NULL")
.list();
return topPrivletList;
}
@Override
@Transactional
public Collection<String> getAllPrivilegeUrls() {
return getSession().createQuery(//
"SELECT DISTINCT p.url FROM Privilege p WHERE p.url IS NOT NULL")//
.list();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有