自定义reaml创建使用实现认证

 

注意清空shiro.ini

创建User对象

package cn.zys.Bean;

public class User {
    private Integer id;
    private String username;
    private String pwd;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", pwd=" + pwd + "]";
    }
    public User(Integer id, String username, String pwd) {
        super();
        this.id = id;
        this.username = username;
        this.pwd = pwd;
    }
    

}

创建模拟请求数据Servise

package cn.zys.servise;
import cn.zys.Bean.User;

public class Servise {
    
    public User queryUser(String username){
        User user = null;
        switch (username) {
        case "zhangsan":
            user = new User(1,"zhangsan","123456");
            break;
        case "lisi":
            user = new User(2,"lisi","123456");
            break;
        case "wangwu":
            user = new User(3,"wangwu","123456");
            break;
        default:
            break;
        }
        System.out.println(user.toString());
        return user;
    }
    
}

创建UserRealm  继承 AuthenticatingRealm

package cn.zys.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;

import cn.zys.Bean.User;
import cn.zys.servise.Servise;

public class UserRealm extends AuthenticatingRealm{

    private Servise servise = new Servise();
    
    //做认证的方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // TODO Auto-generated method stub
        //再shiro中根据用户名查出相关信息,再判断,为以后做加密做准备被
        String username = (String) token.getPrincipal();
        System.out.println(username);

        token.getPrincipal();
        User user = servise.queryUser(username);
        if(user != null){
            /*
             * 参数说明
             * 参数1 可以传入任意对象
             * 2 从数据库中查出的密码
             * 3 当前类名
             * */
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, user.getPwd(), this.getName());
            return info;
        
        }
        return null;
    }


}

TestAuthenticationApp测试认证

package cn.zys.shiro;

import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.zys.realm.UserRealm;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class TestAuthenticationApp {
    //日志输出工具
    private static final transient Logger log = LoggerFactory.getLogger(TestAuthenticationApp.class);
    public static void main(String[] args) {
        
         String username = "zhangsan";
         String password = "123456";
        
         log.info("My First Apache Shiro Application");
         //1 创建安全管理器的工厂对象
         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
         //2 使用工厂创建安全管理器
         DefaultSecurityManager securityManager = (DefaultSecurityManager) factory.getInstance();
         //3 把当前的安全管理器绑定到线程
         //创建userRealm
         UserRealm realm = new UserRealm();
         //securityManager 中注入userrealm
         securityManager.setRealm(realm);
         SecurityUtils.setSecurityManager(securityManager);
         //4 使用SecurityUtils.getSubject() 得到主体
         Subject currentUser = SecurityUtils.getSubject();
        //5 封装用户名
         AuthenticationToken arg0 = new UsernamePasswordToken(username, password);
//         System.out.println(arg0);
         try {
             currentUser.login(arg0);
             System.out.println("认证通过");
             Object getPrincipal = currentUser.getPrincipal();
             System.out.println(getPrincipal);
        } /*catch (AuthenticationException e) {
            // TODO: handle exception
            System.out.println("用户名或密码错误");
        }*/catch(IncorrectCredentialsException e){
            System.out.println("密码不正确");
        }catch(UnknownAccountException e){
            System.out.println("用户名不存在");
        }
         
         
         
         //退出的方法
         //currentUser.logout();


         
    }
}
posted @ 2019-12-16 17:20  一半人生  阅读(243)  评论(0)    收藏  举报