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这个.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值