shiro的加載方式
1:加載user/password的ini配置文件
[users]
zhang=123
wang=123
2:加載single-realm的ini配置文件
#聲明一個realm
myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1
#指定securityManager的realms實(shí)現(xiàn)
securityManager.realms=$myRealm1
3:加載multi-realm的ini配置文件
#聲明一個realm
myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1
myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2
#指定securityManager的realms實(shí)現(xiàn)
securityManager.realms=$myRealm1,$myRealm2
4:通過jdbc-realm驗(yàn)證用戶,會自動賦值給指定realm類的屬性名
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro
dataSource.username=root
#dataSource.password=
jdbcRealm.dataSource=$dataSource
securityManager.realms=$jdbcRealm
#指定securityManager的authenticator實(shí)現(xiàn)
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
securityManager.authenticator=$authenticator
#指定securityManager.authenticator的authenticationStrategy
#FirstSuccessfulStrategy成功驗(yàn)證一個realm即可,返回驗(yàn)證成功的認(rèn)證信息
#AtLeastOneSuccessfulStrategy成功驗(yàn)證一個realm即可,返回所有realm驗(yàn)證成功的認(rèn)證信息(ModularRealmAuthenticator默認(rèn))
#AllSuccessfulStrategy必須成功驗(yàn)證所有realm,返回所有realm驗(yàn)證成功的認(rèn)證信息
allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy
注意:可以自定義authenticationStrategy,需繼承抽AbstractAuthenticationStrategy類
授權(quán)
1:ini配置角色權(quán)限的格式,可以用hasRole/hasAllRoles判斷用戶是否具有某個或者所有角色,isPermitted/isPermittedAll判斷用戶是否具有某個權(quán)限或者所有權(quán)限
[users]
zhang=123,role1,role2
wang=123,role1
[roles]
#權(quán)限通配符:";"表示資源/操作/實(shí)例的分割;","表示操作的分割;"*"表示任意資源/操作/實(shí)例。
#shiro對權(quán)限字符串缺失部分的處理,user:view=user:view:*,可以理解為前綴匹配
role1=user:create,user:update
role2=user:create,user:delete
#role3=role4
role3=system:user:create;system:user:update;system:user:delete;system:user:view
role4=system:user:create,update,delete,view
#role5=role6
role5=system:user:*
role6=system:user
#對資源user的1 實(shí)例擁有view權(quán)限,實(shí)例就是具體到某條記錄
role7=system:user:view:1
role8=system:user:update,delete:1
role9=system:user:auth:*
#對資源user擁有所有權(quán)限
role10=system:user:*:*
2:改變驗(yàn)證權(quán)限的類,如下ini配置
[main]
#自定義authorizer
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
#自定義permissionResolver
#permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
permissionResolver=com.github.zhangkaitao.shiro.chapter3.permission.BitAndWildPermissionResolver
authorizer.permissionResolver=$permissionResolver
#自定義rolePermissionResolver
rolePermissionResolver=com.github.zhangkaitao.shiro.chapter3.permission.MyRolePermissionResolver
authorizer.rolePermissionResolver=$rolePermissionResolver
securityManager.authorizer=$authorizer
#自定義realm 一定要放在securityManager.authorizer賦值之后(因?yàn)檎{(diào)用setRealms會將realms設(shè)置給authorizer,并給各個Realm設(shè)置permissionResolver和rolePermissionResolver)
realm=com.github.zhangkaitao.shiro.chapter3.realm.MyRealm
securityManager.realms=$realm
配置
1:無ini配置,純java代碼
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//設(shè)置authenticator
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
securityManager.setAuthenticator(authenticator);
//設(shè)置authorizer
ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
authorizer.setPermissionResolver(new WildcardPermissionResolver());
securityManager.setAuthorizer(authorizer);
//設(shè)置Realm
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/shiro");
ds.setUsername("root");
ds.setPassword("");
JdbcRealm jdbcRealm = new JdbcRealm();
jdbcRealm.setDataSource(ds);
jdbcRealm.setPermissionsLookupEnabled(true);
securityManager.setRealms(Arrays.asList((Realm) jdbcRealm));
//將SecurityManager設(shè)置到SecurityUtils 方便全局使用
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
subject.login(token);
2:ini配置
Factory<org.apache.shiro.mgt.SecurityManager> factory =
new IniSecurityManagerFactory("classpath:shiro-config.ini");
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
//將SecurityManager設(shè)置到SecurityUtils 方便全局使用
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
subject.login(token);
[main]
#覆蓋默認(rèn)的securityManager
#securityManager=org.apache.shiro.mgt.DefaultSecurityManager
#authenticator
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
authenticator.authenticationStrategy=$authenticationStrategy
securityManager.authenticator=$authenticator
#authorizer
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
authorizer.permissionResolver=$permissionResolver
securityManager.authorizer=$authorizer
#realm
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro
dataSource.username=root
#dataSource.password=
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource=$dataSource
jdbcRealm.permissionsLookupEnabled=true
securityManager.realms=$jdbcRealm
加密解密待完善--密碼處理相關(guān)
realm詳解--留待后面看源碼,詳細(xì)處理
與web繼承--看了spring關(guān)于web這塊源碼之后再來細(xì)看
攔截器,使用spring的比較好,不復(fù)雜,簡單易懂
1:user 攔截器只要用戶登錄(isRemembered()==true or isAuthenticated()==true)過即可訪問成
2:authc 攔截器會判斷用戶是否是通過Subject.login(isAuthenticated()==true)登錄的
會話管理
1:會話管理器SessionManager
2:會話監(jiān)聽器SessionListener
3:會話存儲/持久化SessionDAO-CachingSessionDAO
4:會話驗(yàn)證SessionValidationScheduler
5:sessionFactory 是創(chuàng)建會話的工廠
緩存
1:Cache,CacheManager,CacheManagerAware
2:Realm緩存 CachingRealm實(shí)現(xiàn)了CacheManagerAware
3:建議廢棄shiro的緩存,實(shí)現(xiàn)自己的緩存或者使用spring的緩存SpringCacheManagerWrapper
和spring集成
1:就是將之前的ini配置文件整合成為了bean配置,注意查看和之前的ini配置對比,其實(shí)就是將set改成了property設(shè)置,注入值,最終的一個bean還是securityManager
2:shiro在spring中可以使用注解判斷角色權(quán)限@RequiresRoles("admin"),因?yàn)閟pirng支持aop,如果不具備該角色拋出UnauthorizedException異常,可以采用spring的@ExceptionHandler來捕捉異常
SSL后續(xù)多研究再來完成這部分吧
SSO結(jié)合zheng吧
OAuth2后續(xù)多看一點(diǎn)
只允許一個人登錄
1:擴(kuò)展KickoutSessionControlFilter