栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

shiro认证过程和授权过程(shiro认证流程)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shiro认证过程和授权过程(shiro认证流程)

1.首先创建一个普通的Maven工程

2.导入POM文件依赖---依赖如下

也可进入shiro官网查看需要的依赖“Download Apache Shiro | Apache Shiro”

 
        
            org.apache.shiro
            shiro-core
            1.3.2
        
        
            org.apache.shiro
            shiro-web
            1.3.2
        
        
            org.apache.shiro
            shiro-spring
            1.3.2
        
        
            org.slf4j
            slf4j-api
            1.7.33
        
        
            log4j
            log4j
            1.2.12
        

        
            junit
            junit
            4.12
        
    

3.在resources下创建*.ini文件

4.创建一个测试类

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
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.junit.Test;

public class ShiroTest {
    @Test
    public void shiroTest(){
        //1.读取配置文件,创建安全管理器:SecurityManger
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.createInstance();

        //2.将对象工场和当前线程绑定
        SecurityUtils.setSecurityManager(securityManager);

        //3.从当前线程获取主题对象:Subject
        Subject subject = SecurityUtils.getSubject();

        //创建令牌:封装身份(账号和密码)
        //UsernamePasswordToken token = new UsernamePasswordToken("lucky","abc123");

        boolean authenticated = subject.isAuthenticated();
        System.out.println("认证前"+authenticated);
        if (!authenticated){
            //创建令牌:封装身份(账号和密码)
            UsernamePasswordToken token = new UsernamePasswordToken("lucy","abc123");
               try {
                   subject.login(token);
                   authenticated=subject.isAuthenticated();
                   System.out.println("认证后"+authenticated);
               }catch (UnknownAccountException e){
                   System.out.println("用户名错误");
               }catch (IncorrectCredentialsException e){
                   System.out.println("密码错误");
               }
               //获取用户身份
            Object principal = subject.getPrincipal();
            System.out.println("用户身份"+principal);

            //退出登录
            subject.logout();
            authenticated=subject.isAuthenticated();
            System.out.println("退出后认证"+authenticated);

        }
    }
}

5.测试结果

6.用户名错误 与密码错去运行截图如下:

 

 7.小伙伴们可自行更改:里面的用户和密码进行测试

UsernamePasswordToken token = new UsernamePasswordToken("lucy","1abc123");

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号