前后端分离项目整合的最后一步就是密码加密,把密码加密放到最后,是因为怕忘记了密码等不不上去 :) :)
密码加密
话不多说了
直接上代码
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,要不然你可能会遇到怎么也验证不通过的情况。。哈哈
总结
其实在项目中一直都是前后端分离,也停留在会用的阶段。在网上看了很多文章,大概知道怎么去实现,但是只有亲自去写了才知道可能会遇到的问题。遇到问题,顺着思路解决,解决的多了,就是自己的经验