package com.hangxintong.authorization.config;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
/**
* @Auther: tsj
* @Date: 2021/3/15 14:25
* @Description: 因为之前的密码都是使用jwt置换的密码 所以不适用BCryptPasswordEncoder
*/
@Component
public class JwtPasswordEncoder implements PasswordEncoder {
/**
* 进行加密的方法
*
* @param charSequence 未加密的密码
* @return
*/
@Override
public String encode(CharSequence charSequence) {
return passwordEncode(charSequence);
}
/**
* 进行密码比对的方法
*
* @param charSequence 未加密的密码
* @param s 加密的密码
* @return
*/
@Override
public boolean matches(CharSequence charSequence, String s) {
return passwordEncode(charSequence).equals(s);
}
/**
* 加密方法
*/
private String passwordEncode(CharSequence rawPassword){
String password = JWT.create().sign(Algorithm.HMAC256(rawPassword.toString()));
return password;
}
public static void main(String[] args) {
String password = JWT.create().sign(Algorithm.HMAC256("hxt-secret"));
System.out.println(password);
}
}
重写PasswordEncoder
发布于 2021-04-01 1674 次阅读
Comments | NOTHING