spring注解实现防盗链拦截
首先配置 applicationContext.xml, 添加
<!-- 启用 @AspectJ -->
<aop:aspectj-autoproxy />
新建Java工具类 util.java,获取referer信息
/**
* Title:工具类
* @author Victor
*/
public class util {
/**
* @description 获取referer,实现防盗链
* @param request
* @return String host
*/
public static String getReferer(HttpServletRequest request) {
String referer = request.getHeader("referer");
if(referer == null) {
return "nullReferer";
}
// 提取域名
try {
URL referUrl = new URL(referer);
String host = referUrl.getHost();
return host;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return "nullReferer";
}
}
新建 annotation 注解接口,实现自定义注解 AntitheftChain.java
/**
* Title:自定义注解
* Description: 标识是是否开启防盗链检查
* @author Victor
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AntitheftChain { }
了解更多关于 annotation 注解的知识,转至:https://www.cnblogs.com/victorlyw/articles/9969072.html
新建java类 SecurityAspect.java 实现安全检查
/**
* Title:安全检查切面(是否登录检查)
* @author Victor
*/
@Component
@Aspect
public class SecurityAspect {
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object execute(ProceedingJoinPoint pjp) throws Throwable {
// 从切点上获取目标方法
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
// 目标方法是否开启防盗链检查
if (method.isAnnotationPresent(AntitheftChain.class)) {
// 获取请求域名
String getDomain = util.getReferer(WebContextUtil.getRequest());
if (getDomain == null || !getDomain.startsWith("localhost")) {
throw new domainException("没有认证域名");
}
}
}
}
新建 java类 domainException.java 异常处理
/**
* Title:盗链异常处理
* @author Victor
*/
public class domainException extends RuntimeException {
private static final long serialVersionUID = 1L; private String msg; public DomainException(String msg) {
super();
this.msg = msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
以上异常可以统一处理
spring注解实现防盗链拦截的更多相关文章
- Spring AOP 源码分析 - 拦截器链的执行过程
1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
- Spring注解式事务解析
#Spring注解式事务解析 增加一个Advisor 首先往Spring容器新增一个Advisor,BeanFactoryTransactionAttributeSourceAdvisor,它包含了T ...
- Spring Boot实践——三种拦截器的创建
引用:https://blog.csdn.net/hongxingxiaonan/article/details/48090075 Spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可 ...
- 防盗链&CSRF&API接口幂等性设计
防盗链技术 CSRF(模拟请求) 分析防止伪造Token请求攻击 互联网API接口幂等性设计 忘记密码漏洞分析 1.Http请求防盗链 什么是防盗链 比如A网站有一张图片,被B网站直接通过img标签属 ...
- SpringBoot集成FastDFS+Nginx整合基于Token的防盗链
为什么要用SpringBoot? SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- 0、Spring 注解驱动开发
0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...
- 【Spring注解驱动开发】AOP核心类解析,这是最全的一篇了!!
写在前面 昨天二狗子让我给他讲@EnableAspectJAutoProxy注解,讲到AnnotationAwareAspectJAutoProxyCreator类的源码时,二狗子消化不了了.这不,今 ...
- 【spring 注解驱动开发】Spring AOP原理
尚学堂spring 注解驱动开发学习笔记之 - AOP原理 AOP原理: 1.AOP原理-AOP功能实现 2.AOP原理-@EnableAspectJAutoProxy 3.AOP原理-Annotat ...
- 【spring 注解驱动开发】spring事务处理原理
尚学堂spring 注解驱动开发学习笔记之 - 事务处理 事务处理 1.事务处理实现 实现步骤: * 声明式事务: * * 环境搭建: * 1.导入相关依赖 * 数据源.数据库驱动.Spring-jd ...
随机推荐
- egg.js基础入门
之前一直使用koa, 刚刚接触egg, 做了一些入门的笔记 准备工作 1 首先安装脚手架,,并创建项目. $ npm i egg-init -g $ egg-init egg-demo --type ...
- PyQt5学习笔记
setMouseTracking bool mouseTracking这个属性保存的是窗口部件跟踪鼠标是否生效.如果鼠标跟踪失效(默认),当鼠标被移动的时候只有在至少一个鼠标按键被按下时,这个窗口部件 ...
- windows安装composer总结
1.直接去网吧下载windows安装EXE程序,傻瓜式安装,so easy. 2.通过命令行安装,可以直接在php目录跑起来 php -r "readfile('https://getcom ...
- undefined 和null的区别
undefined 和null的区别null是一个表示"无"的对象,转为数值时为0:undefined是一个表示"无"的原始值,转为数值时为NaN.undefi ...
- Mysql 数据库介绍
数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API接口用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中, ...
- wget下载阿里云RDS备份集
[root@localhost tmp]# more wget.sh #!/bin/bash download_url=`python /tmp/geturl.py` echo $download_u ...
- MyBio小隐本记注册破解
既然开始了,就把这一个系列的都破了算了,这次主角小隐本记MyBio 和WDTP的原理是差不多的,先把软件界面换成e文,然后写了15个记录后提示注册,一样的路子,直接跳过注册窗口的弹出就好了 然后查壳一 ...
- No executable found matching command "dotnet-aspnet-codegenerator"
vs2017,mvc新建控制器报错. 右键项目,选择编辑工程文件(.csproj),加入以下节点 <ItemGroup> <DotNetCliToolReference Includ ...
- 在 jupyter 中添加菜单和自动完成功能
在 jupyter 中添加菜单和自动完成功能 参考文档http://www.360doc.com/content/17/1103/14/1489589_700569828.shtmlhttp://to ...
- idea下使用Git基本操作(转载)
IDEA中Git的更新.提交.还原方法: https://blog.csdn.net/geng31/article/details/78585557 IDEA中Git的提交到GitHub http:/ ...