- 验证码采用一个博客上的验证码 点击跳转
- 采用action的形式获取验证码,并把验证码存入到shiro管理的session中
public void getGifCode(HttpServletResponse response,HttpServletRequest request){
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/gif");
/**
* gif格式动画验证码
* 宽,高,位数。
*/
Captcha captcha = new GifCaptcha(146,42,4);
//输出
ServletOutputStream out = response.getOutputStream();
captcha.out(out);
out.flush();
//存入Shiro会话session
System.out.println( captcha.text().toLowerCase());
TokenManager.setVal2Session(VerifyCodeUtils.V_CODE, captcha.text().toLowerCase());
} catch (Exception e) {
LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
}
}
- 验证码验证的 Shiro 过滤器
public class JCaptchaValidateFilter extends AccessControlFilter{
/**
* 是否开启验证码验证 默认true
*/
private boolean jcaptchaEbabled = true;
/**
* 前台提交的验证码参数名
*/
private String jcaptchaParam = "jcaptchaCode";
/**
* 验证失败后存储到的属性名
*/
private String failureKeyAttribute = "shiroLoginFailure";
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object o) throws Exception {
//1、设置验证码是否开启属性,页面可以根据该属性来决定是否显示验证码
request.setAttribute("jcaptchaEbabled", jcaptchaEbabled);
HttpServletRequest httpRequest = WebUtils.toHttp(request);
//2、判断验证码是否禁用 或不是表单提交(允许访问)
if (jcaptchaEbabled == false || !"post".equalsIgnoreCase(httpRequest.getMethod())) {
return true;
}
//表单提交,校验验证码的正确性
String storedCode = getSubject(request, response).getSession().getAttribute(Constatns.VERIFYCODE).toString();
String currentCode = httpRequest.getParameter(jcaptchaParam);
return StringUtils.equalsIgnoreCase(storedCode, currentCode);
}
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse servletResponse) throws Exception {
//如果验证码失败了,存储失败 key 属性
request.setAttribute(failureKeyAttribute, "jCaptcha.error");
return true;
}
public String getFailureKeyAttribute() {
return failureKeyAttribute;
}
public void setFailureKeyAttribute(String failureKeyAttribute) {
this.failureKeyAttribute = failureKeyAttribute;
}
public String getJcaptchaParam() {
return jcaptchaParam;
}
public void setJcaptchaParam(String jcaptchaParam) {
this.jcaptchaParam = jcaptchaParam;
}
public boolean isJcaptchaEbabled() {
return jcaptchaEbabled;
}
public void setJcaptchaEbabled(boolean jcaptchaEbabled) {
this.jcaptchaEbabled = jcaptchaEbabled;
}
}
- MyFormAuthenticationFilter ,表单提交时,先验证验证码,如果验证成功再走登录流程
用于验证码验证的 Shiro 拦截器在用于身份认证的拦截器之前运行;但是如果验证码验证拦截器失败了,就不需要进行身份认证拦截器流程了;所以需要修改下如FormAuthenticationFilter 身份认证拦截器,当验证码验证失败时不再走身份认证拦截器。
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
if(request.getAttribute(getFailureKeyAttribute()) != null) {
return true;
}
return super.onAccessDenied(request, response);
}
}
- 配置文件的配置
<!-- 基于form表单的身份验证过滤器 带有验证码的过滤-->
<bean id="myformAuthenticationFilter" class="com.nanc.core.shiro.filter.MyFormAuthenticationFilter">
<property name="usernameParam" value="username"/>
<property name="passwordParam" value="password"/>
<property name="rememberMeParam" value="rememberMe"/>
<property name="loginUrl" value="/login"/>
<property name="failureKeyAttribute" value="shiroLoginFailure"/>
</bean>
<!-- 验证码的过滤器 -->
<bean id="jCaptchaFilter" class="com.nanc.core.shiro.filter.JCaptchaValidateFilter">
<property name="failureKeyAttribute" value="shiroLoginFailure"/>
<property name="jcaptchaParam" value="jcaptchaCode"/><!-- 页面的参数名-->
<property name="jcaptchaEbabled" value="true"/>
</bean>
<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="filters">
<map>
<!--<entry key="authc" value-ref="formAuthenticationFilter"/>-->
<entry key="authc" value-ref="myformAuthenticationFilter"/>
<entry key="jcaptcha" value-ref="jCaptchaFilter"/>
<entry key="sysUser" value-ref="sysUserFilter"/>
<entry key="kickout" value-ref="kickoutFilter"/>
<!--添加ssl支持-->
<!--<entry key="ssl" value-ref="sslFilter"/>-->
<!-- 结束ssl支持-->
</map>
</property>
<property name="filterChainDefinitions">
<value>
/login=jcaptcha,authc
/loginDialog=authc
/logout=logout
/authenticated=authc
/dwz_jui/**=anon
/common/**=anon
/**=kickout,user,sysUser
</value>
</property>
</bean>