I am trying to set up Cross Site Forging protection for my site that uses Spring MVC. My idea was to send a token in the HTML request header and verify it using AOP like this:
@Aspect
@Component
public class RequestMappingInterceptor {
@Before("execution(@org.springframework.web.bind.annotation.RequestMapping * *(..)) && args(request,..)")
public void before(JoinPoint point, HttpServletRequest request) throws Throwable {
UserEntity loggedUser = ((AmsUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserEntity();
String encodedToken = Base64.encodeBase64String(SessionEncodingUtils.encryptDecryptString(loggedUser.getId() + ";" + request.getSession(true).getId().hashCode()).getBytes());
if (!encodedToken.equals(request.getHeader("csrfToken"))) {
throw new RuntimeException("go.away");
}
}
}
However this does not work and i am not sure why. Shouldnt this intercept any method adnotated with @RequestMapping
which contain a request argument? Any help would be appreciated