Sen kullanarak, @ControllerAdvice içinden oturumu alabilirsiniz:
Seçenek 1:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = requeset.getSession(true);//true will create if necessary
Seçenek 2:
@Autowired(required=true)
private HttpServletRequest request;
Seçenek 3:
@Context
private HttpServletRequest request;
İşte bir sınav var Tüm denetleyici son nokta yöntemlerini kesiştiren bir Denetleyici yönünü nasıl devrettiğimden emin olun:
@Component
@Aspect
class ControllerAdvice{
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
void hasRequestMappingAnnotation() {}
@Pointcut("execution(* your.base.package..*Controller.*(..))")
void isMethodExecution() {}
/**
* Advice to be executed if this is a method being executed in a Controller class within our package structure
* that has the @RequestMapping annotation.
* @param joinPoint
* @throws Throwable
*/
@Before("hasRequestMappingAnnotation() && isMethodExecution()")
void beforeRequestMappedMethodExecution(JoinPoint joinPoint) {
String method = joinPoint.getSignature().toShortString();
System.out.println("Intercepted: " + method);
//Now do whatever you need to
}
}
Neden bunun için Interceptors kullanmayın? – JSONStatham
@JSONStatham bu mükemmel bir fikir! –