以下代码来自于shiro官方提供的代码,便于新手入门shiro api,中文为个人翻译,仅供参考
public class Quickstart {
private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
public static void main(String[] args) {
// 创建具有realms、users、roles 和permissions的Shiro SecurityManager,最简单方法是使用INI配置。
// 我们将通过使用一个可以读取.ini文件并返回SecurityManager实例的工厂类来实现这一点:
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // 使用classpath目录下的shiro.ini文件
SecurityManager securityManager = factory.getInstance();
// 对于这个简单的快速入门示例,将SecurityManager作为JVM单例进行访问。
// 大多数应用程序不会这样做,而是依赖于它们的容器配置或者webapp的web.xml。
// 但这超出了这个简单快速入门的范围,所以我们只做最简单的操作,这样您就可以继续对事物有所了解。
SecurityUtils.setSecurityManager(securityManager);
// 现在设置了一个简单的Shiro环境,让我们看看您可以做什么:
// 获取当前正在执行的用户:
Subject currentUser = SecurityUtils.getSubject();
// 用session做一些事情(不需要web或EJB容器!!!)
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}
// 让我们登录当前用户,以便检查角色和权限:
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// ... 在此处捕获更多异常(可能是特定于应用程序的自定义异常)?
catch (AuthenticationException ae) {
// 意外情况?错误?
}
}
// 打印其身份主体(在本例中为用户名):
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
// 测试一个权限:
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
}
// 测试类型化权限(不是实例级)
if (currentUser.isPermitted("lightsaber:wield")) {
log.info("You may use a lightsaber ring. Use it wisely.");
} else {
log.info("Sorry, lightsaber rings are for schwartz masters only.");
}
//(非常强大的)实例级权限:
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
// 全部结束后退出
currentUser.logout();
System.exit(0);
}
}
权限相关信息(shiro.ini):
# -----------------------------------------------------------------------------
# Users and their assigned roles
#
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc
# -----------------------------------------------------------------------------
[users]
# user 'root' with password 'secret' and the 'admin' role
root = secret, admin
# user 'guest' with the password 'guest' and the 'guest' role
guest = guest, guest
# user 'presidentskroob' with password '12345' ("That's the same combination on
# my luggage!!!" ;)), and role 'president'
presidentskroob = 12345, president
# user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz'
darkhelmet = ludicrousspeed, darklord, schwartz
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz
# -----------------------------------------------------------------------------
# Roles with assigned permissions
#
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc
# -----------------------------------------------------------------------------
[roles]
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
# The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with
# license plate 'eagle5' (instance specific id)
goodguy = winnebago:drive:eagle5
如果有兴趣了解更多相关内容,欢迎来我的个人网站看看:瞳孔的个人空间