定义一个方法注解,要求登录才能访问
import java.lang.annotation.*;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresLogin {
}
在controller方法上加上注解 @RequiresLogin
import com.example.demo.annotation.RequiresLogin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RequiresLogin
@GetMapping("/annotation")
public String getLoginInfo() {
System.out.println("Into method.");
return "OK";
}
使用切面,发挥注解作用
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.re