shiro认证与授权:基于ini的用户授权
生活随笔
收集整理的這篇文章主要介紹了
shiro认证与授权:基于ini的用户授权
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[users]
#用戶名=密碼,角色名
zhangsan=123456,role1,role2
lisi=123456,role2
[roles]
#角色
#角色名=權限列表
role1=user:save,user:update
role2=user:find
package cn.learn.shiro;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;public class ShiroTest02 {private SecurityManager securityManager;@Beforepublic void init() {//1.根據配置文件創建SecurityManagerFactoryFactory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-test-2.ini");//2.通過工廠獲取SecurityManagerSecurityManager securityManager = factory.getInstance();//3.將SecurityManager綁定到當前運行環境SecurityUtils.setSecurityManager(securityManager);}@Testpublic void testLogin() {Subject subject = SecurityUtils.getSubject();String username = "lisi";String password = "123456";UsernamePasswordToken token = new UsernamePasswordToken(username,password);subject.login(token);//登錄成功之后,完成授權//授權:檢驗當前登錄用戶是否具有操作權限,是否具有某個角色System.out.println(subject.hasRole("role1"));System.out.println(subject.isPermitted("user:save"));}}
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的shiro认证与授权:基于ini的用户授权的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shiro的内部体系结构
- 下一篇: shiro认证与授权:自定义realm