天天看點

菜鳥學習shiro之實作自定義的Realm,進而實作登入驗證,身份驗證和權限驗證4

講了那麼多使用的内置的類進而實作四郎,現在講自定義的境界

首先行家的依賴依然是第一篇的那個依賴

下邊是自定義的境界:

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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CustomRealm extends AuthorizingRealm {

    /*
    * 模拟資料庫
    * */
    Map<String,String> userMap = new HashMap<>(16);

    {
        userMap.put("Mark","283538989cef48f3d7d8a1c1bdf2008f");
        super.setName("customRealm");
    }

    /*
    *   自定義的權限校驗
    * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        String userName = (String)principalCollection.getPrimaryPrincipal();
        Set<String> roles = getRoleByUserName(userName);
        Set<String> permissions = getPermissionByUserName(userName);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }

    private Set<String> getPermissionByUserName(String userName) {
        Set<String> sets = new HashSet<>(16);
        sets.add("user:delete");
        sets.add("user:add");
        return sets;
    }

    /*
    *   這個是模拟資料庫
    * */
    private Set<String> getRoleByUserName(String userName) {
        Set<String> sets = new HashSet<>(10);
        sets.add("admin");
        sets.add("user");
        return sets;
    }

    /*
    *  這是使用者的登入授權
    * */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        // 1.從主體傳過來的認證資訊中,獲得使用者名
        String userName = (String)authenticationToken.getPrincipal();

        // 2.通過使用者名到資料庫中獲得憑證

        String password = getPasswordByUserName(userName);

        if (password == null) {
            return null;
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("Mark",password,"customRealm");
        // 這個是直接加鹽在原來加密的基礎上
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("Mark"));
        return authenticationInfo;
    }

    /*
    *   這是模拟資料庫的
    * */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }

    // 這個是加密生成的字元串,後邊加鹽
  /*  public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("123456","Mark");
        System.out.println(md5Hash);
    }*/
}
           

下邊是測試的:

import com.example.demo.com.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class CustomRealmTest {

    @Test
    public void testAuthenticationTest() {
        // 這個是自定義的realm
        CustomRealm customRealm = new CustomRealm();

        // 1.建構SecurityManager環境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(customRealm);

        // 這是散列加密工具的封裝類啊
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5");
        matcher.setHashIterations(1);
        // 這一步是把自定義的散列加密內建到我自定義的認證機制中
        customRealm.setCredentialsMatcher(matcher);

        // 2.主體送出認證請求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        // 這個是需要認證的資訊
        UsernamePasswordToken token = new UsernamePasswordToken("Mark","123456");
        subject.login(token);

        System.out.println("isAuthenticated="+subject.isAuthenticated());

        //subject.logout();

    /*    System.out.println("isAuthenticated="+subject.isAuthenticated());

        subject.checkRole("admin");
        subject.checkPermissions("user:delete","user:update");*/
        /*subject.checkRole("admin");
        subject.checkPermission("user:add");*/

    }

}

           

代碼講的很清楚了,我就不多說了。