一、移入依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

二、自定义注解:

/**
* @Description
* @Date: 2018/12/13
*/ import java.lang.annotation.*; @Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Admin {
String value() default "";
}

三、AOP切面配置

package com.hsfw.backyard.web.vo;

/**
* @Description
* @Date: 2018/12/13
*/ 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.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component; @Aspect
@Component
public class AdminAspect {
@Pointcut(value = "@annotation(com.hsfw.backyard.web.vo.Admin)")
public void annotationPointCut() {
} @Around("annotationPointCut()")
public Object doAround(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();
System.out.println("方法名:" + methodName);
if (!validate()) {
return "没有权限";
}
try {
return joinPoint.proceed();
} catch (Throwable throwable) {
return null;
}
} private boolean validate() {
// TODO 实现自己的鉴权功能
return false;
}
}

四、controller测试

package com.hsfw.backyard.web.vo;

/**
* @Description
* @Date: 2018/12/13
*/ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
/**
* 访问:http://localhost:8080/login 可以直接访问成功。
* 访问:http://localhost:8080/refund 由于加了@Admin注解,需要验证权限
*/
public class AdminController {
@GetMapping("/login")
public String login() {
return "登录成功!";
} @RequestMapping("/refund")
@Admin
public String refund() {
return "退款成功";
} }

五、启动方法

package com.hsfw.backyard.web.vo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class TestAopApplication {
public static void main(String[] args) {
SpringApplication.run(TestAopApplication.class, args);
}
}

最新文章

  1. I/O 函数总结
  2. 十大Intellij IDEA快捷键(转)
  3. 关于HIVE的数据模型介绍
  4. JS动态获取数据
  5. 彻底解决Android SDK Manager更新慢的问题
  6. 使用logstash+elasticsearch+kibana快速搭建日志平台
  7. 夺命雷公狗—angularjs—3—表单验证的高级用法
  8. js正则标志/g /i /m的用法,以及实例
  9. HTML5 CANVAS 高级
  10. 更新Code First生成的数据库
  11. 使用curl操作openstack swift
  12. C语言初学 计算三角形面积问题
  13. WebService使用的一些总结
  14. 拯救莫莉斯[GDOI2014]
  15. 京东首页原生----js制作|css动画|js动画|计时器--轮播图(好久没更新,这两天闲的蛋疼做个京东页面分辨率1366*768,919京东,适应没调!)要文件加关注找我要哦!
  16. 递归回溯 UVa140 Bandwidth宽带
  17. centos7之zabbix的web检测
  18. 关于Java中static关键字的用法以及从内存角度解析
  19. 理解block和inode
  20. Compile pam with pam_cracklib.so

热门文章

  1. ansible笔记(11):初识ansible playbook(二)
  2. 前端lvs访问多台nginx代理服务时出现404错误的处理
  3. php链式调用(链式操作)
  4. [转]Fiddler模拟post四种请求数据
  5. 21)django-csrf(跨站请求伪造)
  6. hive学习04-员工部门表综合案例
  7. 公历和农历转换的JS代码(车)
  8. FileStorage
  9. Swift 学习- 04 -- 字符串和字符
  10. XmlDocument.Load(url) 本地和http远程