使用Shiro的JdbcRealm实现查询数据库进行身份认证:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/90105995
权限认证,也就是访问控制,即在应用中控制谁能访问哪些资源。
在权限认证中,最核心的三个要素是:权限,角色和用户;
权限,即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利;
角色,是权限的集合,一中角色可以包含多种权限;
用户,在Shiro 中,代表访问系统的用户,即Subject;
在上面搭建项目实现了身份验证的基础上。
完整pom.xml
4.0.0
com.badao.shiro
shiroTest
0.0.1-SNAPSHOT
shiroTest
org.apache.shiro
shiro-core
1.2.4
org.slf4j
slf4j-log4j12
1.7.12
c3p0
c3p0
0.9.1.2
commons-logging
commons-logging
1.2
mysql
mysql-connector-java
8.0.11
junit
junit
4.12
新建common包,包下新建ShiroUtil.java
package com.badao.shiro.common;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
public class ShiroUtil {
public static Subject login(String configFile,String userName,String password){
//读取配置文件,初始化化SecurityManager工厂
Factory factory = new IniSecurityManagerFactory(configFile);
//获取securityManager实例
SecurityManager securityManager = factory.getInstance();
//将securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
//获取当前用户
Subject currentUser = SecurityUtils.getSubject();
//创建token令牌,用户名 密码
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
try {
//身份认证 登录
currentUser.login(token);
System.out.println("身份认证成功");
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("身份认证失败");
}
return currentUser;
}
}
在resource下新建shiro_role.ini配置文件
[users]
badao=123456,role1,role2
liumang=123,role1
上面的写法代表badao拥有角色role1和role2,liumang拥有角色role1
编写测试类
package com.badao.shiro;
import java.util.Arrays;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import com.badao.shiro.common.ShiroUtil;
public class RoleTest {
@Test
public void testHasRole() {
Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "badao", "123");
System.out.println(currentUser.hasRole("role1")?"有role1这个角色":"没有role1这个角色");
boolean []results=currentUser.hasRoles(Arrays.asList("role1","role2","role3"));
System.out.println(results[0]?"有role1这个角色":"没有role1这个角色");
System.out.println(results[1]?"有role2这个角色":"没有role2这个角色");
System.out.println(results[2]?"有role3这个角色":"没有role3这个角色");
System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2这两个角色都有":"role1,role2这个两个角色不全有");
currentUser.logout();
}
@Test
public void testCheckRole() {
Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "badao", "123");
//checkRole没有返回值 验证不成功会报异常
currentUser.checkRole("role1");
currentUser.checkRoles(Arrays.asList("role1","role2"));
currentUser.checkRoles("role1","role2","role3");
currentUser.logout();
}
}
运行结果
testHasRole:
testCheckRole:
编写权限验证配置文件
resource下新建shiro_permission.ini
[users]
badao=123,role1,role2
liumang=456,role1
[roles]
role1=user:select
role2=user:add,user:update,user:delete
已上配置表明role1有select权限,role2有add、update、delete权限。
编写单元测试类
package com.badao.shiro;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import com.badao.shiro.common.ShiroUtil;
public class PermissionTest {
@Test
public void testIsPermitted() {
Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "badao", "123");
System.out.println(currentUser.isPermitted("user:select")?"有user:select这个权限":"没有user:select这个权限");
System.out.println(currentUser.isPermitted("user:update")?"有user:update这个权限":"没有user:update这个权限");
boolean results[]=currentUser.isPermitted("user:select","user:update","user:delete");
System.out.println(results[0]?"有user:select这个权限":"没有user:select这个权限");
System.out.println(results[1]?"有user:update这个权限":"没有user:update这个权限");
System.out.println(results[2]?"有user:delete这个权限":"没有user:delete这个权限");
System.out.println(currentUser.isPermittedAll("user:select","user:update")?"有user:select,update这两个权限":"user:select,update这两个权限不全有");
currentUser.logout();
}
@Test
public void testCheckPermitted() {
Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "badao", "123");
currentUser.checkPermission("user:select");
currentUser.checkPermissions("user:select","user:update","user:delete");
currentUser.logout();
}
}
效果:
testIsPermitted
testCheckPermitted
https://download.csdn.net/download/badao_liumang_qizhi/11173045