在Freemarker模板中检查Spring安全角色和已记录的用户名 [英] Checking Spring security roles and logged username in Freemarker template

查看:79
本文介绍了在Freemarker模板中检查Spring安全角色和已记录的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道freemarker标签来检查freemarker文件中的spring安全角色和用户名?

Does anyone know the freemarker tags to check spring security roles and username in freemarker file?

我从网上的几个资源中发现以下代码会打印登录的用户名。但它不是打印用户名,而是打印只是登录为

I found from couple of resources on the web that the following code would print the logged in username. But it is not printing the username, instead it is printing just "logged in as"

<security:authorize access="isAuthenticated()">
    logged in as <security:authentication property="principal.username" /> 
</security:authorize>

同样检查Freemarker文件中的角色也无效。有没有人以前做过?

Also checking the roles in Freemarker file is also not working. Has anyone done it before?

推荐答案

以下应该有效:

步骤1:包含Spring安全性freemarker文件顶部的标签库

< #assign security = JspTaglibs [http://www.springframework.org/security/tags] />

The following should work:
step 1: Include Spring security tag library on top of freemarker file
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />

第2步:检查角色名称

<@security.authorize ifAnyGranted="ROLE_USER">
    Your role is "ROLE_USER" <br/>
</@security.authorize>

步骤3:检查登录用户的登录名

Step 3: To check logged in user's loginname

<@security.authorize access="isAuthenticated()">
    logged in as <@security.authentication property="principal.username" /> 
</@security.authorize>

<@security.authorize access="! isAuthenticated()">
    Not logged in
</@security.authorize>

希望这会有所帮助。

这篇关于在Freemarker模板中检查Spring安全角色和已记录的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆