2.Shrio IniRealm

2018-10-15  本文已影响0人  勤劳的杯子

shrio自定义IniRealm

image.png

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();
    }
}

注意 需要在resources内添加user.ini配置文件

[users]
gouDan=123456,admin
[roles]
admin=insert,select,delete

需要注意

  • 权限可以配置多个
  • 权限不对会抛出
    UnauthorizedException
上一篇下一篇

猜你喜欢

热点阅读