Spring案例:百度网盘密码数据兼容处理

简介: Spring案例:百度网盘密码数据兼容处理

需求:对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理

d10bf5740372484d94ac962aec14a024.png

分析:

1、在业务方法执行之前对所有的输入参数进行格式出——trim()

2、使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用


代码实现:


ResourcesService业务层接口:

public interface ResourcesService {
    public boolean openURL(String url ,String password);
}

ResourcesServiceImpl业务层实现类:

import com.itheima.dao.ResourcesDao;
import com.itheima.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ResourcesServiceImpl implements ResourcesService {
    @Autowired
    private ResourcesDao resourcesDao;
    public boolean openURL(String url, String password) {
        return resourcesDao.readResources(url,password);
    }
}

ResourcesDao数据层接口:

public interface ResourcesDao {
    boolean readResources(String url, String password);
}

ResourcesDaoImpl数据层实现类:模拟校验

import com.itheima.dao.ResourcesDao;
import org.springframework.stereotype.Repository;
@Repository
public class ResourcesDaoImpl implements ResourcesDao {
    public boolean readResources(String url, String password) {
        //模拟校验
        return password.equals("root");
    }
}

SpringConfig配置类:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}

DataAdvice的aop类:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class DataAdvice {
    @Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))")
    private void servicePt(){}
    @Around("DataAdvice.servicePt()")
    public Object trimStr(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        for (int i = 0; i < args.length; i++) {
            //判断参数是不是字符串
            if(args[i].getClass().equals(String.class)){
                //取出数据,trim()操作后,更新数据
                args[i] = args[i].toString().trim();
            }
        }
        Object ret = pjp.proceed(args);
        return ret;
    }
}

App运行类:

import com.itheima.config.SpringConfig;
import com.itheima.service.ResourcesService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        ResourcesService resourcesService = ctx.getBean(ResourcesService.class);
        boolean flag = resourcesService.openURL("http://pan.baidu.com/haha", "root ");
    }
}

执行结果:

a58ed011b36e4a68977d5ca8a4475eaf.png

可以看出,不管加不加空格,对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理的需求已经解决。如果有些相同的大量使用的功能,需要在很多地方加的话,使用AOP是你的不二选择。使用AOP能简化你的共性功能的开发。

目录
相关文章
|
30天前
|
存储 运维 Java
使用 @Audited 增强Spring Boot 应用程序的数据审计能力
【7月更文挑战第19天】在Spring Boot应用中使用`@Audited`增强数据审计能力涉及在相关实体或方法上添加该注解以标记需审计的操作。例如,在`User`类的`updateUser`方法上使用`@Audited`可记录更新操作的详情。此外,还需配置审计日志存储方式(如数据库)及事件过滤规则等。这有助于满足合规性需求、故障排查及数据分析,对数据安全和完整至关重要。
|
10天前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
2天前
|
JSON Java API
哇塞!Spring Boot 中的 @DateTimeFormat 和 @JsonFormat,竟能引发数据时间大变革!
【8月更文挑战第29天】在Spring Boot开发中,正确处理日期时间至关重要。
|
16天前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
27 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
23天前
|
存储 小程序 C语言
C语言数据的存储(内含百度笔试题)
C语言数据的存储(内含百度笔试题)
29 4
|
23天前
|
数据采集 存储 自然语言处理
【优秀python案例】基于百度贴吧的数据采集与文本分析设计与实现
本文介绍了百度贴吧数据采集与文本分析的设计与实现,包括自动化采集帖子数据、进行情感分析和主题分析,以及使用可视化技术展示分析结果。
|
29天前
|
JSON 前端开发 Java
Spring MVC返回JSON数据
综上所述,Spring MVC提供了灵活、强大的方式来支持返回JSON数据,从直接使用 `@ResponseBody`及 `@RestController`注解,到通过配置消息转换器和异常处理器,开发人员可以根据具体需求选择合适的实现方式。
57 4
|
15天前
|
Java API Spring
Spring5入门到实战------1、Spring5框架概述、入门案例
这篇文章是Spring5框架的入门教程,概述了Spring框架的核心概念和特点,并通过一个创建普通Java类的案例,详细演示了从下载Spring核心Jar包、创建配置文件、编写测试代码到运行测试结果的完整流程,涵盖了Spring IOC容器的使用和依赖注入的基本用法。
|
27天前
|
存储 Java 数据库
使用 @Audited 增强Spring Boot 应用程序的数据审计能力
【8月更文挑战第3天】在Spring Boot应用中,`@Audited`注解能显著提升数据审计能力。它可用于标记需审计的方法或类,记录操作用户、时间和类型等信息。此注解支持与Logback或Log4j等日志框架集成,亦可将审计信息存入数据库,便于后续分析。此外,还支持自定义审计处理器以满足特定需求。
|
2月前
|
存储 Java 数据库
如何在Spring Boot中实现多租户数据隔离
如何在Spring Boot中实现多租户数据隔离
下一篇
云函数