shiro 自定义Realm和加密

在这里插入图片描述

1. 自定义Realm

public class CustomRealm extends AuthorizingRealm {

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

    {
        // 123456
        userMap.put("admin", "a66abb5684c45962d887564f08346e8d");
        super.setName("customRealm");
    }

    // 授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) principals.getPrimaryPrincipal();
        // 获取角色
        Set<String> roles = getRolesByUsername(username);
        Set<String> permissions = getPermissionsByUsername(username);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(roles);
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

    private Set<String> getPermissionsByUsername(String username) {
        Set<String> permissionMap = new HashSet<String>();
        permissionMap.add("user:select");
        permissionMap.add("user:add");
        permissionMap.add("user:update");
        permissionMap.add("user:delete");
        return permissionMap;
    }

    private Set<String> getRolesByUsername(String username) {
        Set<String> roleMap = new HashSet<String>();
        roleMap.add("admin");
        return roleMap;
    }

    // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        // 从主体传过来的认证信息中,获取用户名
        String username = (String) token.getPrincipal();

        // 通过用户名到数据库中获取凭证
        String password = getPasswordByUsername(username);

        if (password == null)
            return null;

        SimpleAuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo(username, password, getName());

        // 使用admin对密码进行加密
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("admin"));

        return authenticationInfo;
    }

    /**
     * 模拟数据库查询凭证
     * @param username
     * @return
     */
    private String getPasswordByUsername(String username) {
        return userMap.get(username);
    }
}

2、测试代码

public class CustomRealmTest {

    // 自定义Realm认证测试
    @Test
    public void testAuthentication() {

        CustomRealm customRealm = new CustomRealm();

        // 1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(customRealm);

        // 2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
        subject.login(token);

        System.out.println("是否认证:" + subject.isAuthenticated());

        subject.checkRole("admin");
//        subject.checkPermission("user:select");
        subject.checkPermissions("user:select", "user:add");

    }
        public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("123456", "Mark");
        System.out.println(md5Hash.toString());
    }

加密测试


	// 加密
    @Test
    public void testAuthentication2() {

        CustomRealm customRealm = new CustomRealm();
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        // 加密类型
        matcher.setHashAlgorithmName("md5");
        // 加密次数
        matcher.setHashIterations(1);
        customRealm.setCredentialsMatcher(matcher);

        // 1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(customRealm);

        // 2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("admin", "123456");
        subject.login(token);

        System.out.println("是否认证:" + subject.isAuthenticated());

        subject.checkRole("admin");
		// subject.checkPermission("user:select");
        subject.checkPermissions("user:select", "user:add");

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值