前后端分离项目 springboot+ant design pro of vue(5)

本文详细介绍了如何在前后端分离项目中实现密码加密,包括自定义MyByteSource类、MD5加密算法的应用,以及如何在Shiro Realm中整合。通过实例代码展示了如何在认证过程中添加salt,并分享了项目实战经验和遇到的问题解决过程。

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

前后端分离项目整合的最后一步就是密码加密,把密码加密放到最后,是因为怕忘记了密码等不不上去   :)   :)

密码加密

话不多说了

直接上代码

MyByteSource

主要是需要ByteSource实现序列化接口

package com.han.springbootshiro.config.shiro;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;

/**
 自定义salt的实现 主salt的序列化
 */
public class MyByteSource implements ByteSource, Serializable {



    /*这里将final去掉了,去掉后要在后面用getter和setter赋、取值*/
    private byte[] bytes;
    private String cachedHex;
    private String cachedBase64;

    /*添加了一个无参构造方法*/
    public MyByteSource(){}

    public MyByteSource(byte[] bytes) {
        this.bytes = bytes;
    }

    public MyByteSource(char[] chars) {
        this.bytes = CodecSupport.toBytes(chars);
    }

    public MyByteSource(String string) {
        this.bytes = CodecSupport.toBytes(string);
    }

    public MyByteSource(ByteSource source) {
        this.bytes = source.getBytes();
    }

    public MyByteSource(File file) {
        this.bytes = (new BytesHelper()).getBytes(file);
    }

    public MyByteSource(InputStream stream) {
        this.bytes = (new BytesHelper()).getBytes(stream);
    }

    public static boolean isCompatible(Object o) {
        return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
    }


    /*这里加了getter和setter*/
    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public byte[] getBytes() {
        return this.bytes;
    }

    public boolean isEmpty() {
        return this.bytes == null || this.bytes.length == 0;
    }

    public String toHex() {
        if (this.cachedHex == null) {
            this.cachedHex = Hex.encodeToString(this.getBytes());
        }

        return this.cachedHex;
    }

    public String toBase64() {
        if (this.cachedBase64 == null) {
            this.cachedBase64 = Base64.encodeToString(this.getBytes());
        }

        return this.cachedBase64;
    }

    public String toString() {
        return this.toBase64();
    }

    public int hashCode() {
        return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof ByteSource) {
            ByteSource bs = (ByteSource)o;
            return Arrays.equals(this.getBytes(), bs.getBytes());
        } else {
            return false;
        }
    }

    private static final class BytesHelper extends CodecSupport {
        private BytesHelper() {
        }

        public byte[] getBytes(File file) {
            return this.toBytes(file);
        }

        public byte[] getBytes(InputStream stream) {
            return this.toBytes(stream);
        }
    }

    /*取代原先加盐的工具类*/
    public static class Util{
        public static ByteSource bytes(byte[] bytes){
            return new MyByteSource(bytes);
        }

        public static ByteSource bytes(String arg0){
            return new MyByteSource(arg0);
        }
    }
}

shiroConfig 注入加密算法,并且加入到realm中

/**
     * 加密算法
     * @return
     */
    public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("MD5");
        credentialsMatcher.setHashIterations(hashIterations);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        log.info("hashIterations:"+hashIterations);
        return credentialsMatcher;
    }

@Bean
    public Realm getRealm(){
        ShiroRealm shiroRealm = new ShiroRealm();
        //使用redis 作为缓存
        shiroRealm.setCacheManager(redisCacheManager());
        shiroRealm.setAuthenticationCachingEnabled(true);
        shiroRealm.setAuthenticationCacheName("authentication");
        shiroRealm.setAuthorizationCachingEnabled(true);
        shiroRealm.setAuthorizationCacheName("authorization");
        shiroRealm.setCredentialsMatcher(credentialsMatcher());

        return shiroRealm;
    }

认证时加上salt

/**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String principal = (String) token.getPrincipal();
        System.out.println("principal:"+principal);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,principal);
        User getUser = userService.getOne(queryWrapper);
        System.out.println(getUser);
        if(null !=getUser){
            return new SimpleAuthenticationInfo(getUser,getUser.getPassword(),MyByteSource.Util.bytes("salt"),this.getName());
        }
        return null;
    }

结束。

测试的时候一定要先退出,清空redis,要不然你可能会遇到怎么也验证不通过的情况。。哈哈

总结

其实在项目中一直都是前后端分离,也停留在会用的阶段。在网上看了很多文章,大概知道怎么去实现,但是只有亲自去写了才知道可能会遇到的问题。遇到问题,顺着思路解决,解决的多了,就是自己的经验

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值