shiro初识

shiro:Java安全框架,有身份验证、授权、密码学和会话管理

Spring security 重量级安全框架(配置很麻烦 做的比较细)
Apache Shiro轻量级安全框架 (配置很容易 很方便,很容易使用)
在这里插入图片描述 自定义Realm

授权:

String username = (String) principalCollection.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> getRolesByUsername(String username) {
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        roles.add("it");
        return roles;
    }
    //模拟根据用户名拿到权限的功能
    private Set<String> getPermissionsByUsername(String username) {
        Set<String> permissions = new HashSet<>();
        permissions.add("employee.*");
        permissions.add("department.save");
        return permissions;
    }

身份认证:

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;

        String usernaem=token.getUsername();

        String password=findUserByUsername(usernaem);

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

        ByteSource salt = ByteSource.Util.bytes("itsource");

        SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(usernaem,password,salt,getName());
        return simpleAuthenticationInfo;
    }
    

 

加密加盐:

    public static void main(String[] args){

        SimpleHash simpleHash=new SimpleHash("MD5", "th", "hahahhs",10);

        System.out.println(simpleHash.toString());

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值