Shiro——入门Demo

Shiro——入门Demo

环境-  引入相关maven依赖, shiro-core,commons-logging

配置shiro配置文件:ini后缀

Shiro——入门Demo_第1张图片

主方法测试:

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * @author DeepSleeping
 * @date 2019/5/27 11:45
 * @description
 */
public class Main {
    public static void main(String[] args) {
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        IniRealm iniRealm = new IniRealm("classpath:shiro/shiro.ini");
        defaultSecurityManager.setRealm(iniRealm);
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("admin", "123456");
        subject.login(usernamePasswordToken);
        System.out.println(subject.isAuthenticated());
    }
}

Shiro——入门Demo_第2张图片

那个iniSecurity啥的Factory过期了,查了资料后这样写的。

可以在登录捕获异常,比如unknownusername,啥的针对不同异常做不同处理。

 

转载于:https://www.cnblogs.com/deepSleeping/p/10930305.html

你可能感兴趣的:(java)