original code:
`ini configuration file (shiro-authenticator-all-success.ini)
Java Code
< H1 > specify the authenticator implementation of securityManager < / H1 > authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
securityManager.authenticator=$authenticator
allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy
Java Code Collection Code
myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1
myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2
myRealm3=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm3
securityManager.realms=$myRealm1,$myRealm3 `
2.1
Java
private void login(String configFile) {
//1SecurityManagerIniSecurityManager
Factory<org.apache.shiro.mgt.SecurityManager> factory =
new IniSecurityManagerFactory(configFile);
//2SecurityManager SecurityUtils
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//3Subject/Token/
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
subject.login(token);
}
2.2AllSuccessfulStrategy:
Java
@Test
public void testAllSuccessfulStrategyWithSuccess() {
login("classpath:shiro-authenticator-all-success.ini");
Subject subject = SecurityUtils.getSubject();
//Realm
PrincipalCollection principalCollection = subject.getPrincipals();
Assert.assertEquals(2, principalCollection.asList().size());
}
PrincipalCollectionzhangzhang@163.com
I also got down the code on github, which is indeed two pieces of identity information, but how can I write one along with it? Without that zhangsan@163.comzhe"tiao
my own:
/**
*
*/
private void login(String configFile) {
//
Factory<SecurityManager> factory = new IniSecurityManagerFactory(configFile);
//securityManagersecurityUtils
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//subjecttoken
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");
//
subject.login(token);
}
/**
* AllSuccessfulStrategy
*/
@Test
public void testAllSuccessfulStrategyWithFail() {
login("classpath:shiro-authenticator-all-success.ini");
Subject subject = SecurityUtils.getSubject();
//,realm
PrincipalCollection principals = subject.getPrincipals();
List list = principals.asList();
}
[main]
-sharpsecurityManagerauthenticator
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
securityManager.authenticator=$authenticator
-sharpsecurityManager.authenticatorauthenticationStrategy
allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy
myRealm1=com.rxiao.demo2.L2_MyRealm1
myRealm2=com.rxiao.demo2.L3_MyRealm2
myRealm3=com.rxiao.demo2.L4_MyRealm3
securityManager.realms=$myRealm1,$myRealm3