@Aspect @Component public class MyAspect { public MyAspect() { logger.info("MyAspect !!"); } static final Logger logger = LoggerFactory.getLogger(MyAspect.class); @Pointcut("execution(* cn.braingame.web.controller.LoginController.*(..))") public void myFirstAspect(){ } @Before("myFirstAspect()") public void testBefore(JoinPoint jp){ Object[] obj = jp.getArgs(); for(Object o : obj){ if(o instanceof HttpServletRequest){ System.out.println(((HttpServletRequest)o).getPathInfo()); } } } }
@RestController @RequestMapping(value = "/api/") public class LoginController { static final Logger logger = LoggerFactory.getLogger(LoginController.class); @RequestMapping(value= "login",method = RequestMethod.PUT) @SystemControllerLog(description = "asdasd") public LoginResult needAOPlogin(@RequestBody LoginBean bean,HttpServletRequest request){ LoginResult loginResult = new LoginResult(); logger.info("login 進來了 email {} password {}", bean.getEmail(), bean.getPassword()); if(StringUtils.equals("a@q.com", bean.getEmail()) && StringUtils.equals("111111",bean.getPassword())){ loginResult.setStatus(Constants.Status.success.getValue()); String token = RandomStringUtils.randomAlphabetic(5); loginResult.setToken(token); CacheManager.cache.put("token",bean.getEmail()); }else{ loginResult.setStatus(Constants.Status.error.getValue()); } return loginResult; } }
LoginController 裏的needAOPlogin必需要public,不然攔截無做用!!!