반응형
gradle handlebars 설정
implementation("com.github.jknack:handlebars:4.4.0")
implementation group: 'pl.allegro.tech.boot', name:'handlebars-spring-boot-starter', version:'0.5.0'
CsrfFilter에서 request 에 _csrf 이름으로 csrfToken값을 설정한다.
CsrfTokenRequestAttributeHandler
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
Supplier<CsrfToken> deferredCsrfToken) {
Assert.notNull(request, "request cannot be null");
Assert.notNull(response, "response cannot be null");
Assert.notNull(deferredCsrfToken, "deferredCsrfToken cannot be null");
request.setAttribute(HttpServletResponse.class.getName(), response);
CsrfToken csrfToken = new SupplierCsrfToken(deferredCsrfToken);
request.setAttribute(CsrfToken.class.getName(), csrfToken);
String csrfAttrName = (this.csrfRequestAttributeName != null) ? this.csrfRequestAttributeName
: csrfToken.getParameterName();
request.setAttribute(csrfAttrName, csrfToken);
}
하지만 handlebars autoconfiguration사용시 request 항목에 대해서 template에서 출력할 수 없다.
기본적으로 View 렌더시에 requestAttribute의 값에 대해서 보안을 위해 merge되지 않도록 되어 있다.
(exposeRequestAttribute 기본값이 false이다.)
AbstractTemplateView
private boolean exposeRequestAttributes = false;
private boolean allowRequestOverride = false;
private boolean exposeSessionAttributes = false;
private boolean allowSessionOverride = false;
private boolean exposeSpringMacroHelpers = true;
@Override
protected final void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (this.exposeRequestAttributes) {
Map<String, Object> exposed = null;
for (Enumeration<String> en = request.getAttributeNames(); en.hasMoreElements();) {
String attribute = en.nextElement();
if (model.containsKey(attribute) && !this.allowRequestOverride) {
throw new ServletException("Cannot expose request attribute '" + attribute +
"' because of an existing model object of the same name");
}
Object attributeValue = request.getAttribute(attribute);
if (logger.isDebugEnabled()) {
exposed = (exposed != null ? exposed : new LinkedHashMap<>());
exposed.put(attribute, attributeValue);
}
model.put(attribute, attributeValue);
}
if (logger.isTraceEnabled() && exposed != null) {
logger.trace("Exposed request attributes to model: " + exposed);
}
}
...
...
}
출력을 위해서는 exposeRequestAttribute항목을 true로 설정해야 한다.
application.properties
application.properties
#handlebars 설정
handlebars.enabled=true
handlebars.suffix=.hbs
handlebars.cache=false
#abstractTemplateView.exposeRequestAttributes 값을 true로 설정
handlebars.exposeRequestAttributes=true
exposeRequestAttributes 속성은 AbstractTemplateViewResolverProperties에 존재하고 있다.
HandlebarsProperties 는 AbstractTemplateViewResolverProperties를 상속받고 있기 때문에
handlebars.exposeRequestAttributes 항목에 true/false를 설정하면 된다.
반응형
'java' 카테고리의 다른 글
spring boot 구동시 로깅 레벨 변경 (0) | 2024.09.25 |
---|---|
intellij console log 에서 jsp 컴파일 에러 확인 (0) | 2024.07.22 |
tomcat9 jmx jconsole 연결 옵션 (0) | 2024.05.08 |
springboot jar파일 외부에서 properties 적용 (0) | 2024.04.15 |
jpa에서 mysql8 예약어로 된 컬럼 사용시 옵션 설정 (0) | 2024.02.05 |