1》 实现过滤器
/**
*
*/
package ******************;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.context.HttpSessionContextIntegrationFilter;
import org.springframework.web.filter.OncePerRequestFilter;
import flexjson.JSONSerializer;
/**
* function:
*
* @author LJ
*
*/
public class LoginAjaxFilter extends OncePerRequestFilter {
private static final Logger log = LoggerFactory
.getLogger(LoginAjaxFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// 检查提交的变量中是否有ajax请求的变量,如果没有,则不是ajax的登录请求,则走默认的请求。
if (!isAjaxRequest(request)) {
filterChain.doFilter(request, response);
return;
}
log.debug("AjaxSecurityFilter: Processing an AJAX call : "
+ request.getRequestURL());
RedirectResponseWrapper redirectResponseWrapper = new RedirectResponseWrapper(
response);
filterChain.doFilter(request, redirectResponseWrapper);
Map<String, String> map = new HashMap<String, String>();
if (redirectResponseWrapper.getRedirect() != null) {
String redirectURL = redirectResponseWrapper.getRedirect();
HttpSession httpSession = request.getSession();
if (redirectURL.indexOf("login") != -1) {
// populate your reply in this case the json object
// with what ever information needed to pop up your login window
if (redirectURL.indexOf("login_error=1") != -1) {
// 登录失败
map.put("success", "false");
}
}
// / your auth is successful the call is successful
else {
// you can return the user name and password in the reply so it
// can be displayed for example in you app
SecurityContext ctx = (SecurityContext) httpSession
.getAttribute(HttpSessionContextIntegrationFilter.SPRING_SECURITY_CONTEXT_KEY);
if (ctx != null) {
Authentication auth = ctx.getAuthentication();
UserDetails user = (UserDetails) auth.getPrincipal();
if (user != null) {
map.put("username", user.getUsername());
map.put("success", "true");
} else {
// 登录失败
map.put("success", "false");
map.put("errorMsg", "error");
}
} else {
map.put("success", "false");
}
}
try {
String outString = new JSONSerializer().serialize(map);
log.debug("jsonString : "+outString);
response.getWriter().write(outString);
} catch (Exception e) {
log.error("{}",e.getMessage());
}
}
}
/**
* @param request
* the request object
* @return true if this request is an ajax request. This is determined by a
* configured name/value pair that is applied to the request header
*/
protected boolean isAjaxRequest(HttpServletRequest request) {
// test with our ajax request pairs
String ajax = request.getParameter("ajax");
if ("".equals(ajax) || ajax == null) {
return false;
}
return true;
}
protected class JsonFlag{
String success;
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
String username;
}
}
2》 web.xml
<!-- 异步登录过滤器 filter -->
<filter>
<filter-name>loginAjaxFilter</filter-name>
<filter-class>com.book511.app.web.login.LoginAjaxFilter</filter-class>
</filter>
<!-- 乱码处理 放置第一位filter-mapping -->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 异步登录过滤器 map -->
<filter-mapping>
<filter-name>loginAjaxFilter</filter-name>
<url-pattern>/j_spring_security_check</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3 》 ajaxLogin.js
//弹出窗口
function openLoginDiv(){
var htl = "<div id='loginFormDiv'><span class='span_error' id='login_error'></span><a href='javascript:void(0);' onclick='closeDiv();'>关闭窗口</a><form action='##' method='post'><table>"
+ "<tr><td colspan='2'><span style='color:#FF0000;' id='login_error'></span></td></tr>"
+ "<tr><td class='td_name'>用户名:</td><td class='td_text'><input type='text' name='j_username' id='j_username' /></td></tr>"
+ "<tr><td class='td_name'>密码:</td><td class='td_text'><input type='password' name='j_password' id='j_password' /></td></tr>"
+ "<tr><td colspan='2' class='td_but'><input type='button' id='login_button' onclick='doLogin();' value='登录' /></td></tr>"
+ "</table></form></div><div id='bg' class='bg' style='display: none;'></div>"+
"<iframe id='popIframe' class='popIframe' frameborder='0'></iframe>";
if (!$(".loginDiv").hasClass(
"loginDiv")) {
//alert(htl);
// 动态写一个div弹出层
$("<div>", {
"class" : "loginDiv",
"id":"loginDiv"
}).append(htl).appendTo("body");
}
document.getElementById('popIframe').style.display = 'block';
document.getElementById('bg').style.display = 'block';
}
// 关闭窗口
function closeDiv(){
$("#loginDiv").remove();
document.getElementById('bg').style.display='none';
document.getElementById('popIframe').style.display='none';
window.location.reload(true);
}
//登录操作
function doLogin(){
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
var redirectURL = location.href;
$.ajax({
type : "POST",
url : "/app/j_spring_security_check",
data : "j_username="
+ j_username
+ "&j_password="
+ j_password
+ "&ajax=ajax"+ "&redirectURL="+redirectURL,
success : function(msg) {
eval("var jsonObj="+ msg);
if (jsonObj.success == "true") {
// 如果登录成功,则跳转到。
alert("登录成功");
window.location.reload(true);
//closeDiv();
//alert(111);
} else if (jsonObj.success = "false") {
// 写入登录失败信息
var errors = "对不起,用户名或密码不正确!";
$("#login_error").html(errors);
}
}
});
}
4 》
相关推荐
3. Spring Security 4.2的新特性 27 3.1 Web改进 27 3.2配置改进 28 3.3杂项 28 4.样品和指南(从这里开始) 28 5. Java配置 29 5.1 Hello Web安全Java配置 29 5.1.1 AbstractSecurityWebApplicationInitializer 31 ...
基于RBAC模型构建权限管理模块,并集成安全框架SpringSecurity,实现用户的认证和授权。 使用Spring Data集成缓存中间件Redis,加快访问速度。 使用Spring Data集成全文搜索搜索引擎ElasticSearch,实现文章信息的...
主要介绍了Spring Boot使用Spring的异步线程池的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Cloud、Spring Security & Spring Security OAuth2,博客Spring系列源码 一、Spring Boot教程 开启Spring Boot Spring Boot基础配置 Spring Boot中使用...
基于SpringSecurity框架的表单/验证码/手机验证码登录方式. 基于SpringSocial的社交登录(qq和微信) 基于SpringSecurityOAuth2的,将自己作为认证服务器和资源服务器.并使用社交/表单/验证码/手机验证码登录方式 获取...
实现了Ajax异步添加修改删除用户,整合了SpringSecurity,jpa等一堆框架...文章说明在https://blog.csdn.net/woshilishu/article/details/89351894
使用Spring Security 做权限控制,替代拦截器的拦截控制,并使用自己的认证方案替代Security 认证流程,使权限认证和控制更加方便灵活。 使用Redis的set实现点赞,zset实现关注,并使用Redis存储登录ticket和验证码...
RocketMQ|[Spring Cloud Alibaba(五)RocketMQ 异步通信实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README5.md) ### 其它 模块名称|主要内容 ---|--- leetcode|[力扣题解...
使用Spring Email + Interceptor + Spring Security等实现网站权限模块开发,完成注册、登录、退出、状态、设置、授权等功能。 实现网站核心功能,包括首页、帖子、评论、私信、敏感词过滤、全局异常处理、统一日志...
- chapter4-3-1:[使用Spring Security](http://blog.didispace.com/springbootsecurity/) - chapter4-3-2:[使用Spring Session(未完成)] #### 缓存支持 - chapter4-4-1:[注解配置与EhCache使用]...
chapter4-3-1:使用Spring Security chapter4-3-2:[使用Spring Session(未完成)] 缓存支持 chapter4-4-1:注解配置与EhCache使用 chapter4-4-2:使用Redis做集中式缓存 邮件发送 chapter4-5-1:实现邮件发送:...
准备 RESTful API 上一章环境搭建,搭建结果: 下载 这章准备一堆 api ,后面基于这些 api 做 认证授权 ...先写好测试类,指定期望的响应码 200 和 期望得到的数据长度 3 测试类 package cn.vshop.security.we
1、登录注册:使用SpringSecurity4框架,即使用已经包装好的接口来实现,简单易用。 2、上传照片:照片是存储在第三方服务器,即七牛云。 3、站内信通知:通过异步队列来实现的站内信通知,其中选择Redis来作为队列...
springboot+springsecurity+jpa+themeleaf+kafka 主要学习日志处理,spring security安全控制 用jpa访问数据库,封装自定义条件查询方法,方便,减少sql语句编写。 采用pagehelper分页处理。 使用springsecurity安全...
安全认证与授权:系统实现用户的安全认证和授权,如使用Spring Security、OAuth2等,保护用户信息和资源的安全性。 监控与日志:系统使用监控工具和日志系统,如Spring Cloud Sleuth、Zipkin、ELK Stack等,实现对...
使用Redis缓存存储菜单列表,使用SpringSecurity角色权限验证,使用WebSocket进行在线聊天,使用 FastDFS存储静态资源文件 实现的功能 实现了登录功能、职位管理、职称管理、部门管 理、操作员管理、员工管理、工资...
1、登录注册:使用SpringSecurity4框架,即使用已经包装好的接口来实现,简单易用。 2、上传照片:照片是存储在第三方服务器,即七牛云。 3、站内信通知:通过异步队列来实现的站内信通知,其中选择Redis来作为队列...
2021年蚂蚁java架构师第七/八期含项目(169G) 〖课程介绍〗: 2021年蚂蚁java架构师第七/八期含项目(169G) 〖课程目录〗: 01架构师必备技能之设计模式 01.站在架构师角度分析...11.springsecurity整合jwt实现接口授权0
涵盖使用Spring Boot 进行Java EE 开发的绝大数应用场景,包含:Web 开发、数据访问、安全控制、批处理、异步消息、系统集成、开发与部署、应用监控、分布式系统开发等。 第一部分 点睛Spring 4.x 第1 章 Spring ...