I want to use JSP tags such as <security: authorize>
on FreeMarker. I would like to confirm for myself that it was introduced at https://vorba.ch/2018/spring-boot-freemarker-security-jsp-taglib.html.
It seems that FreeMarker originally has a function to use JSP tags. → Reference URL
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
</dependency>
</dependencies>
The point is that you need the Spring Security JSP tag (spring-security-taglibs
) and JSP API (tomcat-jsp-api
).
SecurityConfig.java
@Configuration
public class MyFreeMarkerConfig {
private final FreeMarkerConfigurer freeMarkerConfigurer;
public MyFreeMarkerConfig(FreeMarkerConfigurer freeMarkerConfigurer) {
this.freeMarkerConfigurer = freeMarkerConfigurer;
}
@PostConstruct
public void init() {
freeMarkerConfigurer.getTaglibFactory().setClasspathTlds(
Collections.singletonList("/META-INF/security.tld"));
}
}
The point is to specify the path of the TLD file.
index.ftlh
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"]/>
<html>
<head>
<title>Index</title>
<meta charset="UTF-8">
</head>
<body>
<h1>Index</h1>
<@security.authorize access="isAuthenticated()">
<p>Hello, <@security.authentication property="principal.username"/>!</p>
</@security.authorize>
<p><a href="/secret">Go to secret page</a></p>
<form action="/logout" method="post">
<input type="submit" value="Logout">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
Specify the JSP tags with <# assign security = JspTaglibs [" http://www.springframework.org/security/tags "] />
.
When using tags, <@ security. Tag name Property name =" Property value ">
.
https://github.com/MasatoshiTada/freemarker-spring-security-sample
Recommended Posts