Shiro 认证 密码加密

shiro 加密(非xml配置):

public class EncrpyPassword {

    @Test
    public void EncrpyPassword(){

        CustomRealm customRealm = new CustomRealm();
        //构建SecurityManager
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(customRealm);

        //密码加密
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("MD5");
        matcher.setHashIterations(1);

        customRealm.setCredentialsMatcher(matcher);

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

        //主体提交认证请求
        UsernamePasswordToken token = new UsernamePasswordToken("yan","123");
        subject.login(token);

        //subject.checkRole("admin");

    }

模拟测试类:

public class CustomRealm extends AuthorizingRealm {
    Map<String, String> map = new HashMap<String, String>();
    {
        map.put("yan", "202cb962ac59075b964b07152d234b70");
        super.setName("customRealm");
    }

    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) principals.getPrimaryPrincipal();
        Set roles = getRolesByUsername(username);
        Set permissions = getPermissionsByUsername(username);

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

        return simpleAuthorizationInfo;
    }

    private Set getPermissionsByUsername(String username) {
        Set lists = new HashSet();
        lists.add("user:add");
        lists.add("user:delete");
        return lists;
    }

    private Set getRolesByUsername(String username) {
        Set lists = new HashSet();
        lists.add("admin");
        lists.add("user");
        return lists;
    }

    //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户名
        String username = (String) token.getPrincipal();

        //数据库查询密码
        String password = getPasswordByUsername(username);
        if(password ==null){
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("yan",password,"customRealm");
        return simpleAuthenticationInfo;
    }

    //模拟获取密码
    private String getPasswordByUsername(String username) {
        return map.get(username);
    }

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

 

转载于:https://my.oschina.net/fendouan/blog/2209758

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值