码迷,mamicode.com
首页 > 其他好文 > 详细

shiro权限管理入门程序

时间:2018-07-13 12:11:50      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:设置   nand   stl   zhang   ace   flag   subject   int   用户   

最近在学shiro,觉得入门程序还是有用的,记下来防止遗忘,也可供大家参考。

package cn.itcast.shiro.authentication;

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

/**
 * 
 * @author yxf
 * shiro验证用户的登录
 *
 */
public class AuthenticationTest {

    //用户登录退出
    @Test
    public void testLoginAndLogout() {
        //创建securityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
        
        //创建securityManager
        SecurityManager securityManager = factory.getInstance();
        
        //securityManager设置到当前运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        
        //SecurityUtils创建一个subject
        Subject subject = SecurityUtils.getSubject();
        
        //认证提交前准备token
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");
        
        //执行认证提交
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        
        //是否认证通过
        boolean flag = subject.isAuthenticated();
        
        System.out.println("是否认证通过:" + flag);
        
        //退出操作
        subject.logout();
        
        flag = subject.isAuthenticated();
        
        System.out.println("是否认证通过:" + flag);
        
    }
}

 

shiro权限管理入门程序

标签:设置   nand   stl   zhang   ace   flag   subject   int   用户   

原文地址:https://www.cnblogs.com/haobingshuaike/p/9304135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!