Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)

本文主要是介绍Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

第一步:修改liferay-hook.xml

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd"><hook>
<portal-properties>portal.properties</portal-properties>
</hook>

 

如果是liferay7则不需要这一步,只需要注解:

@Component(
immediate = true, property = {"key=auth.pipeline.pre"},
service = Authenticator.class
)

 


第二步:配置认证属性portal.properties

auth.pipeline.pre=com.proliferay.YourAuthenticator


配置auth.pipeline.post 还将进行密码检查,liferay的内部机制是2级检查,一级是身份认证,二级是密码检查,实际上可以通过SKIP_LIFERAY_CHECK来统一处理

 

第三步:开发定制的认证类

import java.util.Map;
import com.liferay.portal.security.auth.AuthException;
import com.liferay.portal.security.auth.Authenticator;public class YourAuthenticator implements Authenticator {@Overridepublic int authenticateByEmailAddress(long companyId, String emailAddress,String password, Map<String, String[]> headerMap,Map<String, String[]> parameterMap) throws AuthException {/*** 这里是认证的逻辑*/return SKIP_LIFERAY_CHECK;}@Overridepublic int authenticateByScreenName(long companyId, String screenName,String password, Map<String, String[]> headerMap,Map<String, String[]> parameterMap) throws AuthException {return DNE;}@Overridepublic int authenticateByUserId(long companyId, long userId,String password, Map<String, String[]> headerMap,Map<String, String[]> parameterMap) throws AuthException {return DNE;}}

 


常数定义:

  • public static final int DNE = 0; //用户不存在
  • public static final int FAILURE = -1;//认证失败
  • public static final int SKIP_LIFERAY_CHECK = 2;
  • public static final int SUCCESS = 1;

要注意SKIP_LIFERAY_CHECK和SUCCESS的区别,通过SKIP_LIFERAY_CHECK来统一处理身份认证,跳过门户的密码检查,如果返回SUCCESS,则必须配合auth.pipeline.post来进行密码检查。
通过图来说明:

 


一个具体的例子:

集成Apache Shiro的认证登陆

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.security.auth.AuthException;
import com.liferay.portal.kernel.security.auth.Authenticator;import java.util.Map;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;@Component(immediate = true, property = {"key=auth.pipeline.pre"},service = Authenticator.class
)
public class ShiroAuthenticatorPre implements Authenticator {@Activatepublic void activate() {Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:userauth.ini"); //shiro配置文件
SecurityUtils.setSecurityManager(factory.getInstance());if (_log.isInfoEnabled()) {_log.info("activate");}}@Overridepublic int authenticateByEmailAddress(long companyId, String emailAddress, String password,Map<String, String[]> headerMap, Map<String, String[]> parameterMap)throws AuthException {if (_log.isInfoEnabled()) {_log.info("authenticateByEmailAddress");}UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(emailAddress, password);Subject currentUser = SecurityUtils.getSubject();try {//shiro的代理登陆
            currentUser.login(usernamePasswordToken);boolean authenticated = currentUser.isAuthenticated();if (authenticated) {if (_log.isInfoEnabled()) {_log.info("authenticated");}return SKIP_LIFERAY_CHECK; //认证通过
            }else {return FAILURE;}}catch (AuthenticationException ae) {_log.error(ae.getMessage(), ae);throw new AuthException(ae.getMessage(), ae);}}@Overridepublic int authenticateByScreenName(long companyId, String screenName, String password,Map<String, String[]> headerMap, Map<String, String[]> parameterMap)throws AuthException {if (_log.isInfoEnabled()) {_log.info("authenticateByScreenName  - not implemented ");}return SUCCESS;}@Overridepublic int authenticateByUserId(long companyId, long userId, String password,Map<String, String[]> headerMap, Map<String, String[]> parameterMap)throws AuthException {if (_log.isInfoEnabled()) {_log.info("authenticateByScreenName  - not implemented ");}return SUCCESS;}private static final Log _log = LogFactoryUtil.getLog(ShiroAuthenticatorPre.class);}

 

 apache shiro框架结构

apache shiro是一套非常著名的安全框架,提供了认证、授权、加密和会话管理功能
了解更多apache shiro的知识:http://www.infoq.com/cn/articles/apache-shiro

Authentication的Token令牌机制
了解更多Authentication Token:https://web.liferay.com/zh/community/wiki/-/wiki/Main/Authentication+Token

Token令牌是为了避免CSRF跨站伪造。
了解更多CSRF:https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)

 

定义认证失败的扩展处理

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.security.auth.AuthException;
import com.liferay.portal.kernel.security.auth.AuthFailure;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import java.util.Map;
import org.osgi.service.component.annotations.Component;@Component(immediate = true, property = {"key=auth.failure"},service = AuthFailure.class
)
public class LogAuthFailure implements AuthFailure {@Overridepublic void onFailureByEmailAddress(long companyId, String emailAddress,Map<String, String[]> headerMap, Map<String, String[]> parameterMap)throws AuthException {try {User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);int failures = user.getFailedLoginAttempts();if (_log.isInfoEnabled()) {_log.info("onFailureByEmailAddress: " + emailAddress +" has failed to login " + failures + " times");}}catch (PortalException pe) {}}@Overridepublic void onFailureByScreenName(long companyId, String screenName, Map<String, String[]> headerMap,Map<String, String[]> parameterMap)throws AuthException {try {User user = UserLocalServiceUtil.getUserByScreenName(companyId, screenName);int failures = user.getFailedLoginAttempts();if (_log.isInfoEnabled()) {_log.info("onFailureByScreenName: " + screenName +" has failed to login " + failures + " times");}}catch (PortalException pe) {}}@Overridepublic void onFailureByUserId(long companyId, long userId, Map<String, String[]> headerMap,Map<String, String[]> parameterMap)throws AuthException {try {User user = UserLocalServiceUtil.getUserById(userId);int failures = user.getFailedLoginAttempts();if (_log.isInfoEnabled()) {_log.info("onFailureByUserId: userId " + userId +" has failed to login " + failures + " times");}}catch (PortalException pe) {}}private static final Log _log = LogFactoryUtil.getLog(LogAuthFailure.class);}

 优秀的平台必然松耦合、易扩展。

这篇关于Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/666034

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部