一共分三大组件 subject(主题,主要是认证的用户密码),securityManager(安全管理器),realm(数据桥梁)
在resource 里建立shiro.ini 文件
#用户 [users] zhangsan=123456 lisi=12345
然后写test类
package cn.taotao; import org.apache.commons.collections.bag.SynchronizedSortedBag; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationToken; 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.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class ActivitiApplicationTests { @Test void contextLoads() { } @Test void testShiro(){ String username="zhangsan"; String password = "123456"; // 创建工厂,注意引入的包名,SecurityManager 是shiro的包 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // 取得安全管理器实例 SecurityManager securityManager = factory.getInstance(); // 把当前SecurityManager 绑定到当前线程 SecurityUtils.setSecurityManager(securityManager); // 取出当前的subject Subject subject = SecurityUtils.getSubject(); // AuthenticationToken authenticationToken = new UsernamePasswordToken(username,password); subject.login(authenticationToken); System.out.println("是否认证成功"+subject.isAuthenticated()); } }