Shiro授權(quán)流程圖
Shiro
Shiro授權(quán)流程
- 創(chuàng)建SecurityManager;
- 主體授權(quán);
- SecurityManager授權(quán);
- Authorizer授權(quán);
- Realm獲取角色權(quán)限數(shù)據(jù)岔留。
maven依賴
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
測試用例
package com.jarworker.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;
/**
* 授權(quán)測試
*/
public class AuthorizerTest {
SimpleAccountRealm simpleAccountRealm;
@Before
public void addAuthorizerUser() throws Exception {
simpleAccountRealm=new SimpleAccountRealm();
// simpleAccountRealm.addAccount("jarworker","123","admin");
simpleAccountRealm.addAccount("jarworker","123","admin","user");
}
@Test
public void testAuthorizer() throws Exception {
//構(gòu)建DefaultSecurityManager 環(huán)境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(simpleAccountRealm);
//主體提交認證請求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("jarworker","123");
subject.login(token);
System.out.println("是否認證:"+subject.isAuthenticated());
// 授權(quán)的時候需要登陸
// subject.checkRoles("admin");
subject.checkRoles("admin","user");
}
}