freemarker使用shiro標簽(spring boot)


freemarker使用shiro標簽(spring boot)

首先需要寫一個類

  1.  
    /**
  2.  
    * 集成Shiro標簽
  3.  
    */
  4.  
    @Component
  5.  
    public class ShiroTagFreeMarkerConfigurer implements InitializingBean {
  6.  
     
  7.  
    @Autowired
  8.  
    private Configuration configuration;
  9.  
     
  10.  
    @Autowired
  11.  
    private FreeMarkerViewResolver resolver;
  12.  
     
  13.  
    @Override
  14.  
    public void afterPropertiesSet() throws Exception {
  15.  
    // 加上這句后,可以在頁面上使用shiro標簽
  16.  
    configuration.setSharedVariable( "shiro", new ShiroTags());
  17.  
    // 加上這句后,可以在頁面上用${context.contextPath}獲取contextPath
  18.  
    resolver.setRequestContextAttribute( "context");
  19.  
    }
  20.  
    }

然后在doGetAuthorizationInfo方法中獲取我們想要驗證的權限,將權限寫入roleNames和PermissionNames中

  1.  
    @Override
  2.  
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
  3.  
    logger.info( "執行Shiro權限認證");
  4.  
    try {
  5.  
    UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
  6.  
    if (user != null) {
  7.  
    // 權限信息對象info,用來存放查出的用戶的所有的角色(role)及權限(permission)
  8.  
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  9.  
     
  10.  
    // 根據用戶名id查詢xxx權限
  11.  
    List<XXX> xxxList = xxxWSService.findxxxbyUserInfoId(user.getId());
  12.  
     
  13.  
    Set<String> roleNames = new HashSet<String>();
  14.  
    Set<String> permissionNames = new HashSet<String>();
  15.  
    for (XXX xxx: xxxList) {
  16.  
    permissionNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString());
  17.  
    roleNames.add(Constants.SHIRO_AUTH_XXX + "_" + xxx.getxxxId().toString() + "_" + xxx.getXxxName().toString());
  18.  
    }
  19.  
     
  20.  
    // 將權限提供給info
  21.  
            info.setStringPermissions(permissionNames);
  22.  
    // 將角色名稱提供給info
  23.  
            info.setRoles(roleNames);
  24.  
     
  25.  
    info.addStringPermission( "");
  26.  
    return info;
  27.  
    }
  28.  
     
  29.  
    } catch (Exception e) {
  30.  
    logger.error( "執行Shiro權限認證異常!", e.getLocalizedMessage() );
  31.  
    e.printStackTrace();
  32.  
    return null;
  33.  
    }
  34.  
    // 返回null的話,就會導致任何用戶訪問被攔截的請求時,都會自動跳轉到unauthorizedUrl指定的地址
  35.  
    return null;
  36.  
    }

最后就可以在前端freemarker模板中使用shiro標簽,有xxx角色的人員才可以看到"保存"按鈕;

  1.  
    <@shiro.hasAnyRoles name="app_${xxx.id?c}_1,app_${xxx.id?c}_2">
  2.  
    <button type="submit"class="btn green" style="padding:6px 22px">保 存</button>
  3.  
    </@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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM