package com.nathanagood.examples.shirotest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ShiroTest {
private static Logger logger = LoggerFactory.getLogger(ShiroTest.class);
public static void main(String[] args) {
// Using the IniSecurityManagerFactory, which will use the an INI file
// as the security file.
Factory factory =
new IniSecurityManagerFactory("auth.ini");
// Setting up the SecurityManager...
org.apache.shiro.mgt.SecurityManager securityManager
= factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject user = SecurityUtils.getSubject();
logger.info("User is authenticated: " + user.isAuthenticated());
}
}
// snipped... same as before.
public class ShiroTest {
private static Logger logger = LoggerFactory.getLogger(ShiroTest.class);
public static void main(String[] args) {
// Using the IniSecurityManagerFactory, which will use the an INI file
// as the security file.
Factory factory =
new IniSecurityManagerFactory("auth.ini");
// Setting up the SecurityManager...
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject user = SecurityUtils.getSubject();
logger.info("User is authenticated: " + user.isAuthenticated());
UsernamePasswordToken token = new UsernamePasswordToken("bjangles", "dance");
user.login(token);
logger.info("User is authenticated: " + user.isAuthenticated());
}
}
// snipped...
public class ShiroLDAPTest {
private static Logger logger = LoggerFactory.getLogger(ShiroLDAPTest.class);
/**
* @param args
*/
public static void main(String[] args) {
// Using the IniSecurityManagerFactory, which will use the an INI file
// as the security file.
Factory factory =
new IniSecurityManagerFactory("actived.ini");
// Setting up the SecurityManager...
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject user = SecurityUtils.getSubject();
logger.info("User is authenticated: " + user.isAuthenticated());
UsernamePasswordToken token =
new UsernamePasswordToken(
"cn=Cornelius Buckley,ou=people,o=sevenSeas", "argh");
user.login(token);
logger.info("User is authenticated: " + user.isAuthenticated());
}
}
除了 INI 文件名和用户名及码密以外,码代与之前用 INI 文件内的记载停止身份验证的码代同相。这类相似性的现出是因为您可以应用 INI 文件来置配 Shiro。用来设置 Shiro 针对 Apache Directory 停止身份验证的这些 INI 记载如 清单 6 所示。
清单 6. actived.ini 文件