使用AOP的JavaSpringMVC-CSRF令牌
我正在尝试为使用SpringMVC的站点设置跨站点伪造保护。我的想法是在HTML请求头中发送一个令牌,并使用AOP进行验证,如下所示:使用AOP的JavaSpringMVC-CSRF令牌,java,spring,spring-mvc,aop,Java,Spring,Spring Mvc,Aop,我正在尝试为使用SpringMVC的站点设置跨站点伪造保护。我的想法是在HTML请求头中发送一个令牌,并使用AOP进行验证,如下所示: @Aspect @Component public class RequestMappingInterceptor { @Before("execution(@org.springframework.web.bind.annotation.RequestMapping * *(..)) && args(request,..)")
@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");
}
}
}
然而,这不起作用,我不知道为什么。这难道不应该拦截任何用
@RequestMapping
表示的包含请求参数的方法吗?任何帮助都将不胜感激可能相关:为什么不使用Spring Security?它已经内置了CSRF保护?-请参阅并可能与之相关:为什么不使用Spring安全性?它已经内置了CSRF保护?-看到和