菜鸟学习shiro之实现自定义的Realm,从而实现登录验证,身份验证和权限验证4

  1. 云栖社区>
  2. 博客>
  3. 正文

菜鸟学习shiro之实现自定义的Realm,从而实现登录验证,身份验证和权限验证4

上古休斯 2018-07-28 08:38:21 浏览827
展开阅读全文

讲了那么多使用的内置的类从而实现四郎,现在讲自定义的境界

首先行家的依赖依然是第一篇的那个依赖

下边是自定义的境界:

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");*/

    }

}

代码讲的很清楚了,我就不多说了。

网友评论

登录后评论
0/500
评论
上古休斯
+ 关注