Shiro授权

授权过程
  • 1、创建SecurityManager
  • 2、主体授权
  • 3、SecurityManager授权
  • 4、Authorizer授权
  • 5、Realm获取角色权限数据
代码演示
public class AuthorizerTest {
    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
    @Before
    public void addUser() {
        simpleAccountRealm.addAccount("Mark", "123456", "admin", "user");
    }

    @Test
    public void testAuthorizer() {
        // 1. 构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(simpleAccountRealm);

        // 2. 主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");
        subject.login(token);

        System.out.println("isAuthenticated:" + subject.isAuthenticated());

        subject.checkRoles("admin", "user");
    }

}
posted @ 2018-11-22 17:16  _sanjun  阅读(101)  评论(0编辑  收藏  举报