shrio自定义IniRealm
image.png- pom文件在Shrio认证基础上
- java代码
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class IniRealmTest {
@Test
public void testAuthentiation(){
IniRealm iniRealm = new IniRealm("classpath:user.ini");
//构建SecurityManager环境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(iniRealm);
//主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("gouDan","123456");
//登陆
subject.login(token);
System.out.println(subject.isAuthenticated());
//检查是否具备权限
subject.checkPermissions("select","insert");
//subject.checkRole();
}
}
- user.ini
注意 需要在resources内添加user.ini配置文件
[users]
gouDan=123456,admin
[roles]
admin=insert,select,delete
需要注意
- 权限可以配置多个
- 权限不对会抛出
UnauthorizedException
网友评论