You can get the session from within your @ControllerAdvice, using:
Option 1:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = requeset.getSession(true);//true will create if necessary
Option 2:
@Autowired(required=true)
private HttpServletRequest request;
Option 3:
@Context
private HttpServletRequest request;
Here is an example of how I have devined a Controller aspect that intercepts all controller endpoint methods:
@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
}
}