如何在过滤器中查看 spring csrf 令牌
how to view spring csrf token in filter
我正在使用 spring mvc 4.3 版本。我们使用默认的 spring csrf。在其中一种情况下,我收到无效令牌错误,但我看到无论我得到什么,令牌都已发送到服务器。有什么方法可以放置日志消息以查看服务器响应以查看生成的 csrf 令牌。在 UI JSP 页面上我正在使用标签,在页面视图源中我可以看到令牌。但我想在服务器端的过滤器中查看,以确保我的页面上没有其他令牌丢失。
我的页面上有两个选项卡。每当我更改选项卡时,我都会遇到此令牌无效问题。你能帮我如何在我的自定义过滤器中访问此令牌详细信息吗?
我试过这样的 AOP 是否需要更改?
@看点
public class AfterGenerateCsrfTockenAspect {
protected transient ITSLogger logger = LogManager.getLogger(this.getClass().getName());
@AfterReturning(
pointcut = "org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.generateToken(..)",
returning = "result"
)
public void logGeneratedCsrfToken(Object result) {
logger.debug("CSRF token was generated. :::: " + result);
}
}
Is there any way I can put log message to see server response to see the csrf token that is generated
是的,AOP 来拯救。例如,您可以在每次生成令牌时记录令牌:
@Aspect
@Slf4j // lombok for logging
public class AfterGenerateCsrfTockenAspect {
@AfterReturning( // use here FQDN of your Token generator method
pointcut = "org.springframework.security.web.csrf.CookieCsrfTokenRepository.generateToken(..)",
returning = "result"
)
public void logGeneratedCsrfToken(Object result) {
log.info("CSRF token '{}' was generated.", result);
}
}
默认情况下 spring 在会话中存储 csrf 令牌。我们可以从会话属性访问 csrf 令牌
String key ="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN"
HttpSession session = request.getSession(true);
CsrfToken token =(CsrfToken) session.getAttribute(key);
我正在使用 spring mvc 4.3 版本。我们使用默认的 spring csrf。在其中一种情况下,我收到无效令牌错误,但我看到无论我得到什么,令牌都已发送到服务器。有什么方法可以放置日志消息以查看服务器响应以查看生成的 csrf 令牌。在 UI JSP 页面上我正在使用标签,在页面视图源中我可以看到令牌。但我想在服务器端的过滤器中查看,以确保我的页面上没有其他令牌丢失。
我的页面上有两个选项卡。每当我更改选项卡时,我都会遇到此令牌无效问题。你能帮我如何在我的自定义过滤器中访问此令牌详细信息吗?
我试过这样的 AOP 是否需要更改?
@看点 public class AfterGenerateCsrfTockenAspect {
protected transient ITSLogger logger = LogManager.getLogger(this.getClass().getName());
@AfterReturning(
pointcut = "org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.generateToken(..)",
returning = "result"
)
public void logGeneratedCsrfToken(Object result) {
logger.debug("CSRF token was generated. :::: " + result);
}
}
Is there any way I can put log message to see server response to see the csrf token that is generated
是的,AOP 来拯救。例如,您可以在每次生成令牌时记录令牌:
@Aspect
@Slf4j // lombok for logging
public class AfterGenerateCsrfTockenAspect {
@AfterReturning( // use here FQDN of your Token generator method
pointcut = "org.springframework.security.web.csrf.CookieCsrfTokenRepository.generateToken(..)",
returning = "result"
)
public void logGeneratedCsrfToken(Object result) {
log.info("CSRF token '{}' was generated.", result);
}
}
默认情况下 spring 在会话中存储 csrf 令牌。我们可以从会话属性访问 csrf 令牌
String key ="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN"
HttpSession session = request.getSession(true);
CsrfToken token =(CsrfToken) session.getAttribute(key);