freemarker使用shiro標簽(spring boot)
首先需要寫一個類
-
/**
-
* 集成Shiro標簽
-
*/
-
-
public class ShiroTagFreeMarkerConfigurer implements InitializingBean {
-
-
-
private Configuration configuration;
-
-
-
private FreeMarkerViewResolver resolver;
-
-
-
public void afterPropertiesSet() throws Exception {
-
// 加上這句后,可以在頁面上使用shiro標簽
-
configuration.setSharedVariable( "shiro", new ShiroTags());
-
// 加上這句后,可以在頁面上用${context.contextPath}獲取contextPath
-
resolver.setRequestContextAttribute( "context");
-
}
-
}
然后在doGetAuthorizationInfo方法中獲取我們想要驗證的權限,將權限寫入roleNames和PermissionNames中
-
-
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
-
logger.info( "執行Shiro權限認證");
-
try {
-
UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
-
if (user != null) {
-
// 權限信息對象info,用來存放查出的用戶的所有的角色(role)及權限(permission)
-
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
-
-
// 根據用戶名id查詢xxx權限
-
List<XXX> xxxList = xxxWSService.findxxxbyUserInfoId(user.getId());
-
-
Set<String> roleNames = new HashSet<String>();
-
Set<String> permissionNames = new HashSet<String>();
-
for (XXX xxx: xxxList) {
-
permissionNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString());
-
roleNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString() + "_" + xxx.getXxxName().toString());
-
}
-
-
// 將權限提供給info
-
info.setStringPermissions(permissionNames);
-
// 將角色名稱提供給info
-
info.setRoles(roleNames);
-
-
info.addStringPermission( "");
-
return info;
-
}
-
-
} catch (Exception e) {
-
logger.error( "執行Shiro權限認證異常!", e.getLocalizedMessage() );
-
e.printStackTrace();
-
return null;
-
}
-
// 返回null的話,就會導致任何用戶訪問被攔截的請求時,都會自動跳轉到unauthorizedUrl指定的地址
-
return null;
-
}
最后就可以在前端freemarker模板中使用shiro標簽,有xxx角色的人員才可以看到"保存"按鈕;
-
<@shiro.hasAnyRoles name="app_${xxx.id?c}_1,app_${xxx.id?c}_2">
-
<button type="submit"class="btn green" style="padding:6px 22px">保 存</button>
-
</@shiro.hasAnyRoles>
Shiro包含的標簽:
guest標簽:驗證當前用戶是否為“訪客”,即未認證(包含未記住)的用戶;shiro標簽:<shiro:guest></shiro:guest> ;freemark中: <@shiro.guest> </@shiro.guest>
user標簽:認證通過或已記住的用戶 shiro標簽:<shiro:user> </shiro:user> ;freemark中: <@shiro.user> </@shiro.user>
authenticated標簽:已認證通過的用戶。不包含已記住的用戶,這是與user標簽的區別所在。 shiro標簽:<shiro:authenticated> </shiro:authenticated>;freemark中: <@shiro.authenticated></@shiro.authenticated>
notAuthenticated標簽:未認證通過的用戶。與authenticated標簽相對。 shiro標簽:<shiro:notAuthenticated> </shiro:notAuthenticated>;freemark中: <@shiro.notAuthenticated></@shiro.notAuthenticated>
principal標簽:輸出當前用戶信息,通常為登錄帳號信息 shiro標簽:Hello, <@shiro.principal property="name" /> ;freemarker中: Hello, <@shiro.principal property="name" />, how are you today?
hasRole標簽:驗證當前用戶是否屬於該角色 ,shiro標簽: <shiro:hasRole name="administrator"> Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name=”admin”>Hello admin!</@shiro.hasRole>
hasAnyRoles標簽:驗證當前用戶是否屬於這些角色中的任何一個,角色之間逗號分隔 ,shiro標簽: <shiro:hasAnyRoles name="admin,user,operator"> Administer the system </shiro:hasAnyRoles> ;freemarker中:<@shiro.hasAnyRoles name="admin,user,operator">Hello admin!</@shiro.hasAnyRoles>
hasPermission標簽:驗證當前用戶是否擁有該權限 ,shiro標簽: <shiro:hasPermission name="/order:*"> 訂單 </shiro:hasPermission> ;freemarker中:<@shiro.hasPermission name="/order:*">訂單/@shiro.hasPermission>
lacksRole標簽:驗證當前用戶不屬於該角色,與hasRole標簽想反,shiro標簽: <shiro:hasRole name="admin"> Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name="admin">Hello admin!</@shiro.hasRole>
lacksPermission標簽:驗證當前用戶不擁有某種權限,與hasPermission標簽是相對的,shiro標簽: <shiro:lacksPermission name="/order:*"> trade </shiro:lacksPermission> ;freemarker中:<@shiro.lacksPermission name="/order:*">trade</@shiro.lacksPermission>
原文 轉自: https://blog.csdn.net/sayoko06/article/details/80897658