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 ...
随机推荐
- 10 个非常实用的 SVG 动画操作JavaScript 库
SVG 通常可以用作跨分辨率视频.这意味着在一块高分屏幕上不会降低图片的锐度.此外,你甚至可以让SVG动起来,通过使用一些javascript类库.下面,我们分享一些javascript类库,这些 ...
- JS判断元素是否在数组内
//判断元素是否在数组内 function contains(arr, obj) { var i = arr.length; while (i--) { if (arr[i] === obj) { r ...
- css3 二维码 添加 扫描特效
<section data-role="paragraph" class="_135editor" style="border: 0px non ...
- Html table、thead、tr、th、td 标签
Html table.thead.tr.th.td 标签 案例一 <!-- table 表格标签,配置表格使用.border="1" 添加表格框架 --> <ta ...
- final修饰符与多态
知识点一.final 最终的可以修饰属性.方法.类1.final修饰的属性,表示常量,初始化以后值不能改变.final修饰引用数据类型的变量,引用地址不能改变.2.final修饰类,不能被继承.比如: ...
- vue中父子组件之间的传值、非父子组件之间的传值
在Vue实例中每个组件之间都是相互独立的,都有自己的作用域,所以组件之间是不能直接获取数据.在项目开发中一个组件可能需要获取另一个组件的值,我们可以通过其他方法间接的获取.所以,获取的方法有以下几种: ...
- “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
概述 Material Design设计规范的受欢迎程度和实用性已经引起了 ComponentOne 技术团队的重视.ComponentOne Enterprise 2018V3 版本将全面支持Mat ...
- IdentityServer4 错误解决方案
错误 解决方案 Grant types list cannot contain both xx and xx 以下授权类型不能同时存在: Implicit AuthorizationCode Impl ...
- Visual Studio 实用扩展工具
Indent Guides 为每个缩进绘制一条虚线: Highlight all occurrences of selected word 高亮相关代码: Productivity Power Too ...
- React项目中使用HighCharts
大家都知道BizCharts是基于react封装的一套图表工具,而HighCharts是基于jQuery的.但是由于本人对BizCharts甚是不熟,所以在react项目开发中选择了HighChart ...