https://blog.csdn.net/paul342/article/details/51436565

今天结合Java的Annotation和Struts2进行注解拦截器权限控制。

功能需求:添加、查找、删除三个功能,添加、查找功能需进行权限拦截判断,删除功能则不需进行权限拦截判断。

操作流程如下:客户未登录或登录已超时,提示“客户还没登陆或登陆已超时!!!”,终止执行,然后跳转到某页面;否则继续往下执行。

以下模拟案例大概实现如上需求,接下来废话少说,直接copy代码

项目地址

1、项目目录结构

2、权限控制注解类Authority.java

  1. package com.ljq.action;
  2.  
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7.  
  8. /**
  9. * 用于识别在进行action调用的时候,标注该方法调用是否需要权限控制,需要什么样的权限的注解类。
  10. *
  11. * 该注解类一般会包括两个属性,一个是需要的权限,一个是对应的action。
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. //表示在什么级别保存该注解信息
  17. @Retention(RetentionPolicy.RUNTIME)
  18. //表示该注解用于什么地方
  19. @Target(ElementType.METHOD)
  20. public @interface Authority {
  21. String actionName();
  22. String privilege();
  23. }

3、权限拦截器类AuthorityInterceptor.java

  1. package com.ljq.action;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.Date;
  5.  
  6. import org.apache.struts2.ServletActionContext;
  7.  
  8. import com.opensymphony.xwork2.ActionInvocation;
  9. import com.opensymphony.xwork2.interceptor.Interceptor;
  10.  
  11. /**
  12. * 用于拦截请求判断是否拥有权限的拦截器
  13. *
  14. * @author Administrator
  15. *
  16. */
  17. @SuppressWarnings("serial")
  18. public class AuthorityInterceptor implements Interceptor{
  19.  
  20. public void destroy() {
  21.  
  22. }
  23.  
  24. public void init() {
  25.  
  26. }
  27.  
  28. public String intercept(ActionInvocation actionInvocation) throws Exception {
  29. String methodName=actionInvocation.getProxy().getMethod();
  30. Method currentMethod=actionInvocation.getAction()
  31. .getClass().getMethod(methodName, null);
  32.  
  33. //1、判断客户是否登陆
  34.  
  35. //从session获取当前客户信息
  36. Employee employee=(Employee)ServletActionContext
  37. .getRequest().getSession().getAttribute("employee");
  38. if(employee==null){
  39. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  40. System.out.println("客户还没登陆或登陆已超时!!!");
  41. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  42. System.out.println();
  43. return "index";
  44. }
  45.  
  46. //2、进行权限控制判断
  47.  
  48. //如果该请求方法是需要进行验证的则需执行以下逻辑
  49. if(currentMethod.isAnnotationPresent(Authority.class)){
  50. //获取权限校验的注解
  51. Authority authority=currentMethod.getAnnotation(Authority.class);
  52. //获取当前请求的注解的actionName
  53. String actionName=authority.actionName();
  54. //获取当前请求需要的权限
  55. String privilege=authority.privilege();
  56.  
  57. //可以在此判断当前客户是否拥有对应的权限,如果没有可以跳到指定的无权限提示页面,如果拥有则可以继续往下执行。
  58.  
  59. //if(拥有对应的权限){
  60. // return actionInvocation.invoke();
  61. //}else{
  62. // return "无权限";
  63. //}
  64.  
  65. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  66. System.out.println("客户" + employee.getUserName() + "在" + new Date() + "执行了" + actionName+"方法,拥有"+privilege+"权限!!");
  67. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  68. System.out.println();
  69. return actionInvocation.invoke();
  70. }
  71.  
  72. //3、进行非权限控制判断
  73.  
  74. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  75. System.out.println("我执行了没有??");
  76. System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  77. return "index";
  78. }
  79.  
  80. }

4、客户信息类Employee.java

  1. package com.ljq.action;
  2.  
  3. import java.io.Serializable;
  4.  
  5. @SuppressWarnings("serial")
  6. public class Employee implements Serializable {
  7.  
  8. private Integer id;
  9. private String userName;
  10. private String pwd;
  11.  
  12. public Employee() {
  13. }
  14.  
  15. public Integer getId() {
  16. return id;
  17. }
  18.  
  19. public void setId(Integer id) {
  20. this.id = id;
  21. }
  22.  
  23. public String getUserName() {
  24. return userName;
  25. }
  26.  
  27. public void setUserName(String userName) {
  28. this.userName = userName;
  29. }
  30.  
  31. public String getPwd() {
  32. return pwd;
  33. }
  34.  
  35. public void setPwd(String pwd) {
  36. this.pwd = pwd;
  37. }
  38.  
  39. }

5、action类EmployeeAction

  1. package com.ljq.action;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4.  
  5. @SuppressWarnings("serial")
  6. public class EmployeeAction extends ActionSupport{
  7.  
  8. /**
  9. * 添加
  10. *
  11. * 请求该方法需要拥有对test的add权限,会通过拦截器拦截
  12. *
  13. * @return
  14. */
  15. @Authority(actionName="test", privilege="add")
  16. public String add(){
  17. System.out.println("执行了add方法!!!");
  18. return SUCCESS;
  19. }
  20.  
  21. /**
  22. * 查找
  23. *
  24. * 请求该方法的时候需要拥有对test的find权限,会通过拦截器拦截
  25. *
  26. * @return
  27. * @throws Exception
  28. */
  29. @Authority(actionName="test", privilege="find")
  30. public String find() throws Exception {
  31. System.out.println("执行了find方法!!!");
  32. return SUCCESS;
  33. }
  34.  
  35. /**
  36. * 删除
  37. *
  38. * 不会通过拦截器拦截,因为没对actionName进行权限配置
  39. *
  40. * @return
  41. * @throws Exception
  42. */
  43. public String delete() throws Exception {
  44. System.out.println("执行了delete方法!!!");
  45. return SUCCESS;
  46. }
  47.  
  48. }
 

6、首页index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@taglib uri="/struts-tags" prefix="s"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12.  
  13. <title>My JSP 'index.jsp' starting page</title>
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22. </head>
  23.  
  24. <body>
  25. 欢迎您的到来....
  26. </body>
  27. </html>

7、登录页login.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@page import="com.ljq.action.Employee"%>
  3. <%
  4. Employee employee=new Employee();
  5. employee.setId(1);
  6. employee.setUserName("jiqinlin");
  7. employee.setPwd("123456");
  8. request.getSession().setAttribute("employee", employee);
  9. %>

8、struts2配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7. <constant name="struts.serve.static.browserCache" value="false"/>
  8. <constant name="struts.action.extension" value="do"/>
  9. <constant name="struts.i18n.encoding" value="UTF-8"/>
  10.  
  11. <package name="base" extends="struts-default">
  12. <global-results>
  13. <result name="index">/index.jsp</result>
  14. <result name="success">/login.jsp</result>
  15. </global-results>
  16. </package>
  17.  
  18. <!-- 自定义拦截器 -->
  19. <package name="permissionInterceptor"
  20. namespace="/permissionInterceptor" extends="base">
  21. <interceptors>
  22. <!-- 注册自定义的权限控制拦截器 -->
  23. <interceptor name="authorityInterceptor" class="com.ljq.action.AuthorityInterceptor"/>
  24.  
  25. <!-- 把自定义的权限控制拦截器和默认的拦截器栈加到新的自定义的拦截器栈 -->
  26. <interceptor-stack name="myInterceptors">
  27. <interceptor-ref name="defaultStack"/>
  28. <interceptor-ref name="authorityInterceptor"/>
  29. </interceptor-stack>
  30. </interceptors>
  31. <!-- 指定新的自定义的拦截器栈为默认的拦截器栈,这样自定义的权限控制拦截器就可以发挥作用了 -->
  32. <default-interceptor-ref name="myInterceptors"/>
  33. </package>
  34.  
  35. <package name="employee" extends="permissionInterceptor">
  36. <action name="*Employee" class="com.ljq.action.EmployeeAction" method="{1}">
  37. </action>
  38. </package>
  39.  
  40. </struts>

web.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6.  
  7. <filter>
  8. <filter-name>struts2</filter-name>
  9. <filter-class>
  10. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  11. </filter-class>
  12. </filter>
  13.  
  14. <filter-mapping>
  15. <filter-name>struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18.  
  19. </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拦截器加自定义注解实现权限控制的更多相关文章

  1. SpringMVC拦截器+Spring自定义注解实现权限验证

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. 从struts2拦截器到自定义拦截器

    拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.xml的配置中,package内直接或间接继承了struts-default.xml,这样st ...

  3. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  4. 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制

    权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...

  5. [Java]利用拦截器和自定义注解做登录以及权限验证

    1.自定义注解 需要验证登录的注解 package com.etaofinance.wap.common; import java.lang.annotation.Documented; import ...

  6. 拦截器和自定义注解@interface

    1 .拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在Handler执行之前或之后加入某些操作,其实就是AOP的一种实现策略. 拦截用户的请求并进行相应的处理,比如:判断用 ...

  7. Spring MVC基础知识整理➣拦截器和自定义注解

    概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...

  8. SpringBoot 拦截器和自定义注解判断请求是否合法

    应用场景举例: 当不同身份的用户请求一个接口时,用来校验用户某些身份,这样可以对单个字段数据进行精确权限控制,具体看代码注释 自定义注解 /** * 对比请求的用户身份是否符合 * @author l ...

  9. 6、Struts2拦截器实现权限控制

    1.创建如下项目结果 2.在com.entity包下创建 package com.entity; public class User { private String name; private St ...

随机推荐

  1. IIR型高斯滤波的原理及实现

    二.实现 GIMP中有IIR型高斯滤波的实现,代码位于contrast-retinex.c中,读者可自行查看.下面给出本人实现的核心代码: #include"stdafx.h" t ...

  2. Oracle历史版本及oracle相关软件下载地址

    网站:https://edelivery.oracle.com/ 可能需要注册个账号!!!(账号注册登录自己折腾下就好了) 下载数据库或者oracle的相关软件的话,如下 选择对应的下载即可!

  3. neo4j(图数据库)是什么?

    不多说,直接上干货! 作为一款强健的,可伸缩的高性能数据库,Neo4j最适合完整的企业部署或者用于一个轻量级项目中完整服务器的一个子集存在. 它包括如下几个显著特点: 完整的ACID支持 高可用性 轻 ...

  4. js删除最后一个字符

    在最近做一个系统,使用socket来完成后台操作,C#来完成前端操作.但是在定的协议里面,一定要用某个符号来表示传的数据结束.后台进行交互时,获取到的数据必须进行删除最后一个字符的操作. 比如我们协议 ...

  5. 05.NopCommerce给Topic表添加排序及类别字段

    在用到Nopcommerce中静态页面表时,发现Topic表没有排序字段和类别字段,导致如果Page文件很多的话,无法区分是哪个类别,为此我稍微扩展了一下字段,在此记录一下操作流程,方便以后自己查看, ...

  6. Jquery 中使用String.Format

    第一种方法: String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; ...

  7. 高阶函数之filter 和 sorted

    filter函数 接受一个函数和序列,把传入的函数依次作用于每个序列,然后根据返回值时True还是False保留或舍弃元素. def func(n): if n%2 == 0: return n m ...

  8. checkbox:click事件触发span元素内容改变

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. SIRI课程表

    wen 周一@0@{今天没课哦}周一 周二今天共一节课 第2节,可编程控制器应用,11号教学楼1 0 3房间 周二 周三今天共三节课 第二节,过程控制系统,2号楼2 1 0房间,第三节机械制造技术11 ...

  10. centos启用socks5服务

    直接在终端用 root 安装 *** 官方客户端 apt-get install python-pip -ypip install shadowsocks 然后编辑 /etc/shadowsocks. ...