• 大小: 31.21MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-06-28
  • 语言: 其他
  • 标签: Shiro  ajax  ssh  

资源简介

Shiro和AJAX完美整合

资源截图

代码片段和文件信息

/**
 * 
 */
package com.wms.studio.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author WMS
 * 
 */
public class CaptchaFormAuthenticationFilter extends FormAuthenticationFilter {

private static final Logger log = LoggerFactory
.getLogger(CaptchaFormAuthenticationFilter.class);

/*
 * 主要是针对登入成功的处理方法。对于请求头是AJAX的之间返回JSON字符串。
 */
@Override
protected boolean onLoginSuccess(AuthenticationToken token
Subject subject ServletRequest request ServletResponse response)
throws Exception {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

if (!“xmlHttpRequest“.equalsIgnoreCase(httpServletRequest
.getHeader(“X-Requested-With“))) {// 不是ajax请求
issueSuccessRedirect(request response);
} else {
httpServletResponse.setCharacterEncoding(“UTF-8“);
PrintWriter out = httpServletResponse.getWriter();
out.println(“{success:truemessage:‘登入成功‘}“);
out.flush();
out.close();
}
return false;
}

/**
 * 主要是处理登入失败的方法
 */
@Override
protected boolean onLoginFailure(AuthenticationToken token
AuthenticationException e ServletRequest request
ServletResponse response) {
if (!“xmlHttpRequest“.equalsIgnoreCase(((HttpServletRequest) request)
.getHeader(“X-Requested-With“))) {// 不是ajax请求
setFailureAttribute(request e);
return true;
}
try {
response.setCharacterEncoding(“UTF-8“);
PrintWriter out = response.getWriter();
String message = e.getClass().getSimpleName();
if (“IncorrectCredentialsException“.equals(message)) {
out.println(“{success:falsemessage:‘密码错误‘}“);
} else if (“UnknownAccountException“.equals(message)) {
out.println(“{success:falsemessage:‘账号不存在‘}“);
} else if (“LockedAccountException“.equals(message)) {
out.println(“{success:falsemessage:‘账号被锁定‘}“);
} else {
out.println(“{success:falsemessage:‘未知错误‘}“);
}
out.flush();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}

/**
 * 所有请求都会经过的方法。
 */
@Override
protected boolean onAccessDenied(ServletRequest request
ServletResponse response) throws Exception {

if (isLoginRequest(request response)) {
if (isLoginSubmission(request response)) {
if (log.isTraceEnabled()) {
log.trace(“Login submission detected.  Attempting to execute login.“);
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-09-21 08:23  Shiro+AJAX+SSH\
     文件         783  2013-09-21 08:22  Shiro+AJAX+SSH\.classpath
     目录           0  2013-09-21 07:46  Shiro+AJAX+SSH\.myeclipse\
     文件         315  2013-09-21 07:46  Shiro+AJAX+SSH\.mymetadata
     文件        1757  2013-09-21 08:03  Shiro+AJAX+SSH\.project
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\.settings\
     文件         500  2013-09-21 07:46  Shiro+AJAX+SSH\.settings\.jsdtscope
     文件          57  2013-09-21 07:54  Shiro+AJAX+SSH\.settings\org.eclipse.core.resources.prefs
     文件         364  2013-09-21 07:46  Shiro+AJAX+SSH\.settings\org.eclipse.jdt.core.prefs
     文件         752  2013-09-21 07:51  Shiro+AJAX+SSH\.settings\org.eclipse.wst.common.component
     文件         252  2013-09-21 07:46  Shiro+AJAX+SSH\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2013-09-21 07:46  Shiro+AJAX+SSH\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2013-09-21 07:46  Shiro+AJAX+SSH\.settings\org.eclipse.wst.jsdt.ui.superType.name
     文件         394  2013-09-21 08:27  Shiro+AJAX+SSH\ReadMe.txt
     目录           0  2013-09-21 07:51  Shiro+AJAX+SSH\src\
     目录           0  2013-09-21 07:51  Shiro+AJAX+SSH\src\main\
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\com\
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\com\wms\
     目录           0  2013-09-21 08:06  Shiro+AJAX+SSH\src\main\java\com\wms\studio\
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\com\wms\studio\filter\
     文件        4732  2013-09-18 20:14  Shiro+AJAX+SSH\src\main\java\com\wms\studio\filter\CaptchaFormAuthenticationFilter.java
     目录           0  2013-09-21 07:57  Shiro+AJAX+SSH\src\main\java\com\wms\studio\model\
     文件        1492  2013-09-21 08:10  Shiro+AJAX+SSH\src\main\java\com\wms\studio\model\Role.java
     文件        1838  2013-09-21 08:13  Shiro+AJAX+SSH\src\main\java\com\wms\studio\model\User.java
     目录           0  2013-09-21 08:07  Shiro+AJAX+SSH\src\main\java\com\wms\studio\repository\
     文件         262  2013-09-16 14:47  Shiro+AJAX+SSH\src\main\java\com\wms\studio\repository\RoleRepository.java
     文件         591  2013-09-20 09:56  Shiro+AJAX+SSH\src\main\java\com\wms\studio\repository\UserRepository.java
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\com\wms\studio\security\
     目录           0  2013-09-21 07:54  Shiro+AJAX+SSH\src\main\java\com\wms\studio\security\utils\
     文件        5637  2013-09-16 15:58  Shiro+AJAX+SSH\src\main\java\com\wms\studio\security\utils\Cryptos.java
............此处省略3328个文件信息

评论

共有 条评论