基于SSM的RBAC权限系统(4)-巧用Shiro自带加密

本文介绍如何在SSM框架中利用Shiro进行密码加密,通过具体代码示例展示如何在用户注册和登录时实现密码的安全存储。文章还提供了完整的项目地址供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于SSM的RBAC权限系统(4)-巧用Shiro自带加密

背景

在打算用Shiro做密码加密的时候,一开始Shiro只能在验证登录口令时Shiro才拿加密密码以及自己从数据库的密码去匹配,但是在注册、添加、编辑需要再次加密的时候却找不到对应的办法,难道要我自己写个方法加密?但是既然Shiro有对登录密码进行加密,那么肯定就有加密的地方,跟着调试器debug进去,果然发现了这些方法。

然后,问题就解决了。

        ByteSource credentialsSalt = ByteSource.Util.bytes(record.getAccount());
        SimpleHash simpleHash = new SimpleHash("MD5",  record.getPassword(), credentialsSalt, 16);
        record.setPassword(simpleHash.toString());

附上认证的配置

    <bean id="jdbcRealm" class="cn.etop.rbac.common.shiro.realms.ShiroRealm">
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="MD5"></property>
                <property name="hashIterations" value="16"></property>
            </bean>
        </property>
    </bean>
   @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
        String username = upToken.getUsername();

        boolean isExit=false;
        try {
            isExit = userService.checkUserNameExit(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(isExit){
            throw new UnknownAccountException("用户不存在!");
        }
        User temp=null;
        try {
            temp=loginServiceImpl.getUserByAccount (username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Object principal = username;
        Object credentials = temp.getPassword();
        String realmName = getName();
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo info = null;
        info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt ,realmName);
        return info;

    }

完整项目地址

这是我第一个写的web项目,代码烂得飞起,仅供纪念,不做参考
带Shiro版:https://github.com/EnTaroAdunZ/ssm_rbac_shiro.git
不带Shiro版:https://github.com/EnTaroAdunZ/ssm_rbac.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值