Shiro——入门Demo

Shiro——入门Demo

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

配置shiro配置文件:ini后缀

主方法测试:

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());
    }
}

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

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

 

posted @ 2019-05-27 13:47  瓶子coder  阅读(155)  评论(0编辑  收藏  举报