gpt4 book ai didi

springboot 自定义权限标签(tld),在freemarker引用操作

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章springboot 自定义权限标签(tld),在freemarker引用操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

第一步:引入jar包 。

?
1
2
3
4
5
< dependency >
   < groupId >javax.servlet.jsp</ groupId >
   < artifactId >jsp-api</ artifactId >
   < version >2.2.1-b03</ version >
</ dependency >

第二步:自定义标签类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
 
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.web.servlet.tags.RequestContextAwareTag;
 
import com.goodidea.sso.dto.PrivilegesDto;
import com.goodidea.sso.dto.ResourcesDto;
import com.goodidea.sso.service.PrivilegesService;
 
/**
  *
* @ClassName: PrivilegeTag
* @Description: 权限标签类
* @author lsg
* @date 2017年9月12日 下午5:36:01
*
  */
public class PrivilegeTag extends RequestContextAwareTag{
 
 
   private static final long serialVersionUID = 534416848523276042L;
 
   private String menuAlias;
 
   private String priAlias;
 
   public String getMenuAlias() {
     return menuAlias;
   }
 
   public void setMenuAlias(String menuAlias) {
     this .menuAlias = menuAlias;
   }
 
 
   public String getPriAlias() {
     return priAlias;
   }
 
   public void setPriAlias(String priAlias) {
     this .priAlias = priAlias;
   }
 
   @Override
   protected int doStartTagInternal() {
     // TODO Auto-generated method stub
     boolean result = false ;
      try {
       HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
        Map<String, Object> attributes = principal.getAttributes();
       String username=(String) attributes.get( "username" );
       PrivilegesService privilegesService= (PrivilegesService) this .getRequestContext().getWebApplicationContext().getBean( "privilegesServiceImpl" );
       Set<ResourcesDto> dto = privilegesService.findResourcesByUsername(username);
       for (ResourcesDto resourcesDto : dto) {
         if ( this .menuAlias.equals(resourcesDto.getAlias())){
           for (PrivilegesDto pdto : resourcesDto.getPrivileges()) {
             if ( this .priAlias.equals(pdto.getAlias())){
                result = true ;
             }
           }
         }
       }
      } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       result = false ;
     }
     return result? EVAL_BODY_INCLUDE : SKIP_BODY;
   }
}

第三步:创建tld标签,放入在web-inf下 。

springboot 自定义权限标签(tld),在freemarker引用操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
< taglib >
   < tlibversion >1.0</ tlibversion >
   < jspversion >1.1</ jspversion >
   < shortname >privilege</ shortname >
   < tag >
     < name >privilege</ name >
     < tagclass >com.goodidea.sso.core.PrivilegeTag</ tagclass >
     < bodycontent >empty</ bodycontent > <!--这里如果设为empty,则无body-->
     < attribute >
       < name >menuAlias</ name >
       < required >true</ required >
       < rtexprvalue >true</ rtexprvalue >
     </ attribute >
     < attribute >
       < name >priAlias</ name >
       < required >true</ required >
       < rtexprvalue >true</ rtexprvalue >
     </ attribute >
   </ tag >
</ taglib >

第四:页面引用 。

<#assign p=JspTaglibs["/WEB-INF/tld/privilege.tld"] /> 。

注意tld,如果不在web.xml上进行引入的话,就放在web-inf下,要不然会报找不到tld资源异常 。

补充知识:Springboot项目 freemarker 引入shiro 标签 。

springboot集成shiro权限过程略过 。

1、添加maven 依赖 。

?
1
2
3
4
5
< dependency >
  < groupId >net.mingsoft</ groupId >
  < artifactId >shiro-freemarker-tags</ artifactId >
  < version >0.1</ version >
</ dependency >

2、注入FreeMarkerConfigurer,未指定templateLoaderPath时遇到过跳转到页面404问题 。

?
1
2
3
4
5
6
7
8
9
10
11
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException {
   FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
   freeMarkerConfigurer.setTemplateLoaderPath( "classpath:templates/" );
   freemarker.template.Configuration configuration = freeMarkerConfigurer.createConfiguration();
   configuration.setDefaultEncoding( "UTF-8" );
   //这里可以添加其他共享变量 比如sso登录地址
   configuration.setSharedVariable( "shiro" , new ShiroTags());
   freeMarkerConfigurer.setConfiguration(configuration);
   return freeMarkerConfigurer;
}

3、shiro标签 。

1、session中读取登录人信息 。

<@shiro.principal/> 。

2、带有loginName属性的对象转换为Prinipal后保存在session 。

<shiro:principal property="loginName" /> 。

3、带有loginName属性的Json(个人使用的是FashJson对象)转换为Prinipal后保存在session,使用freemarker标签处理Json 。

<#assign loginInfo><@shiro.principal/></#assign> 。

<#assign data=loginInfo?eval> 。

用户:${data.loginName!""} 。

4、其他shiro标签使用 。

<@shiro.hasPermission name="权限编码"> ... </@shiro.hasPermission> 。

以上这篇springboot 自定义权限标签(tld),在freemarker引用操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://blog.csdn.net/cutterwolf/article/details/78085808 。

最后此篇关于springboot 自定义权限标签(tld),在freemarker引用操作的文章就讲到这里了,如果你想了解更多关于springboot 自定义权限标签(tld),在freemarker引用操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com