浅谈关于shiro——SimpleAuthenticationInfo中的参数

/**
	 * 执行认证逻辑
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        
        UsernamePasswordToken token = (UsernamePasswordToken) arg0;
        
        User user = userService.findUserByName(token.getUsername());
        
        if(user==null) {
        	//用户名不存在
        	return null;
        }
        
        //密码不存在
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
	}

其中:SimpleAuthenticationInfo中可以传三个参数也可以传四个参数。

第一个参数:传入的都是com.java.entity包下的User类的user对象

        注意:此参数可以通过subject.getPrincipal()方法获取—获取当前记录的用户,从这个用户对象进而再获取一系列的所需要的属性。

    Subject subject = SecurityUtils.getSubject();
    User user = (User) subject.getPrincipal();

第二个参数:  传入的是从数据库中获取到的password,然后再与token中的password进行对比,匹配上了就通过,匹配不上就报异常。

第三个参数,盐–用于加密密码对比。 若不需要,则可以设置为空 “ ”

第四个参数:当前realm的名字。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章