Java tutorial
package com.vico.license.aop; import com.github.pagehelper.StringUtil; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * @author Liu.Dun * Aop- */ @Aspect @Component public class SecurityAspect { private static final String DEFAULT_TOKEN_NAME = "X-TOKEN"; private TokenManager tokenManager; private String tokenName; public void setTokenManager(TokenManager tokenManager) { this.tokenManager = tokenManager; } public void setTokenName(String tokenName) { if (StringUtil.isEmpty(tokenName)) { tokenName = DEFAULT_TOKEN_NAME; } this.tokenName = tokenName; } // @Pointcut("execution(* com..controller.*Controller.*Aspect(..))") // private void pointCutMethod() { // } // // // @Pointcut("within(@org.springframework.stereotype.Controller *)") // public void thing() { // System.out.println("dsada"); // } // // @Pointcut("execution(* *(..))") // public void methodPointcut() {} @Pointcut("@annotation(com.vico.license.aop.NeedCheck)") public void needAnnotation() { } @Before("needAnnotation()") public void before(JoinPoint joinPoint) throws Throwable { //Before?JoinPoint,?ProceedingJoinPoint System.out.println("=====SysLogAspect ?====="); } //@Around("com.vico.license.controller.licenseController() && @annotation(com.vico.license.aop.IgnoreSecurity)") //@Around("execution(@com.vico.license..NeedCheck * *(..)) && @annotation(com.vico.license.aop.NeedCheck)") //@Around("thing() && methodPointcut() && @annotation(org.springframework.web.bind.annotation.RequestMapping)") //@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)") @Around("needAnnotation()") public Object execute(ProceedingJoinPoint pjp) throws Throwable { System.out.println("=====SysLogAspect ====="); //?? MethodSignature methodSignature = (MethodSignature) pjp.getSignature(); Method method = methodSignature.getMethod(); //,(ignore?) if (method.isAnnotationPresent(IgnoreSecurity.class)) { return pjp.proceed(); } //request header??token System.out.println(WebContext.getRequest()); String token = WebContext.getRequest().getHeader(tokenName); //, System.out.println(token); //token if (!tokenManager.checkToken(token)) { String message = String.format("token [%s] is invalid", token); throw new TokenException(message); } // return pjp.proceed(); } }