Model 就不给了,直接给业务逻辑。
beas.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.vti"></context:component-scan> <bean id="articleService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.vti.service.ArticleService</value> </property> <property name="target" ref="authTarget"/> <property name="interceptorNames"> <list> <value>authority</value> </list> </property> </bean> </beans>
daoimpl
package com.vti.dao.impl; import org.springframework.stereotype.Component; import com.vti.dao.ArticleDao; import com.vti.model.Article; @Component(value="articleDao") public class ArticleDaoImpl implements ArticleDao{ @Override public void addArticle(Article article) { System.out.println("文章添加成功!"); } @Override public void delArticle(Article article) { System.out.println("文章删除成功!"); } @Override public void getArticle() { System.out.println("阅读文章成功"); } }
artcleServiceImpl
package com.vti.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.vti.dao.ArticleDao; import com.vti.model.Article; import com.vti.service.ArticleService; @Component(value="authTarget") public class ArticleServiceImpl implements ArticleService{ private ArticleDao articleDao; public ArticleDao getArticleDao() { return articleDao; } @Resource public void setArticleDao(ArticleDao articleDao) { this.articleDao = articleDao; } @Override public void addArticle(Article article) { articleDao.addArticle(article); } @Override public void delArticle(Article article) { articleDao.delArticle(article); } @Override public void getArticle() { articleDao.getArticle(); } }
AuthorityInterceptor
package com.vti.aop; import javax.annotation.Resource; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.stereotype.Component; import com.vti.proxy.UserProxy; @Component(value="authority") public class AuthorityInterceptor implements MethodInterceptor{ private UserProxy proxy; public UserProxy getProxy() { return proxy; } @Resource public void setProxy(UserProxy proxy) { this.proxy = proxy; } @Override public Object invoke(MethodInvocation invocation) throws Throwable { String methodName = invocation.getMethod().getName(); int authority=proxy.getUser().getAuthority(); if (authority==1){//普通用户 if (methodName.equals("getArticle")) { return invocation.proceed(); }else { System.out.println("无权限"); return null; } }else if (authority==2){//发布用户 if (methodName.equals("getArticle")) { return invocation.proceed(); }else if (methodName.equals("addArticle")) { return invocation.proceed(); }else { System.out.println("无权限"); return null; } }else { //管理用户 return invocation.proceed(); } } }
UserProxy
package com.vti.proxy; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.vti.model.Article; import com.vti.model.User; import com.vti.service.ArticleService; @Component(value="proxy") public class UserProxy{ private User user; private ArticleService articleService; public ArticleService getArticleService() { return articleService; } @Resource public void setArticleService(ArticleService articleService) { this.articleService = articleService; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void add() { Article article=new Article(); articleService.addArticle(article); } public void del() { Article article=new Article(); articleService.delArticle(article); } public void read() { articleService.getArticle(); } }
Test
package com.vti.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.vti.model.User; import com.vti.proxy.UserProxy; public class AOPTest { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); UserProxy proxy=context.getBean(UserProxy.class); User user=new User(); user.setAuthority(1); proxy.setUser(user); proxy.del(); } }
测试结果如下:
setAuthority=1,只能阅读
setAuthority=2,能添加也能阅读
setAuthority=3.,还能删除
大家自己测试,建议熟悉了SSH的人才来做这个。
相关推荐
使用SpringAop使用Oracle数据权限控制
Spring Boot AOP权限控制模块开发,利用AOP功能完成属于自己的权限模块开发
传统的应用程序实现 J2EE容器实现 AOP下的应用程序权限控制实现
用spring aop 编写的权限验证实例代码,下载后将解压后的文件直接导入到eclipse中,直接运行测试方法即可。
本篇文章主要介绍了Java之Spring AOP 实现用户权限验证,用户登录、权限管理这些是必不可少的业务逻辑,具有一定的参考价值,有兴趣的可以了解一下。
Spring 权限 这里是一个 spring aop 实现的一个权限 包括权限设计 这个不是很能懂 包括注释
NULL 博文链接:https://panmingzhi0815.iteye.com/blog/1139504
可扩展性强:使用 Spring AOP 进行方法耗时监测,可以方便的对其他的切面进行扩展,例如:日志、异常处理、权限控制等切面。 4. 提高系统性能:通过对系统中的方法进行耗时监测,可以及时发现系统中的性能瓶颈,...
一个简单的采用自定义注解结合SpringAop实现方法执行的权限管理,这个demo中并没有涉及到与数据库的交互和业务代码,用户权限在登陆时采用简单的手动初始化。该demo用的jdk1.7编译,Spring4.0版本,只想通过这个demo...
本篇文章主要介绍了spring aop实现用户权限管理的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
内含有mybatis 拦截器实现的分页代码,spring 的事务和aop 测试、和反射工具类
AOP 采取横向抽取机制,取代了传统纵向继承体系的重复性代码,其应用主要体现在事务处理、日志管理、权限控制、异常处理等方面。 目前最流行的 AOP 框架有两个,分别为 Spring AOP 和 AspectJ。Spring AOP 使用纯 ...
主要介绍了Spring AOP实现权限检查的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
基于spring_aop_权限管理系统原型
SpringAOP与SpringMVC拦截器两种方式实现权限管控,前台xml读取配置根据登录用户判断button是否enable
springAOP指的是在spring中的AOP,什么是AOP,相对于java中的面向对象(oop),在面向对象中一些公共的行为,像日志记录,权限验证等如果都使用面向对象来做,会在每个业务方法中都写上重复的代码,造成代码的冗余。...
1、编写切面类,包含权限审核方法和日志记录方法,这两个方法将来会织入到...5、编写案例,运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。请掌握这五种通知的特点,及应用场景
Spring中的AOP可以控制权限,在我们做项目的时候,用Struts的过滤方法也可以实现。 当然这个文件只是提供测试,我们主要是学习这个AOP的思想。大家下载后可以再摸索......
主要介绍了spring aop 拦截业务方法,实现权限控制示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。