博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
maven 架设 struts2 注解方式 权限控制
阅读量:4079 次
发布时间:2019-05-25

本文共 6140 字,大约阅读时间需要 20 分钟。

1.目录结构

2.Authority.java

package cn.sigangjun.action;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 用于识别在进行action调用的时候,标注该方法调用是否需要权限控制,需要什么样的权限的注解类。 *  * 该注解类一般会包括两个属性,一个是需要的权限,一个是对应的action。 *  * @author sigangjun *  */// 表示在什么级别保存该注解信息@Retention(RetentionPolicy.RUNTIME)// 表示该注解用于什么地方@Target(ElementType.METHOD)public @interface Authority {	String actionName();	String privilege();}

3.AuthorityInterceptor.java

package cn.sigangjun.action;import java.lang.reflect.Method;import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;/** * 用于拦截请求判断是否拥有权限的拦截器 *  * @author sigangjun *  */@SuppressWarnings("serial")public class AuthorityInterceptor implements Interceptor {	public void destroy() {}	public void init() {}	public String intercept(ActionInvocation actionInvocation) throws Exception {		String methodName = actionInvocation.getProxy().getMethod();		Method currentMethod = actionInvocation.getAction().getClass().getMethod(methodName, null);		// 如果该请求方法是需要进行验证的则需执行以下逻辑		if (currentMethod.isAnnotationPresent(Authority.class)) {			// 获取权限校验的注解			Authority authority = currentMethod.getAnnotation(Authority.class);			// 获取当前请求的注解的actionName			String actionName = authority.actionName();			// 获取当前请求需要的权限			String privilege = authority.privilege();						//1、判断客户是否登陆			Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");			if (employee == null) {				System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");				System.out.println("客户还没登陆或登陆已超时!!!无权限访问!");				System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");				System.out.println();				return "index";			}			System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");			System.out.println("客户" + employee.getUserName() + "在" + new Date() + "执行了" + actionName + "方法,拥有" + privilege + "权限!!");			System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");		}				return actionInvocation.invoke();	}}

4.Employee.java

package cn.sigangjun.action;import java.io.Serializable;/** * @author sigangjun * */@SuppressWarnings("serial")public class Employee implements Serializable {	private Integer id;	private String userName;	private String pwd;	public Employee() {	}	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getUserName() {		return userName;	}	public void setUserName(String userName) {		this.userName = userName;	}	public String getPwd() {		return pwd;	}	public void setPwd(String pwd) {		this.pwd = pwd;	}}

5.EmployeeAction

package cn.sigangjun.action;import com.opensymphony.xwork2.ActionSupport;/** * @author sigangjun * */@SuppressWarnings("serial")public class EmployeeAction extends ActionSupport {	/**	 * 请求该方法需要拥有对test的add权限,会通过拦截器拦截	 */	@Authority(actionName = "test", privilege = "add")	public String add() {		System.out.println("执行了add方法!!!");		return SUCCESS;	}	/**	 * 请求该方法的时候需要拥有对test的find权限,会通过拦截器拦截	 */	@Authority(actionName = "test", privilege = "find")	public String find() throws Exception {		System.out.println("执行了find方法!!!");		return SUCCESS;	}	/**	 * 不会通过拦截器拦截,因为没对actionName进行权限配置	 */	public String delete() throws Exception {		System.out.println("执行了delete方法!!!");		return SUCCESS;	}}

6.struts.xml

/index.jsp
/login.jsp

7.web.xml

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*

8.pom.xml

4.0.0
cn.sigangjun
03struts2
war
0.0.1-SNAPSHOT
03struts2 Maven Webapp
http://maven.apache.org
org.apache.struts
struts2-core
2.3.4.1
org.apache.struts
struts2-convention-plugin
2.3.4.1
junit
junit
4.10
test
log4j
log4j
1.2.17
03struts2

9.login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="cn.sigangjun.action.*"%><%    Employee employee=new Employee();    employee.setId(1);    employee.setUserName("sigangjun");    employee.setPwd("123456");    request.getSession().setAttribute("employee", employee);%>客户已经登录

10.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
欢迎您的到来....
你可能感兴趣的文章
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
为什么很多程序员都选择跳槽?
查看>>
医疗行业运用企业云盘可以带来什么样的提升
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成(一)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 单机部署(二)
查看>>
C#控件集DotNetBar安装及破解
查看>>
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>
mongodb管理与安全认证
查看>>
nodejs内存控制
查看>>
MongoDB 数据文件备份与恢复
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>