close
SpringBoot+SpringSecurity+Freemarker專案中在頁面上使用security標籤控制按鈕顯示隱藏達到對按鈕級許可權控制還是比較方便的,如下配置即可。
1. gradle 引入依賴
dependencies {
compile("org.springframework.security:spring-security-taglibs")
compile("javax.servlet.jsp:javax.servlet.jsp-api:2.3.3")
}
2. 依賴引入後到spring-security-taglibs包中META-INF下security.tld複製出來,放到/resources/static下,最後建一個目錄tags,如下:
3. 建一個配置類
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Autowired
FreeMarkerConfigurer freeMarkerConfigurer;
/**
*
* 加入 spring-security-taglibs 對 FreeMarker 的支援
*/
@PostConstruct
public void freeMarkerConfigurer() {
List tlds = new ArrayList();
tlds.add("/static/tags/security.tld");
TaglibFactory taglibFactory = freeMarkerConfigurer.getTaglibFactory();
taglibFactory.setClasspathTlds(tlds);
if(taglibFactory.getObjectWrapper() == null) {
taglibFactory.setObjectWrapper(freeMarkerConfigurer.getConfiguration().getObjectWrapper());
}
}
}
4. 在freemarker頁面頂部引入標籤
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
5. 標籤應用如下
<@security.authorize access="hasRole('ADMIN')">
Admin can read
</@security.authorize>
<@security.authorize access="hasRole('USER')">
User can read
</@security.authorize>
文章標籤
全站熱搜
留言列表