目标:通过注解参数的修改,在不改动AOP的情况下,适配不同的加解密和验签需求,在AOP中对加密密文解密后传递到接口。
在使用过程中,具体实现加解密的方法必须实现定义的接口,需要加密的某个参数实体类需要继承定义的基本实体类。
SpringUtil工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
logger.info("ApplicationContext配置成功,applicationContext对象:{}",SpringUtil.applicationContext);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name,Class<T> clazz) {
return getApplicationContext().getBean(name,clazz);
}
}
密文实体类
@Data
public class BaseVO {
// 加密密文
private String encryptStr;
}
需要解密的实体类,继承密文实体类,也可以不继承实体类,不继承实体类在使用的时候,需要在该实体类中新增一个密文字段,在接口中注明,AOP就会直接从该实体类中获取加密密文。
继承baseVO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BusinessVO extends BaseVO{
private String id;
private String name;
}