Spring AOP 权限

本文介绍了一个基于Spring AOP的权限控制系统实现。系统通过定义Service接口及其实现类,结合Action来触发权限检查,并利用AuthorityInterceptor拦截器进行权限验证。根据不同用户身份限制其对数据的查看和修改操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Service

package org.zbq.authority;

public interface Service {
	public void view();
	public void modify();
}
ServiceImpl

package org.zbq.authority;

public class ServiceImpl implements Service {

	@Override
	public void view() {
		System.out.println("用户查看数据");
	}

	@Override
	public void modify() {
		System.out.println("用户修改数据");
	}

}
Action
package org.zbq.authority;

public interface Action {
	public void modify();
	public void view();
}
ActionImpl
package org.zbq.authority;

public class ActionImpl implements Action {
	private Service s;
	
	public void setS(Service s) {
		this.s = s;
	}

	@Override
	public void modify() {
		s.modify();
	}

	@Override
	public void view() {
		s.view();
	}

}
AuthorityInterceptor

package org.zbq.authority;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AuthorityInterceptor implements MethodInterceptor {
	private String user;
	
	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("=================");
		String methodName = invocation.getMethod().getName();
		
		if(! "admin".equals(user) && ! "abc".equals(user)){
			System.out.println("您没有该权限");
			return null;
		}else if("abc".equals(user) && "modify".equals(methodName)){
			System.out.println("您没有管理员权限");
			return null;
		}
		
		return invocation.proceed();
	}

}
Client
package org.zbq.authority;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Client {

	public static void main(String[] args) {
		ClassPathResource cpr = new ClassPathResource("authority.xml");
		XmlBeanFactory factory = new XmlBeanFactory(cpr);
		
		Action action = (Action) factory.getBean("action");
		action.view();
		action.modify();
	}

}
authority.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="serviceTarget" class="org.zbq.authority.ServiceImpl"/>
    
    <bean id="authorityInterceptor" class="org.zbq.authority.AuthorityInterceptor">
    	<property name="user">
    		<value>a</value>
    	</property>
    </bean>
    
    <bean id="service"
    	class="org.springframework.aop.framework.ProxyFactoryBean">
    	<property name="proxyInterfaces">
    		<value>org.zbq.authority.Service</value>
    	</property>
    	
    	<property name="target">
    		<ref local="serviceTarget"/>
    	</property>
    	
    	<property name="interceptorNames">
    		<list>
    			<value>authorityInterceptor</value>
    		</list>
    	</property>
    </bean>
    
    <bean id="action" class="org.zbq.authority.ActionImpl">
    	<property name="s">
    		<ref local="service"/>
    	</property>
    </bean>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值