Java tutorial
/** * Copyright (c) 2011-2014, hubin (jobob@qq.com). * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.framework.aop; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import com.baomidou.framework.annotations.FormToken; /** * <p> * ???????? * </p> * <p> * ????????? token ?????? * <input type="hidden" name="token" value="${requestScope.token}" /> * </p> * * @author hubin * @Date 2016-04-09 */ @Aspect @Component public class ResubmitAspect { private static final String PARAM_TOKEN = "token"; private static final String PARAM_TOKEN_FLAG = "TokenFlag_"; /** * <p> * ? * </p> * * @param joinPoint * ? * @param formToken * ?? * @throws Throwable */ @Around("@annotation(formToken)") public void execute(ProceedingJoinPoint joinPoint, FormToken formToken) throws Throwable { Object[] args = joinPoint.getArgs(); String className = joinPoint.getTarget().getClass().getName(); for (Object arg : args) { if (arg != null && arg instanceof HttpServletRequest) { HttpServletRequest request = (HttpServletRequest) arg; HttpSession session = request.getSession(true); if (formToken != null) { if ("GET".equalsIgnoreCase(request.getMethod())) { /* GET ? token */ this.generate(joinPoint, request, session, PARAM_TOKEN_FLAG + className); } else { /* POST ? token */ this.validation(joinPoint, request, session, PARAM_TOKEN_FLAG + className); } } } } } /** * <p> * ?? token * </p> */ public void generate(ProceedingJoinPoint joinPoint, HttpServletRequest request, HttpSession session, String tokenFlag) throws Throwable { String uuid = UUID.randomUUID().toString(); session.setAttribute(tokenFlag, uuid); request.setAttribute(PARAM_TOKEN, uuid); joinPoint.proceed(); } /** * <p> * ?? token * </p> * <p> * ????token<br> * ???? * </p> */ public void validation(ProceedingJoinPoint joinPoint, HttpServletRequest request, HttpSession session, String tokenFlag) throws Throwable { Object sessionFlag = session.getAttribute(tokenFlag); Object requestFlag = request.getParameter(PARAM_TOKEN); if (sessionFlag != null && sessionFlag.equals(requestFlag)) { session.removeAttribute(tokenFlag); joinPoint.proceed(); } } }