apache-shiro学习笔记(补足六个汉字).docx
文本预览下载声明
shiro.ini
[main]
#创建一个HashedCrededtialsMatcher对象
cm = org.apache.shiro.authc.credential.HashedCredentialsMatcher
#调用cm的setHashAlgorithm(...)方法
cm.hashAlgorithm = SHA-512
cm.hashIterations = 1024
# Base64 encoding (less text):
cm.storedCredentialsHexEncoded = false
# $cm,引用上面定义的cm对象
# 指定密码编码方式
iniRealm.credentialsMatcher = $cm
#设置某个filter启用状态
ssl.enabled = false
#设置缓存机制
#Shiro 的SecurityManager 实现及所有AuthorizingRealm实现都实现了CacheManagerAware,设置会改变这些类的缓存机制
cacheManager = my.implementation.of.CacheManager
[users]
##username = password, roleName1, roleName2, …, roleNameN
jdoe = TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJpcyByZWFzb2
asmith = IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbXNoZWQsIG5vdCB
[roles]
#rolename = permissionDefinition1, permissionDefinition2, … , permissionDefinitionN
#permissionDefinition 是一个任意的字符串,但大多数人将会使用符合
#org.apache.shiro.authz.permission.WildcardPermission 格式的字符串
admin = *
master = winnebago:drive:eagle5
guest = printer:5thFloor:print,info
[urls]
#格式为:URL_Ant_Path_Expression = Path_Specific_Filter_Chain
#右边为用逗号隔开的过滤器列表,格式为:filter1[optional_config1], ..., filterN[optional_configN]
/assets/** = anon
#第一个匹配的url会短路之后匹配的url 即FIRST MATCH WINS原则
/user/signup = anon
/user/** = user
/rpc/rest/** = perms[rpc:invoke], authc
/** = authc
加载配置文件
//1.装入INI配置
FactorySecurityManager factory = new IniSecurityManagerFactory(classpath:shiro.ini);
//2. 创建SecurityManager
SecurityManager securityManager = factory.getInstance();
//3. 使其可访问
SecurityUtils.setSecurityManager(securityManager);
Subject登录
//1. 接受提交的当事人和证书:
AuthenticationToken token = new UsernamePasswordToken(username, password);
//2. 获取当前Subject:
Subject currentUser = SecurityUtils.getSubject();
//3. 登录:
currentUser.login(token);
//退出
subject.logout();
//可设置“记住我”
token.setRememberMe(true);
验证步骤:
控制失败的登录
//3. 登录:
try {
currentUser.login(token);
} catch (IncorrectCredentialsException ice) {
…
} catch (LockedAccountException lae) {
…
}
…
catch (AuthenticationException ae) {…
}
验证是否登录
subject.is
显示全部