Skip to content

Commit

Permalink
第二十二章
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangkaitao committed Mar 4, 2014
1 parent 83b1a22 commit be73dde
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* <p>Version: 1.0
*/
public class JCaptcha {
public static final EsManageableImageCaptchaService captchaService
= new EsManageableImageCaptchaService(new FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000, 75000);
public static final MyManageableImageCaptchaService captchaService
= new MyManageableImageCaptchaService(new FastHashMapCaptchaStore(), new GMailEngine(), 180, 100000, 75000);


public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.github.zhangkaitao.shiro.chapter22.jcaptcha;

import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
Expand All @@ -19,50 +20,39 @@
*/
public class JCaptchaValidateFilter extends AccessControlFilter {

private boolean jcaptchaEbabled = true;
private boolean jcaptchaEbabled = true;//是否开启验证码支持

private String jcaptchaParam = "jcaptchaCode";
private String jcaptchaParam = "jcaptchaCode";//前台提交的验证码参数名

private String failureKeyAttribute = "shiroLoginFailure";
private String failureKeyAttribute = "shiroLoginFailure"; //验证码验证失败后存储到的属性名

/**
* 是否开启jcaptcha
*
* @param jcaptchaEbabled
*/
public void setJcaptchaEbabled(boolean jcaptchaEbabled) {
this.jcaptchaEbabled = jcaptchaEbabled;
}

/**
* 前台传入的验证码
*
* @param jcaptchaParam
*/
public void setJcaptchaParam(String jcaptchaParam) {
this.jcaptchaParam = jcaptchaParam;
}

public void setFailureKeyAttribute(String failureKeyAttribute) {
this.failureKeyAttribute = failureKeyAttribute;
}

@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
//1、设置验证码是否开启属性,页面可以根据该属性来决定是否显示验证码
request.setAttribute("jcaptchaEbabled", jcaptchaEbabled);

HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//验证码禁用 或不是表单提交 允许访问
HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
//2、判断验证码是否禁用 或不是表单提交允许访问
if (jcaptchaEbabled == false || !"post".equalsIgnoreCase(httpServletRequest.getMethod())) {
return true;
}
//3、此时是表单提交,验证验证码是否正确
return JCaptcha.validateResponse(httpServletRequest, httpServletRequest.getParameter(jcaptchaParam));
}

@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//如果验证码失败了,存储失败key属性
request.setAttribute(failureKeyAttribute, "jCaptcha.error");
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* <p>Date: 13-3-22 下午3:38
* <p>Version: 1.0
*/
public class EsManageableImageCaptchaService extends DefaultManageableImageCaptchaService {
public class MyManageableImageCaptchaService extends DefaultManageableImageCaptchaService {

public EsManageableImageCaptchaService(com.octo.captcha.service.captchastore.CaptchaStore captchaStore, com.octo.captcha.engine.CaptchaEngine captchaEngine, int minGuarantedStorageDelayInSeconds, int maxCaptchaStoreSize, int captchaStoreLoadBeforeGarbageCollection) {
public MyManageableImageCaptchaService(com.octo.captcha.service.captchastore.CaptchaStore captchaStore, com.octo.captcha.engine.CaptchaEngine captchaEngine, int minGuarantedStorageDelayInSeconds, int maxCaptchaStoreSize, int captchaStoreLoadBeforeGarbageCollection) {
super(captchaStore, captchaEngine, minGuarantedStorageDelayInSeconds, maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@
<property name="jcaptchaParam" value="jcaptchaCode"/>
<property name="failureKeyAttribute" value="shiroLoginFailure"/>
</bean>


<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
Expand Down

0 comments on commit be73dde

Please sign in to comment.