教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 文秘资料 >

利用SpringAOP管理权限

来源:网络收集 时间:2026-09-05
导读: 本书以一个小项目的方式来演示了怎样通过SPRING的AOP面向切面编程来实现用户的权限管理 利用spring AOP做的管理权限简单实例; 首先定义一个用户: Java代码 public class User { private String username; public String getUsername() { return username; }

本书以一个小项目的方式来演示了怎样通过SPRING的AOP面向切面编程来实现用户的权限管理

利用spring AOP做的管理权限简单实例;

首先定义一个用户:

Java代码

public class User {

private String username;

public String getUsername() {

return username;

}

public void setUsername(String username) {

http://www.77cn.com.cnername = username;

}

}

public class User {

private String username;

public String getUsername() {

return username;

}

public void setUsername(String username) {

http://www.77cn.com.cnername = username;

}

}

用户有三种人:未注册用户,注册用户,与管理员

注册用户可以可以发表,回复帖子

管理员除了可以发表,回复帖子,还可以删除帖子!

下面定义TestCommunity接口:

Java代码

public interface TestCommunity {

public void answerTopic();

public void deleteTopic();

}

public interface TestCommunity {

public void answerTopic();

public void deleteTopic();

}

实现上面接口的TestCommunityImpl类:

Java代码

public class TestCommunityImpl implements TestCommunity {

//注册用户与管理员拥有的功能

public void answerTopic() {

System.out.println("可以发表,回复帖子");

}

//管理员拥有的功能

public void deleteTopic() {

System.out.println("可以删除帖子!");

}

}

public class TestCommunityImpl implements TestCommunity {

//注册用户与管理员拥有的功能

public void answerTopic() {

本书以一个小项目的方式来演示了怎样通过SPRING的AOP面向切面编程来实现用户的权限管理

System.out.println("可以发表,回复帖子");

}

//管理员拥有的功能

public void deleteTopic() {

System.out.println("可以删除帖子!");

}

}

下一步,建立一下依赖注入的实现类TestResultImpl:

Java代码

public class TestResultImpl {

private TestCommunity test;

public void setTest(TestCommunity test) {

this.test = test;

}

public void answerTopic() {

test.answerTopic();

}

public void deleteTopic() {

test.deleteTopic();

}

}

public class TestResultImpl {

private TestCommunity test;

public void setTest(TestCommunity test) {

this.test = test;

}

public void answerTopic() {

test.answerTopic();

}

public void deleteTopic() {

test.deleteTopic();

}

}

接下来就是最重要的一个类,拦截器Around处理类型的类TestAuthorityInterceptor:

Java代码

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

//创建Around处理应该实现MethodInterceptor接口

public class TestAuthorityInterceptor implements MethodInterceptor {

private User user; public User getUser() {

return user;

}

public void setUser(User user) {

http://www.77cn.com.cner = user;

}

// invoke方法返回调用的结果

public Object invoke(MethodInvocation invocation) throws Throwable {

String methodName = invocation.getMethod().getName();

本书以一个小项目的方式来演示了怎样通过SPRING的AOP面向切面编程来实现用户的权限管理

if (user.getUsername().equals("unRegistedUser")) {

System.out.println("你的身份是未注册用户,没有权限回复,删除帖子!");

return null;

}

if ((user.getUsername().equals("user")) && (methodName.equals("deleteTopic"))) {

System.out.println("你的身份是注册用户,没有权限删除帖子");

return null;

}

// proceed()方法对连接点的整个拦截器链起作用,拦截器链中的每个拦截器都执行该方法,并返回它的返回值

return invocation.proceed();

}

}

配置文件:

Java代码

org.test.lighter.TestCommunity TestAuthorityInterceptor

org.test.lighter.TestCommunity TestAuthorityInterceptor

再写一个执行文件BeanTest:

Java代码

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext; public class BeanTest {

public static void main(String[] args) throws Exception {

ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml"); TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");

test.answerTopic();

test.deleteTopic();

}

}

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext; public class BeanTest {

public static void main(String[] args) throws Exception {

ApplicationContext ctx = new FileSystemXmlApplicationContext("src/bean.xml"); TestResultImpl test = (TestResultImpl)ctx.getBean("testResult");

test.answerTopic();

test.deleteTopic();

}

}

执行结果:大家猜一下啦

Java代码

1、如果是管理员,打印出: 可以发表,回复帖子 可以删除帖子!

2、如果是注册用户: 可以发表,回复帖子 你的身份是注册用户,没有权限删除帖子

3、未注册用户: 你的身份是未注册用户,没有权限回复,删除帖子!

…… 此处隐藏:1861字,全部文档内容请下载后查看。喜欢就下载吧 ……
利用SpringAOP管理权限.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/735637.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)