`
Tyler_Zhou
  • 浏览: 216996 次
  • 性别: Icon_minigender_1
  • 来自: 湖北->上海
社区版块
存档分类
最新评论

liferay验证码认证

阅读更多
生成验证码相关:
/**
 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.liferay.portal.captcha;

import com.liferay.portal.struts.ActionConstants;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PropsFiles;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.ActionResponseImpl;
import com.liferay.util.ExtPropertiesLoader;

import java.util.Properties;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;

import javax.servlet.http.HttpServletResponse;

import nl.captcha.servlet.CaptchaProducer;
import nl.captcha.util.Helper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * <a href="CaptchaPortletAction.java.html"><b><i>View Source</i></b></a>
 *
 * @author Brian Wing Shun Chan
 *
 */
public class CaptchaPortletAction extends PortletAction {

	public CaptchaPortletAction() {
		Properties props =  ExtPropertiesLoader.getInstance(
			PropsFiles.CAPTCHA).getProperties();

		_producer = (CaptchaProducer)Helper.ThingFactory.loadImpl(
			Helper.ThingFactory.CPROD, props);
	}

	public void processAction(
			ActionMapping mapping, ActionForm form, PortletConfig config,
			ActionRequest req, ActionResponse res)
		throws Exception {

		try {
			PortletSession ses = req.getPortletSession();

			String captchaText = _producer.createText();

			ses.setAttribute(WebKeys.CAPTCHA_TEXT, captchaText);

			HttpServletResponse httpRes =
				((ActionResponseImpl)res).getHttpServletResponse();

			_producer.createImage(httpRes.getOutputStream(), captchaText);

			setForward(req, ActionConstants.COMMON_NULL);
		}
		catch (Exception e) {
			_log.error(e);
		}
	}

	protected boolean isCheckMethodOnProcessAction() {
		return _CHECK_METHOD_ON_PROCESS_ACTION;
	}

	private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;

	private static Log _log = LogFactory.getLog(CaptchaPortletAction.class);

	private CaptchaProducer _producer;

}

验证码配置相关:
include-and-override=captcha-ext.properties

cap.border=yes
cap.border.c=black
cap.border.th=1

cap.image.height=50
cap.image.width=150

#cap.text.producer=com.liferay.portal.captcha.DictionaryWordTextProducer
cap.text.producer=com.liferay.portal.captcha.PinNumberTextProducer

读取配置文件相关:
/**
 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.liferay.util;

import com.germinus.easyconf.ComponentConfiguration;
import com.germinus.easyconf.ComponentProperties;
import com.germinus.easyconf.EasyConf;

import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Company;
import com.liferay.portal.service.CompanyLocalServiceUtil;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <a href="ExtPropertiesLoader.java.html"><b><i>View Source</i></b></a>
 *
 * @author Brian Wing Shun Chan
 *
 */
public class ExtPropertiesLoader {

	public static ExtPropertiesLoader getInstance(String name) {
		ExtPropertiesLoader props = _propsPool.get(name);

		if (props == null) {
			props = new ExtPropertiesLoader(name);

			_propsPool.put(name, props);
		}

		return props;
	}

	public static ExtPropertiesLoader getInstance(String name, long companyId) {
		String key = name + _COMPANY_ID_SEPARATOR + companyId;

		ExtPropertiesLoader props = _propsPool.get(key);

		if (props == null) {
			props = new ExtPropertiesLoader(name, companyId);

			_propsPool.put(key, props);
		}

		return props;
	}

	public boolean containsKey(String key) {
		return getComponentProperties().containsKey(key);
	}

	public String get(String key) {
		if (_PRINT_DUPLICATE_KEYS) {
			if (_keys.contains(key)) {
				System.out.println("Duplicate key " + key);
			}
			else {
				_keys.add(key);
			}
		}

		return getComponentProperties().getString(key);
	}

	public void set(String key, String value) {
		getComponentProperties().setProperty(key, value);
	}

	public String[] getArray(String key) {
		String[] array = getComponentProperties().getStringArray(key);

		if (array == null) {
			return new String[0];
		}
		else if (array.length > 0) {

			// Commons Configuration parses an empty property into a String
			// array with one String containing one space. It also leaves a
			// trailing array member if you set a property in more than one
			// line.

			if (Validator.isNull(array[array.length - 1])) {
				String[] subArray = new String[array.length - 1];

				System.arraycopy(array, 0, subArray, 0, subArray.length);

				array = subArray;
			}
		}

		return array;
	}

	public Properties getProperties() {

		// For some strange reason, componentProperties.getProperties() returns
		// values with spaces after commas. So a property setting of "xyz=1,2,3"
		// actually returns "xyz=1, 2, 3". This can break applications that
		// don't expect that extra space. However, getting the property value
		// directly through componentProperties returns the correct value. This
		// method fixes the weird behavior by returing properties with the
		// correct values.

		Properties props = new Properties();

		ComponentProperties componentProps = getComponentProperties();

		Iterator<Map.Entry<Object, Object>> itr =
			componentProps.getProperties().entrySet().iterator();

		while (itr.hasNext()) {
			Map.Entry<Object, Object> entry = itr.next();

			String key = (String)entry.getKey();
			String value = (String)entry.getValue();

			props.setProperty(key, value);
		}

		return props;
	}

	public ComponentProperties getComponentProperties() {
		return _conf.getProperties();
	}

	private ExtPropertiesLoader(String name) {
		_conf = EasyConf.getConfiguration(name);

		_printSources(name);
	}

	private ExtPropertiesLoader(String name, long companyId) {
		String webId = null;

		if (companyId > 0) {
			try {
				Company company = CompanyLocalServiceUtil.getCompanyById(
					companyId);

				webId = company.getWebId();
			}
			catch (Exception e) {
			}
		}

		_conf = EasyConf.getConfiguration(webId, name);

		_printSources(name, companyId, webId);
	}

	private void _printSources(String name) {
		_printSources(name, 0, null);
	}

	private void _printSources(String name, long companyId, String webId) {
		List<String> sources = getComponentProperties().getLoadedSources();

		for (int i = sources.size() - 1; i >= 0; i--) {
			String source = sources.get(i);

			String info = "Loading " + source;

			if (companyId > 0) {
				info +=
					" for {companyId=" + companyId + ", webId=" + webId + "}";
			}

			System.out.println(info);
		}
	}

	private static Map<String, ExtPropertiesLoader> _propsPool =
		new ConcurrentHashMap<String, ExtPropertiesLoader>();

	private static final String _COMPANY_ID_SEPARATOR = "_COMPANY_ID_";

	private static final boolean _PRINT_DUPLICATE_KEYS = false;

	private ComponentConfiguration _conf;
	private Set<String> _keys = new HashSet<String>();

}

check验证码相关:
/**
 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.liferay.portal.captcha;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PropsValues;
import com.liferay.portal.util.WebKeys;

import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <a href="CaptchaUtil.java.html"><b><i>View Source</i></b></a>
 *
 * @author Brian Wing Shun Chan
 *
 */
public class CaptchaUtil {

	public static void check(HttpServletRequest req)
		throws CaptchaTextException {

		if (isEnabled(req)) {
			HttpSession ses = req.getSession();

			String captchaText = (String)ses.getAttribute(WebKeys.CAPTCHA_TEXT);

			if (captchaText != null) {
				if (!captchaText.equals(
						ParamUtil.getString(req, "captchaText"))) {

					throw new CaptchaTextException();
				}
				else {
					if (_log.isDebugEnabled()) {
						_log.debug("Captcha text is valid");
					}

					ses.removeAttribute(WebKeys.CAPTCHA_TEXT);

					if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
						(Validator.isNotNull(req.getRemoteUser()))) {

						Integer count = (Integer)ses.getAttribute(
							WebKeys.CAPTCHA_COUNT);

						if (count == null) {
							count = new Integer(1);
						}
						else {
							count = new Integer(count.intValue() + 1);
						}

						ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
					}
				}
			}
			else {
				if (_log.isErrorEnabled()) {
					_log.error(
						"Captcha text is null. User " + req.getRemoteUser() +
							" may be trying to circumvent the captcha.");
				}

				throw new CaptchaTextException();
			}
		}
	}

	public static void check(PortletRequest req) throws CaptchaTextException {
		if (isEnabled(req)) {
			PortletSession ses = req.getPortletSession();

			String captchaText = (String)ses.getAttribute(WebKeys.CAPTCHA_TEXT);

			if (captchaText != null) {
				if (!captchaText.equals(
						ParamUtil.getString(req, "captchaText"))) {

					throw new CaptchaTextException();
				}
				else {
					if (_log.isDebugEnabled()) {
						_log.debug("Captcha text is valid");
					}

					ses.removeAttribute(WebKeys.CAPTCHA_TEXT);

					if ((PropsValues.CAPTCHA_MAX_CHALLENGES > 0) &&
						(Validator.isNotNull(req.getRemoteUser()))) {

						Integer count = (Integer)ses.getAttribute(
							WebKeys.CAPTCHA_COUNT);

						if (count == null) {
							count = new Integer(1);
						}
						else {
							count = new Integer(count.intValue() + 1);
						}

						ses.setAttribute(WebKeys.CAPTCHA_COUNT, count);
					}
				}
			}
			else {
				if (_log.isErrorEnabled()) {
					_log.error(
						"Captcha text is null. User " + req.getRemoteUser() +
							" may be trying to circumvent the captcha.");
				}

				throw new CaptchaTextException();
			}
		}
	}

	public static boolean isEnabled(HttpServletRequest req) {
		if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
			HttpSession ses = req.getSession();

			Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);

			if ((count != null) &&
				(PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {

				return false;
			}
			else {
				return true;
			}
		}
		else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
			return false;
		}
		else {
			return true;
		}
	}

	public static boolean isEnabled(PortletRequest req) {
		if (PropsValues.CAPTCHA_MAX_CHALLENGES > 0) {
			PortletSession ses = req.getPortletSession();

			Integer count = (Integer)ses.getAttribute(WebKeys.CAPTCHA_COUNT);

			if ((count != null) &&
				(PropsValues.CAPTCHA_MAX_CHALLENGES <= count.intValue())) {

				return false;
			}
			else {
				return true;
			}
		}
		else if (PropsValues.CAPTCHA_MAX_CHALLENGES < 0) {
			return false;
		}
		else {
			return true;
		}
	}

	private static Log _log = LogFactory.getLog(CaptchaUtil.class);

}

显示标签相关:
<%@ include file="/html/taglib/init.jsp" %>

<%@ page import="com.liferay.portal.captcha.CaptchaUtil" %>

<%
String url = (String)request.getAttribute("liferay-ui:captcha:url");

boolean captchaEnabled = false;

if (renderRequest != null) {
	captchaEnabled = CaptchaUtil.isEnabled(renderRequest);
}
else {
	captchaEnabled = CaptchaUtil.isEnabled(request);
}
%>

<c:if test="<%= captchaEnabled %>">
	<div>
		<img src="<%= url %>" />
	</div>

	<br />

	<table class="lfr-table">
	<tr>
		<td>
			<liferay-ui:message key="text-verification" />
		</td>
		<td>
			<input name="<%= namespace %>captchaText" size="10" type="text" value="" />
		</td>
	</tr>
	</table>

	<br />
</c:if>

现在变懒了,不知道各位能看得明白不?最关键的是nl.captcha.servlet.CaptchaProducer和nl.captcha.util.Helper这个.
分享到:
评论

相关推荐

    Liferay Portal Liferay IDE

    Liferay Portal Liferay IDE

    liferay详细讲解 liferay项目完全讲解

    Liferay是一款开源的企业级门户平台,它提供了丰富的功能和高度可定制性,广泛应用于构建企业网站、内部系统、协作平台等。在这个“liferay详细讲解 liferay项目完全讲解”的资料包中,我们可以期待深入了解到关于...

    liferay扩展环境 liferay ext

    标题 "Liferay扩展环境 Liferay Ext" 涉及到的是Liferay门户平台的一个关键概念,Liferay Ext是用于扩展和定制Liferay功能的核心工具。Liferay是一个开源的企业级内容管理平台,它允许用户根据需求构建自定义的数字...

    liferay+cas实现单点登录步骤

    - 在Liferay中,你需要配置服务提供者(Service Provider)以识别CAS服务器作为认证源。这通常涉及修改Liferay的配置文件,如`portal-ext.properties`,并启用CAS插件或模块。 6. **配置CAS客户端** - 配置...

    Liferay的CAS SSO实现

    3. **设置Liferay安全策略**:在Liferay的控制台中,你需要为你的站点或特定的Web内容设置安全策略,启用CAS认证,并指定相应的CAS服务器配置。 4. **测试SSO功能**:完成配置后,可以通过尝试登录Liferay并访问受...

    liferay环境的搭建

    ### Liferay环境搭建详解 #### 一、简介 Liferay是一款功能强大的开源企业级门户平台,主要用于构建企业级Web应用程序和服务。本文档旨在详细介绍如何利用Eclipse集成开发环境(IDE)来搭建Liferay开发环境,并...

    Liferay合集.zip

    7. **安全与权限**:掌握Liferay的安全模型,包括用户认证、授权和权限控制。 8. **性能优化**:学习如何调整Liferay的配置以提高系统性能,包括缓存策略、数据库调优等。 9. **API与插件开发**:熟悉Liferay的...

    liferay快速入门quickstart

    ### Liferay快速入门知识点详解 #### 一、Liferay Portal简介与快速启动 Liferay Portal是一款功能强大且灵活的企业级门户平台,它基于Java技术,遵循JSR-168标准,提供了丰富的特性来满足企业内外部网站的需求。...

    Liferay 6 入门教程

    【Liferay 6 入门教程】 Liferay是一款开源的企业级门户平台,广泛用于构建企业网站、社交网络和协作工具。Liferay 6是其一个重要的版本,提供了丰富的功能和改进,包括更好的用户体验、增强的社区参与度以及更强大...

    liferay 6.2开发指南+用户手册

    Liferay 6.2是一款流行的开源企业级门户平台,它提供了丰富的功能,包括内容管理、社交媒体集成、工作流程以及自定义开发能力。本指南将基于提供的"liferay 6.2开发指南+用户手册"来深入讲解其核心知识点。 1. **...

    liferay经典书籍8本

    Liferay是一款开源的企业级门户平台,它提供了丰富的功能来构建和管理数字体验,包括网站创建、内容管理、社交协作以及工作流程。以下是对这8本Liferay经典书籍的知识点总结,旨在帮助读者深入理解并掌握Liferay的...

    liferay 整合struts例子

    Liferay是一款开源的企业级门户平台,它提供了丰富的功能和高度的可扩展性。Struts则是一个经典的MVC(模型-视图-控制器)框架,用于构建Java Web应用程序。将Liferay与Struts整合,可以充分利用Struts的业务逻辑...

    liferay 超级学习文档

    Liferay 是一个开源的企业级门户平台,它提供了一个强大的框架,用于构建和管理Web应用程序、内容和工作流程。本超学习文档将深入探讨Liferay的核心特性、开发环境、Java技术的应用以及Hibernate在Liferay中的整合。...

    liferay开发文档集合

    《Liferay开发文档集合》是针对企业级门户平台Liferay的一系列技术指南,涵盖了从基础安装到高级定制的全方位知识。这些PDF文档旨在帮助开发者、系统管理员和IT专业人员更好地理解和利用Liferay Portal来构建和管理...

    liferay+cas

    - **集成 Liferay**:在 Liferay 中配置 CAS 作为认证提供者,这可能涉及修改 Liferay 的 `portal-ext.properties` 文件,添加 CAS 服务器的相关配置,比如 CAS 服务器的 URL、服务验证 URL 等。 - **测试集成**:...

    liferay6.06

    《Liferay Portal 6.0.6 学习手册》是针对企业级开源门户平台Liferay Portal的一个详细学习指南,由作者李少华编写。本文将深入探讨Liferay Portal的基础知识,安装配置,源码分析,开发环境设置,以及基于Struts2的...

    Liferay入门帮助文档(Liferay开发指南)

    Liferay入门帮助文档是针对Liferay开发的一份指南,它主要涵盖了如何开始使用这个全面的门户解决方案。Liferay是一个基于Java Enterprise Edition (J2EE) 的应用,它整合了多种技术,如EJB (Enterprise JavaBeans) ...

    liferay

    ### Liferay Portal 二次开发详解 #### 一、Liferay Portal 概览 **Liferay Portal** 是一款开源的企业级门户平台,它基于Java技术构建,支持多种标准,包括JSR 168 和 WSRP (Web Services for Remote Portlets)。...

    LIFERAY的ppt课件

    **Liferay简介** Liferay是一款开源的企业级门户平台,它提供了强大的内容管理、协作和社交功能,被广泛用于构建企业内部或外部的Web应用程序。Liferay的核心特性包括网站构建、内容管理、工作流程、社区参与以及与...

Global site tag (gtag.js) - Google Analytics