百度网盘分享连接输入密码数据错误兼容处理
🚸🚸
需求:对百度网盘分享链接输入提取码时尾部多输入的空格做兼容处理
🎊🎊️️
分析:
①:在业务方法执行之前对所有的输入参数进行格式处理—— trim()
【trim是String类型数据的一个方法,Java中的<font color=red> trim() </font>⽅法是⽤于删除字符串的头尾空⽩符(只有头和尾),返回值是删除头尾空⽩符的字符串】
②:使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用
🍭🍭
实现:
【读取链接与提取码并返回提取码的布尔值】
package com.GY.dao;
public interface dataDao {
boolean readdata(String url,String password) ;
}
package com.GY.dao;
import org.springframework.stereotype.Repository;
@Repository
public class dataDaoImpl implements dataDao {
@Override
public boolean readdata(String url, String password) {
return password.equals("123456");
}
}
【判断链接与提取码是否正确】
package com.GY.service;
public interface dataService {
boolean URL(String url,String password);
}
package com.GY.service;
import com.GY.dao.dataDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class dataServiceImpl implements dataService{
@Autowired
private dataDao data;
public boolean URL(String url, String password) {
return data.readdata(url,password);
}
}
【Spring配置类】
package com.GY.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.*")
@EnableAspectJAutoProxy
public class SpringConfig {
}
【Spring切面】
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Advice {
@Pointcut("execution( boolean com.GY.service.dataService.URL(*,*))")
private void G(){}
@Around("G()")
public Object method(ProceedingJoinPoint P) throws Throwable {
//取出参数(若有空格则去掉)
Object[]arg= P.getArgs();
for (int i = 0; i <arg.length ; i++) {
//判断参数是否为字符串
if( arg[i].getClass().equals(String.class)){
arg[i]=arg[i].toString().trim();
}
}
//将修改完的参数放回
Object O= P.proceed(arg);
return O;
}
}
import com.GY.config.SpringConfig;
import com.GY.service.dataService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class GY {
public static void main(String[] args) {
ApplicationContext A=new AnnotationConfigApplicationContext(SpringConfig.class);
dataService dataService=A.getBean(com.GY.service.dataService.class);
Boolean GY=dataService.URL("http://www.GY.com","123456 ");
System.out.println(GY);
}
}
🛳️🛳️
结果:(提取码首尾存在空格时也可正常进行提取)