淺談關於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的名字。

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