shiro_day01 shiro basic concepts and code

Basic concepts

shiro :安全管理框架
Subject : 需要验证的实体类,也充当shiro框架对外的接口
securityManager : 安全管理器,shiro框架的核心,负责调用其他模块进行授权认证
authenticator :认证器,一般用于登录时使用
authorizer :授权器,验证是否有权限
realm :用户与权限的域
sessionManager :会话管理器,可用于实现单点登录	
cacheManeger : 缓存管理器
cryptography : 密码管理器,提供了加密解密算法

shiro basic combat

The introduction of dependence

  		<!-- shiro核心包 -->
		<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!--junit包(非必须) -->
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!--日志框架包,shiro框架依赖slf4j,需引入slf4j与log4j2的桥接包,里面已经包含了log4j2包 -->
		<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.1</version>
            <scope>test</scope>
        </dependency>

Add a profile

shiro.ini

[roles]
admin=product:view,product:create,product:update,product:delete
public=product:view

[users]
zhangsan=123,admin
lisi=1234,public

shiro profile is divided into two parts, the users, part roles.
The main roles for storing characters and the format of the format key = values, the role name is left, the right is the resource name, resource name, separated by commas plurality of resources;
Users mainly used for storing the user, the format also uses key = values of format, the user name is left, the right is the name of the role, multiple roles the same name separated by a comma.

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <logger name="org.springframework" level="info"></logger>
        <root level="DEBUG">
            <AppenderRef ref="Console" />
        </root>
    </Loggers>
</Configuration>

Code Testing

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import sun.rmi.runtime.Log;

import java.util.logging.Logger;

/**
 * TODO:
 *
 * @author wangkui
 * @date 2020/1/29 17:08
 */
public class ShiroTest {
    private static Logger log = java.util.logging.Logger.getLogger(String.valueOf(ShiroTest.class.getClass()));
    @Test
    public void test(){
        //1初始化安全管理器
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //2设置realm
        Realm realm = new IniRealm("classpath:shiro.ini");
        securityManager.setRealm(realm);
        //3设置securityUtil将securityManeger设置到运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        //4创建Subject实例
        Subject subject = SecurityUtils.getSubject();
        //5创建token令牌,记录用户认证身份,账号与密码
        UsernamePasswordToken token = new UsernamePasswordToken("lisi","1234");
        //6主体进行登录
        log.info("是否已登录:"+subject.isAuthenticated());
        subject.login(token);
        log.info("是否已登录:"+subject.isAuthenticated());
        //7检查授权角色
        log.info("用户是否有角色admin:"+subject.hasRole("admin"));
        log.info("用户是否有角色public:"+subject.hasRole("public"));
        //8检查资源授权
        log.info("用户是否有权限create:"+subject.isPermitted("product:create"));
        log.info("用户是否有权限view:"+subject.isPermitted("product:view"));
        subject.checkPermission("product:view");
        //9账号登出
        subject.logout();
        log.info("是否已登录:"+subject.isAuthenticated());
    }
}
Released eight original articles · won praise 1 · views 974

Guess you like

Origin blog.csdn.net/wk841610731/article/details/104113675