天天看點

自定義Realm自定義Realm

文章目錄

  • 自定義Realm
    • 1.1 在main.java下建立包:com.amoscxy.shiro.realm
    • 1.2 建立CustomRealm繼承AuthorizingRealm
    • 在com.amoscxy.test包中建立測試類:CustomRealmTest:

自定義Realm

1.1 在main.java下建立包:com.amoscxy.shiro.realm

自定義Realm自定義Realm

1.2 建立CustomRealm繼承AuthorizingRealm

package com.amoscxy.shiro.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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.*;

/**
 * Created by cxy on 2018/9/18.
 */
public class CustomRealm extends AuthorizingRealm {

    Map<String,String> userMap = new HashMap<String, String>(16);

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

    /**
     * 做授權
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.從主體傳過來的認證資訊中,獲得使用者名
        String userName = (String)principalCollection.getPrimaryPrincipal();
        //2.從資料庫或者緩存中擷取角色資料
        Set<String> roles = getRolesByUserName(userName);
        //從資料庫或者緩存中擷取權限資料
        Set<String> permissions = getPermissionsByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);
        return simpleAuthorizationInfo;
    }

    /**
     * 做認證
     * @param authenticationToken
     * @return
     * @throws AuthenticationException:主體傳過來的認證資訊
     */
    @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(userName,password,"customRealm");
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(userName));
        return authenticationInfo;
    }

    /**
     * 模拟資料庫或緩存中擷取權限資料
     * @param userName
     * @return
     */
    private Set<String> getPermissionsByUserName(String userName) {
        Set<String> sets = new HashSet<String>();
        sets.add("user:delete");
        sets.add("user:add");
        return sets;
    }

    /**
     * 模拟從資料庫或者緩存中擷取角色
     * @param userName
     * @return
     */
    private Set<String> getRolesByUserName(String userName) {
        Set<String> sets = new HashSet<String>();
        sets.add("admin");
        sets.add("user");
        return sets;
    }

    /**
     * 模拟資料庫查詢憑證
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }
}
           

在com.amoscxy.test包中建立測試類:CustomRealmTest:

package com.amoscxy.test;

import com.amoscxy.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class CustomRealmTest {

	@Test
	public void testAuthentication(){
		
		CustomRealm customRealm = new CustomRealm();
		
		//1.建構SecurityManager
		DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
		defaultSecurityManager.setRealm(customRealm);

//		//Shiro加密
//		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.checkRole("admin");

		subject.checkPermissions("user:add","user:delete");
	
	}
}