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

原文地址:https://www.cnblogs.com/honey01/p/9782791.html