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

简介: 讲了那么多使用的内置的类从而实现四郎,现在讲自定义的境界首先行家的依赖依然是第一篇的那个依赖下边是自定义的境界:import org.

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

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

下边是自定义的境界:

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

    }

}

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

相关文章
|
3月前
|
数据库 数据安全/隐私保护
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(二)-全面详解(学习总结---从入门到深化)
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(二)-全面详解(学习总结---从入门到深化)
66 0
|
3月前
|
数据库 数据安全/隐私保护
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(四)-全面详解(学习总结---从入门到深化)
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(四)-全面详解(学习总结---从入门到深化)
31 1
|
5月前
Shrio配置多个Realm、SecurityManager认证策略
Shrio配置多个Realm、SecurityManager认证策略
|
数据库 数据安全/隐私保护
【Shiro】3、Shiro实现自定义密码验证规则
我们在使用 Shiro 实现登录的时候,我们只需要将账号、密码,Shiro 会自动判断账户、密码是否正确,那么 Shiro 怎么会知道我们的密码加密规则呢?所以我们需要自定义密码的加密规则
155 0
|
安全 Java 数据库连接
【Shiro】1、Shiro实现登录授权认证功能(上)
之前在 SSM 项目中使用过 shiro,发现 shiro 的权限管理做的真不错,但是在 SSM 项目中的配置太繁杂了,于是这次在 SpringBoot 中使用了 shiro,下面一起看看吧
261 0
|
Java 数据安全/隐私保护
【Shiro】1、Shiro实现登录授权认证功能(中)
之前在 SSM 项目中使用过 shiro,发现 shiro 的权限管理做的真不错,但是在 SSM 项目中的配置太繁杂了,于是这次在 SpringBoot 中使用了 shiro,下面一起看看吧
110 0
|
Java 数据安全/隐私保护
【Shiro】1、Shiro实现登录授权认证功能(下)
之前在 SSM 项目中使用过 shiro,发现 shiro 的权限管理做的真不错,但是在 SSM 项目中的配置太繁杂了,于是这次在 SpringBoot 中使用了 shiro,下面一起看看吧
99 0
|
Java 数据安全/隐私保护
【Shiro】Shiro从小白到大神(三)-权限认证(授权)-1
【Shiro】Shiro从小白到大神(三)-权限认证(授权)-1
248 0
|
Java Apache 数据安全/隐私保护
【Shiro】Shiro从小白到大神(三)-权限认证(授权)-2
【Shiro】Shiro从小白到大神(三)-权限认证(授权)-2
132 0
|
缓存 数据库 数据安全/隐私保护
Shiro自定义Realm实现认证和授权(五)上
Shiro自定义Realm实现认证和授权(五)
290 0
Shiro自定义Realm实现认证和授权(五)上