shiro实例
所属分类 shiro
浏览量 988
配置文件 shiro.ini
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz
[users]
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz
[roles]
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
org.apache.shiro.realm.text.TextConfigurationRealm
Factory factory = new IniSecurityManagerFactory("classpath:shiro2.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
}
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
currentUser.hasRole("schwartz")
currentUser.isPermitted("lightsaber:wield")
currentUser.isPermitted("winnebago:drive:eagle5")
currentUser.logout();
完整代码
https://gitee.com/dyyx/demos/blob/master/apache-shiro/src/main/java/guide/Quickstart.java
上一篇
下一篇
Spring Security和Shiro简单比较
Shiro简介
通配符和正则的区别
职场修炼心法
Java 位运算实例
K8S简介