用spring Aop 做的管理权限(简单实例)
2013-10-09 23:57
337 查看
首先定义一个用户:
Java代码
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
用户有三种人:未注册用户,注册用户,与管理员
注册用户可以可以发表,回复帖子
管理员除了可以发表,回复帖子,还可以删除帖子!
下面定义TestCommunity接口:
Java代码
public interface TestCommunity {
public void answerTopic();
public void deleteTopic();
}
4000
实现上面接口的TestCommunityImpl类:
Java代码
public class TestCommunityImpl implements TestCommunity {
//注册用户与管理员拥有的功能
public void answerTopic() {
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();
}
}
接下来,就是最重要的一个类,拦截器,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) {
this.user = user;
}
// invoke方法返回调用的结果
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName();
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代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="authTarget" class="org.test.lighter.TestCommunityImpl" />
<!-- 其中的username可以写为admin,user,和unRegistedUser -->
<bean id="user" class="org.test.lighter.User">
<property name="username" value="user" />
</bean>
<!-- 配置拦截器 -->
<bean id="TestAuthorityInterceptor"
class="org.test.lighter.TestAuthorityInterceptor">
<property name="user" ref="user" />
</bean>
<!-- 配置代理工厂bean -->
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.test.lighter.TestCommunity</value>
</property>
<property name="target" ref="authTarget"/>
<property name="interceptorNames">
<list>
<value>TestAuthorityInterceptor</value>
</list>
</property>
</bean>
<bean id="testResult" class="org.test.lighter.TestResultImpl">
<property name="test" ref="service" />
</bean>
</beans>
再写一个执行文件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();
}
}
执行结果:大家猜一下啦
Java代码
1、如果是管理员,打印出:
可以发表,回复帖子
可以删除帖子!
2、如果是注册用户:
可以发表,回复帖子
你的身份是注册用户,没有权限删除帖子
3、未注册用户:
你的身份是未注册用户,没有权限回复,删除帖子!
Java代码
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
用户有三种人:未注册用户,注册用户,与管理员
注册用户可以可以发表,回复帖子
管理员除了可以发表,回复帖子,还可以删除帖子!
下面定义TestCommunity接口:
Java代码
public interface TestCommunity {
public void answerTopic();
public void deleteTopic();
}
4000
实现上面接口的TestCommunityImpl类:
Java代码
public class TestCommunityImpl implements TestCommunity {
//注册用户与管理员拥有的功能
public void answerTopic() {
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();
}
}
接下来,就是最重要的一个类,拦截器,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) {
this.user = user;
}
// invoke方法返回调用的结果
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName();
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代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="authTarget" class="org.test.lighter.TestCommunityImpl" />
<!-- 其中的username可以写为admin,user,和unRegistedUser -->
<bean id="user" class="org.test.lighter.User">
<property name="username" value="user" />
</bean>
<!-- 配置拦截器 -->
<bean id="TestAuthorityInterceptor"
class="org.test.lighter.TestAuthorityInterceptor">
<property name="user" ref="user" />
</bean>
<!-- 配置代理工厂bean -->
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.test.lighter.TestCommunity</value>
</property>
<property name="target" ref="authTarget"/>
<property name="interceptorNames">
<list>
<value>TestAuthorityInterceptor</value>
</list>
</property>
</bean>
<bean id="testResult" class="org.test.lighter.TestResultImpl">
<property name="test" ref="service" />
</bean>
</beans>
再写一个执行文件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();
}
}
执行结果:大家猜一下啦
Java代码
1、如果是管理员,打印出:
可以发表,回复帖子
可以删除帖子!
2、如果是注册用户:
可以发表,回复帖子
你的身份是注册用户,没有权限删除帖子
3、未注册用户:
你的身份是未注册用户,没有权限回复,删除帖子!
相关文章推荐
- 利用spring AOP管理权限[简单实例]
- springmvc+mybatis+shiro简单权限菜单管理实例
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- 最简单的动态代理实例(spring基于接口代理的AOP原理)
- 基于spring3.0.5 mvc 简单用户管理实例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- Spring-AOP(权限管理Demo)
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- Spring Aop简单实例
- Spring 注解版简单实例之AOP(四)
- 基于spring3.0.5 mvc 简单用户管理实例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- Spring Security3.1 最新配置实例(spring权限管理)
- spring aop实现权限管理
- 运用Spring AOP验证用户权限实例
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- spirngMVC+Hibernate+SpringSecuirty+Extjs一个简单的权限管理系统
- 利用spring AOP管理权限
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- spring boot mybatis 整合shiro简单实现登陆权限管理