Liferay根据用户权限登陆后跳转到不同页面

1 篇文章 0 订阅
1 篇文章 0 订阅

场景:Liferay用户登录之后,自动根据不同的角色,跳转到不同的页面。

原理:创建Hook,根据用户信息查找其权限列表,从而达到登陆后跳转到不同的页面目的。

环境:Liferay7.0

步骤:

  1. 使用Liferay的IDE工具,创建Liferay Plugin Project,类型选择hook,创建项目。
  2. 然后在项目下创建Liferay Hook Configuration,hook type选择 Portal propeties;添加Event Action,Event选择login.events.post,superClass选择com.liferay.portal.kernel.events.Action;
  3. 在创建的Hook下实现相应代码
  4. 部署项目。

代码如下:

package com.kyq.liferay;

import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.events.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;

/**
 * 自定义登录跳转action,根据用户拥有的权限来跳转到菜单第一个界面。
 * 注意,按照菜单layoutId来排序的。
 * **/
public class LoginRedirect extends Action {
	private static final Log _log = LogFactoryUtil.getLog(LoginRedirect.class);
	/* (non-Java-doc)
	 * @see com.liferay.portal.kernel.events.Action#Action()
	 */
	public LoginRedirect() {
		super();
	}

	/* (non-Java-doc)
	 * @see com.liferay.portal.kernel.events.Action#run(HttpServletRequest arg0, HttpServletResponse arg1)
	 */
	public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		String useridStr=(String) session.getAttribute("j_username");    //获取用户ID
		String redirect="/web/guest/7";//默认为首页,如果没有会自动跳转到home
		
		//==========获取登录用户有权限的菜单
		String querySql = "select \n" +
                "d.*\n" +
                "from\n" +
                "users_usergroups a\n" +
                "inner join usergroupgrouprole b on a.userGroupId=b.userGroupId\n" +
                "inner join resourcepermission c on b.roleId=c.roleId\n" +
                "inner join layout d on d.plid=c.primKeyId\n" +
                "where a.userId='"+useridStr+"' and type_<>'node' and c.viewActionId=1 \n" +
                "ORDER BY d.layoutId asc limit 1";
		try(java.sql.Connection conn = DataAccess.getConnection();
				java.sql.Statement stmt = conn.createStatement();
				java.sql.ResultSet rt = stmt.executeQuery(querySql)){
			while(rt.next()){
				String friendlyURL = rt.getString("friendlyURL");
				redirect = "/web/guest"+friendlyURL;
			}
		} catch (java.sql.SQLException e) {
			_log.error(e.getSQLState());
			_log.error(e.getMessage());
		}
		//=============设置登录后的跳转路径
		LastPath lastPath=new LastPath(StringPool.BLANK, redirect);
		session.setAttribute(WebKeys.LAST_PATH, lastPath);
	}

}

tips:

1. IDE,Portal,Portal源码,plugin-sdk下载地址:https://sourceforge.net/projects/lportal/files/?source=navbar

2.关于打包:Liferay IDE可以在官方网站下载,其为Eclipse+Liferay插件的合集包。代码完成之后在项目上鼠标右击选择Liferay->SDK->war即可将写好的代码打包。

3.编写Hook需要导入plugin sdk,配置sdk的时候需要选择plugin sdk解压路径,同时在sdk下添加build.${computerUserName}.properties文件(${computerUserName}表示你自己电脑的用户名如:build.kyq1024.properties),在配置文件里面添加如下参数:

app.server.type=tomcat
app.server.dir=D:/workspace/document/liferay/liferay-ce-portal-7.0-ga4/tomcat-8.0.32
app.server.parent.dir=D://workspace//document//liferay//liferay-ce-portal-7.0-ga4
app.server.tomcat.dir=${app.server.parent.dir}/tomcat-8.0.32

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值