shiro学习(4)-授权

本文详细介绍了使用Apache Shiro框架进行权限管理的具体实现,包括基于角色的授权和细粒度资源授权过程,以及如何通过自定义Realm进行授权。

概念

授权,又称作为访问控制,是对资源的访问管理的过程,即对于认证通过的用户,授予他可以访问某些资源的权限。

授权流程图

 

 

 

简单授权实现

在shiro-permession.ini文件中设置

[users]
#用户admin的密码是123456,此用户具有role2角色
coco=123456,role1
admin=123456,role1,role2

[roles]
#角色role1对资源user拥有create、update、delete权限
role1=user:create,user:update,user:delete
#角色role2对资源user拥有create权限
role2=user:create
#角色role3对资源user拥有select权
role3=user:select

验证角色和权限

@Test
	public void demoTree(){
		// 装入 INI 配置
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-permession.ini");
		//创建SecurityManager对象
		SecurityManager instance = factory.getInstance();
		//使SecurityManager可以访问
		SecurityUtils.setSecurityManager(instance);
		//接受提交的用户名和密码: 
		UsernamePasswordToken tooken = new UsernamePasswordToken("admin","123456");
		//获取当前主体
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(tooken);
		} catch (UnknownAccountException e) {
			System.out.println("用户名错误!");
		}
		 catch (IncorrectCredentialsException e) {
				System.out.println("密码错误!");
			}
		System.out.println("是否认证成功:" + subject.isAuthenticated());
		//粗颗粒度授权 ===> 角色验证
		System.out.println(subject.getPrincipal()+" 是否具有role1角色====> "+subject.hasRole("role1"));
		System.out.println(subject.getPrincipal()+" 是否具有role2角色====> "+subject.hasRole("role2"));
		System.out.println(subject.getPrincipal()+" 是否具有role3角色====> "+subject.hasRole("role3"));
		System.out.println(subject.getPrincipal()+" 是否具有role1和role2角色====> "+subject.hasAllRoles(Arrays.asList("role1","role2")));
		//subject.checkRole("role1");
		//细颗粒度授权 ===> 资源验证
		System.out.println(subject.getPrincipal()+" 是否具有user:create资源权限====> "+subject.isPermitted("user:create"));
		System.out.println(subject.getPrincipal()+" 是否具有user:delete资源权限====> "+subject.isPermitted("user:delete"));
		System.out.println(subject.getPrincipal()+" 是否具有user:delete,user:update资源权限====> "+subject.isPermittedAll("user:delete","user:update"));
		
	}

输出结果

是否认证成功:true
admin 是否具有role1角色====> true
admin 是否具有role2角色====> true
admin 是否具有role3角色====> false
admin 是否具有role1和role2角色====> true
admin 是否具有user:create资源权限====> true
admin 是否具有user:delete资源权限====> true
admin 是否具有user:delete,user:update资源权限====> true

注意

subject.checkRole("role1");
subject.checkPermission("user:create");

检查是否存在该角色和权限,如果不存在则会抛异常

自定义Realm授权

重写授权的方法

注意:这里认证方法中采用的是明文认证

public class ShiroRealmsOne extends AuthorizingRealm{
	/**
	 * 认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username =(String)token.getPrincipal();
		if(!"admin".equals(username)){
			return null;
		}
		//String pwd = "123456"; 密码
		//String salt = "copy"; 盐值
		//  acd1b8d62a8369c3d6278ea6f663407b  两次迭代加密后的密码
		String salt = "copy";
		ByteSource saltByte = ByteSource.Util.bytes(salt);
		String password = "123456";
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,password,this.getName());
		return info;
	}

	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		String username = (String)principals.getPrimaryPrincipal();
		List<String> list = new ArrayList<String>();
		list.add("project:create");
		list.add("user:delete");
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addStringPermissions(list);
		return info;
	}

	

}

 shiro-realms.ini文件

[main]
shiroUserRealm=com.sumeng.shiro.ShiroRealmsOne
securityManager.realms=$shiroUserRealm

测试

@Test
	public void demoTree(){
		// 装入 INI 配置
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realms.ini");
		//创建SecurityManager对象
		SecurityManager instance = factory.getInstance();
		//使SecurityManager可以访问
		SecurityUtils.setSecurityManager(instance);
		//接受提交的用户名和密码: 
		UsernamePasswordToken tooken = new UsernamePasswordToken("admin","123456");
		//获取当前主体
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(tooken);
		} catch (UnknownAccountException e) {
			System.out.println("用户名错误!");
		}
		 catch (IncorrectCredentialsException e) {
				System.out.println("密码错误!");
			}
		System.out.println("是否认证成功:" + subject.isAuthenticated());
		//细颗粒度授权 ===> 资源验证
		System.out.println(subject.getPrincipal()+" 是否具有user:create资源权限====> "+subject.isPermitted("user:create"));
		System.out.println(subject.getPrincipal()+" 是否具有user:delete资源权限====> "+subject.isPermitted("user:delete"));
		System.out.println(subject.getPrincipal()+" 是否具有project:create资源权限====> "+subject.isPermitted("project:create"));
		System.out.println(subject.getPrincipal()+" 是否具有user:delete,project:create资源权限====> "+subject.isPermittedAll("user:delete","project:create"));
		
	}

输出结果

是否认证成功:true
admin 是否具有user:create资源权限====> false
admin 是否具有user:delete资源权限====> true
admin 是否具有project:create资源权限====> true
admin 是否具有user:delete,project:create资源权限====> true

 

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值