zoukankan      html  css  js  c++  java
  • shiro初探,最简单的认证,通过ini文件。

    一共分三大组件 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());
    
    
        }
    
    }
  • 相关阅读:
    MySQL主从数据库同步延迟问题解决(转)
    Python2.6升级Python2.7
    Socket网络编程
    Python 面向对象进阶
    Python类基础知识(面向对象基础)
    shell脚本中出现^M
    Centos6下Python3的编译安装
    Python成长之路(常用模块学习)
    SVN使用总结
    xshell锁屏
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/15236905.html
Copyright © 2011-2022 走看看