01-Shiro初识

Shiro初识

Apache Shiro是一个强大易用的Java安全框架,

提供了认证、授权、加密和会话管理功能,可为任何应用提供安全保障,

从命令行应用、移动应用到大型网络及企业应用

    认证:用户身份识别,常被称为用户"登录"

    授权:访问控制

    密码加密:保护或隐藏数据防止被偷窃

    会话管理:每用户相关的时间敏感的状态

Shiro:还支持一些辅助特性,
如
Web应用安全、
单元测试
多线程,它们的存在强化了上面提到的四个要素

maven Jar

http://mvnrepository.com/

项目体系图

pom.xml


    4.0.0
    com.matrix.shiro
    ShiroTest
    0.0.1-SNAPSHOT
    ShiroTest
    ShiroTest


    
        
            org.apache.shiro
            shiro-core
            1.2.4
        

        
            org.slf4j
            slf4j-log4j12
            1.7.12
        
    


shiro.ini

[users]
matrix=123456
Jack=123456

Shiro HelloWorld

package com.matrix.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;

public class HelloWorld {

    public static void main(String[] args) {
        // 读取配置文件,初始化SecurityManager工厂
        Factory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        // 获取SecurityManager实例
        SecurityManager securityManager = factory.getInstance();
        // 绑定securityManager实例绑定到SecurityUtils上去
        SecurityUtils.setSecurityManager(securityManager);
        // 得到当前执行的用户
        Subject currentUser = SecurityUtils.getSubject();
        // 创建Token令牌、用户名/密码
        UsernamePasswordToken token = new UsernamePasswordToken("matrix","123456");
        try{
            // 登录
            currentUser.login(token);
            System.out.println("身份认证成功!");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("身份认证失败!");
        }
        // 退出
        currentUser.logout();
    }
}

运行结果

你可能感兴趣的:(JavaEE)