Springboot+aop+annotation

一、不结合注解方式的aop

1、首先要见一个springboot的项目

 

2、pom.xml中引入包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.3.1.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <exclusions>
            <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jetty</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

3、配置application.properties

4、编写Aspect类

package owner;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogOwnerAspect {


    @Pointcut("execution(public * owner.*.*(..))")
    public void execute(){}


    @Before("execute()")
    public void before(){
        System.out.println("LogOwnerAspect方法执行前");
    }

    @Around("execute()")
    public Object around(ProceedingJoinPoint pjd) throws  Throwable{
        System.out.println("LogOwnerAspect环绕执行");
        Object res= pjd.proceed();
        System.out.println("LogOwnerAspect环绕执行hou"+res);
        return res;
    }

    @After("execute()")
    public void after(){
        System.out.println("LogOwnerAspect方法执行后");
    }

  /*  @Around("execute()")
    public void around(ProceedingJoinPoint pjd) throws  Throwable{
        System.out.println("环绕执行");
        Object res= pjd.proceed();
        System.out.println("环绕执行2"+res);

    }*/
}

5、编写后面的类

package owner;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Owner")
public class UserOwnerController {

    @RequestMapping("/first")
    public String first(){
        return "first Controller";
    }
    @RequestMapping("/second")
    @UserAccessOwner()
    public String second(){
        return "second Controller";
    }
}

6、编写启动类

package owner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OwnerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OwnerApplication.class);
    }
}

7、运行OwnerApplication,访问http://localhost:8092/Owner/first

二、结合注解aop

1、首先要见一个springboot的项目

 

2、pom.xml中引入包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.3.1.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>org.junit.vintage</groupId>
               <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
         </exclusions>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <exclusions>
            <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jetty</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-aop</artifactId>
      </dependency>

   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

3、配置application.properties

4、编写注解

package owner;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface UserAccessOwner {
    String value() default "无信息";
}

5、编写Aspect类

package owner;

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;

import java.lang.reflect.Method;

@Aspect
@Component
public class UserAccessOwnerAspect {

    @Pointcut("@annotation(owner.UserAccessOwner)")
    public void access(){}

    @Around("access()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("执行前");
        Method method =((MethodSignature)joinPoint.getSignature()).getMethod();
        /*System.out.println(method.getName());
        System.out.println(method.getAnnotation(UserAccessOwner.class));*/
        UserAccessOwner userAccessOwner= method.getAnnotation(UserAccessOwner.class);
        System.out.println("second around:"+userAccessOwner.value());
        System.out.println(method.getDeclaringClass());
        Object object=joinPoint.proceed();
        System.out.println("执行后"+object);
        return object;
    }
}
6、编写后面的类
package owner;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Owner")
public class UserOwnerController {

    @RequestMapping("/first")
    public String first(){
        return "first Controller";
    }
    @RequestMapping("/second")
    @UserAccessOwner("自己的")
    public String second(){
        return "second Controller";
    }
}

7、编写启动类

package owner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OwnerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OwnerApplication.class);
    }
}

8、运行OwnerApplication,访问http://localhost:8092/Owner/second

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot AOP可以用来实现基于注解的权限控制。通过在需要进行权限控制的方法上添加注解,然后使用AOP来拦截这些方法,从而实现对访问权限的控制。 以下是实现基于注解的权限控制的步骤: 1. 定义一个注解,用于标注需要进行权限控制的方法。例如: ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RequiresPermission { String value() default ""; } ``` 2. 在需要进行权限控制的方法上添加注解。例如: ```java @RequiresPermission("admin") public void deleteItem(int id) { // ... } ``` 3. 编写一个AOP切面,用于拦截带有注解的方法,并进行权限验证。例如: ```java @Aspect @Component public class PermissionAspect { @Autowired private HttpServletRequest request; @Around("@annotation(com.example.demo.annotation.RequiresPermission)") public Object checkPermission(ProceedingJoinPoint joinPoint) throws Throwable { String permission = joinPoint.getTarget().getClass().getAnnotation(RequiresPermission.class).value(); if (request.isUserInRole(permission)) { return joinPoint.proceed(); } else { throw new AccessDeniedException("Access Denied"); } } } ``` 4. 在应用程序的配置类中启用AOP自动代理。例如: ```java @EnableAspectJAutoProxy @SpringBootApplication public class Application { // ... } ``` 这样,就可以通过注解来实现基于权限的访问控制了。在需要进行权限控制的方法上添加注解,AOP切面会自动拦截这些方法,并进行权限验证。如果用户没有相应的权限,则会抛出AccessDeniedException异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值