今天结合Java的Annotation和Struts2进行注解拦截器权限控制。
功能需求:添加、查找、删除三个功能,添加、查找功能需进行权限拦截判断,删除功能则不需进行权限拦截判断。
操作流程如下:客户未登录或登录已超时,提示“客户还没登陆或登陆已超时!!!”,终止执行,然后跳转到某页面;否则继续往下执行。
以下模拟案例大概实现如上需求,接下来废话少说,直接copy代码
1、项目目录结构
2、权限控制注解类Authority.java
package com.ljq.action;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 用于识别在进行action调用的时候,标注该方法调用是否需要权限控制,需要什么样的权限的注解类。
*
* 该注解类一般会包括两个属性,一个是需要的权限,一个是对应的action。
*
* @author Administrator
*
*/
//表示在什么级别保存该注解信息
@Retention(RetentionPolicy.RUNTIME)
//表示该注解用于什么地方
@Target(ElementType.METHOD)
public @interface Authority {
String actionName();
String privilege();
}
3、权限拦截器类AuthorityInterceptor.java
package com.ljq.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 Administrator
*
*/
@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);
//1、判断客户是否登陆
//从session获取当前客户信息
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";
}
//2、进行权限控制判断
//如果该请求方法是需要进行验证的则需执行以下逻辑
if(currentMethod.isAnnotationPresent(Authority.class)){
//获取权限校验的注解
Authority authority=currentMethod.getAnnotation(Authority.class);
//获取当前请求的注解的actionName
String actionName=authority.actionName();
//获取当前请求需要的权限
String privilege=authority.privilege();
//可以在此判断当前客户是否拥有对应的权限,如果没有可以跳到指定的无权限提示页面,如果拥有则可以继续往下执行。
//if(拥有对应的权限){
// return actionInvocation.invoke();
//}else{
// return "无权限";
//}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("客户" + employee.getUserName() + "在" + new Date() + "执行了" + actionName+"方法,拥有"+privilege+"权限!!");
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println();
return actionInvocation.invoke();
}
//3、进行非权限控制判断
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("我执行了没有??");
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
return "index";
}
}
4、客户信息类Employee.java
package com.ljq.action;
import java.io.Serializable;
@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、action类EmployeeAction
package com.ljq.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class EmployeeAction extends ActionSupport{
/**
* 添加
*
* 请求该方法需要拥有对test的add权限,会通过拦截器拦截
*
* @return
*/
@Authority(actionName="test", privilege="add")
public String add(){
System.out.println("执行了add方法!!!");
return SUCCESS;
}
/**
* 查找
*
* 请求该方法的时候需要拥有对test的find权限,会通过拦截器拦截
*
* @return
* @throws Exception
*/
@Authority(actionName="test", privilege="find")
public String find() throws Exception {
System.out.println("执行了find方法!!!");
return SUCCESS;
}
/**
* 删除
*
* 不会通过拦截器拦截,因为没对actionName进行权限配置
*
* @return
* @throws Exception
*/
public String delete() throws Exception {
System.out.println("执行了delete方法!!!");
return SUCCESS;
}
}
6、首页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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
欢迎您的到来....
</body>
</html>
7、登录页login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.ljq.action.Employee"%>
<%
Employee employee=new Employee();
employee.setId(1);
employee.setUserName("jiqinlin");
employee.setPwd("123456");
request.getSession().setAttribute("employee", employee);
%>
客户已经登录
8、struts2配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.serve.static.browserCache" value="false"/>
<constant name="struts.action.extension" value="do"/>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="base" extends="struts-default">
<global-results>
<result name="index">/index.jsp</result>
<result name="success">/login.jsp</result>
</global-results>
</package>
<!-- 自定义拦截器 -->
<package name="permissionInterceptor"
namespace="/permissionInterceptor" extends="base">
<interceptors>
<!-- 注册自定义的权限控制拦截器 -->
<interceptor name="authorityInterceptor" class="com.ljq.action.AuthorityInterceptor"/>
<!-- 把自定义的权限控制拦截器和默认的拦截器栈加到新的自定义的拦截器栈 -->
<interceptor-stack name="myInterceptors">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authorityInterceptor"/>
</interceptor-stack>
</interceptors>
<!-- 指定新的自定义的拦截器栈为默认的拦截器栈,这样自定义的权限控制拦截器就可以发挥作用了 -->
<default-interceptor-ref name="myInterceptors"/>
</package>
<package name="employee" extends="permissionInterceptor">
<action name="*Employee" class="com.ljq.action.EmployeeAction" method="{1}">
</action>
</package>
</struts>
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
跟踪控制台打印的信息
1、未登录,访问查找功能:http://localhost:8083/struts2_authority_interceptor/addEmployee.do
2、已登录,访问添加功能:http://localhost:8083/struts2_authority_interceptor/login.jsp
http://localhost:8083/struts2_authority_interceptor/addEmployee.do
已登录,访问查找功能:http://localhost:8083/struts2_authority_interceptor/login.jsp
http://localhost:8083/struts2_authority_interceptor/findEmployee.do
3、已登录,访问删除功能
已登录,访问查找功能:http://localhost:8083/struts2_authority_interceptor/login.jsp
http://localhost:8083/struts2_authority_interceptor/deleteEmployee.do
完毕!!
分享到:
相关推荐
反射和注解的妙用-struts2权限的控制
本系统是基于Struts+Hibernate开发的一套权限后台管理系统,功能包含完善的权限管理,和信息发布功能。开发环境: <br>Eclipse3.01+myeclipse3.84+mysql5.0(oracle、ms sqlserver2000)。 <br>压缩包里包含了...
spring3 struts2 Mybatis3 组件注解 事务注解 管理系统框架实例 CREATE DATABASE `gszsk` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; CREATE TABLE `user_admin` ( `id` int(10) unsigned NOT NULL AUTO_...
十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...
(1)该项目是基于spring3+struts2+hibernate3+spring security3的权限管理项目 (2)后台我已经实现了权限管理,包括用户,角色和资源的分配。前台实现了spring security3的管理 (3)网上案例普遍是后台单一登陆。...
struts 拦截器实现权限拦截,例子简单易懂,有详细的注释。
1、shiro+strut2 2、shiro注解 + struts2注解 2、shiro 缓存:因为使用了缓存,第一使用admin登录,如果没有logout,再用test登录,则test具有admin的权限
1,shiro+hibernate4+struts2+spring+easyui+fusioncharts部分bootstrap样式全注解零配置简单权限管理web项目 2,简单shiro权限管控 细度到按钮 3,安装方法按config.properties参数配置你的数据库 4,运行dbsql.sql 5,...
前台页面展示用zTree插件,后台数据用XML保存,利用Dom4j进行解析,涉及到了对XML的增、删、改等等特别需求的技术,并用struts2+spring进行整合项目,源代码中有非常完整的注释,导入myEclipse部署运行,即可从...
Struts2+Hibernate3+Spring3+Spring Security3(jar包) 支持邮件、权限过滤、注解等包,本包在开发中都是经常用到的
本Java JSP项目源码是一个基于Hibernate 3、Struts 2和Spring 2构建的内容管理系统(CMS),具有灵活、可扩展且易于维护的特点。它适用于各种规模的企业或组织,用于管理和发布各类在线内容,如新闻、博客、产品信息...
用struts2写的权限,实例,里面有注释,增删该查都有
含有配置文件,同时代码有详细的注释、合理的分层。本源码是JavaWeb图书馆管理系统,采用Struts框架、java、jsp、servlet。 内部有以下模块:↓↓↓↓↓↓↓↓ 用户登录图书馆管理系统后,可看到图书借阅排行榜,...
Hibernate4.2.11(注解配置) 前端:JQuery-EasyUI1.3.1 Javascript JQuery Ajax 数据库:Oracle10g 网络服务器:Tomcat 7 构建工具:Maven Linux:CentoOS 6.4 其他:JFreeChart POI C3P0 JUnit Log4j Jackson ...
系统控制器、业务层、数据层采用spring注解方式,hibernate事务注入,c3p0数据库连接池等。又实现了权限拦截器、项目监听器、单元测试等功能。 页面简洁大气美观,系统耦合度小,可配置度高,几乎包含了全部常用...
SSH框架整合,全jpa注解真正的零配置。 struts2.3.8 spring3.2 hibernate4.1.9. 带有分页,全局异常,权限拦截器,session过滤等功能。 内有jar包。导入开发工具后部署既可跑起来。已做了简单的数据库操作功能。供...
SSH框架整合,全jpa注解真正的零配置。 struts2.3.8 spring3.2 hibernate4.1.9. 带有分页,全局异常,权限拦截器,session过滤等功能。 内有jar包。导入开发工具后部署既可跑起来。已做了简单的数据库操作功能。供...
spring+struts2+hibernate+ajax。 使用oracle数据库。 B/S结构。 包含个人信息、邮件管理、考勤管理、权限管理四大模块。 jsp表单全struts2标签。 邮件管理包含附件上传和下载功能,上传文件部分经过了精心设计,如...
spring+struts2+hibernate+ajax。 使用oracle数据库。 B/S结构。 包含个人信息、邮件管理、考勤管理、权限管理四大模块。 jsp表单全struts2标签。 邮件管理包含附件上传和下载功能,上传文件部分经过了精心设计,如...