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

网友投稿 261 2023-03-13

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

第一步:引入jar包

javax.servlet.jsp

jsp-api

2.2.1-b03

第二步:自定义标签类

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 attributes = principal.getAttributes();

String username=(String) attributes.get("username");

PrivilegesService privilegesService= (PrivilegesService)this.getRequestContext().getWebApplicationContext().getBean("privilegesServiceImpl");

Set 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下

1.0

1.1

privilege

privilege

com.goodidea.sso.core.PrivilegeTag

empty

menuAlias

true

true

priAlias

true

true

第四:页面引用

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

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

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

springboot集成shiro权限过程略过

一、添加maven 依赖

net.mingsoft

shiro-freemarker-tags

0.1

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

@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;

}

三、shiro标签

1、session中读取登录人信息

<@shiro.principal/>

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

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

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

<#assign data=loginInfo?http://eval>

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

4、其他shiro标签使用

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

...

@shiro.hasPermission>

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:数据平台架构图(数据平台架构图怎么做)
下一篇:java实现飞机游戏代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~