- 浏览: 2539743 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
SpringSecurity with JQuery AJAX - Handle Session Timeout
1. Simple But not Best Solution
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="../resources/components/json/json2.js"></script>
<script type="text/javascript">
$(function() {
$("#GetAjax").click(function() {
var theId = $.trim($("#theId").val());
$.ajax({
type: "GET",
url: "../service/person/" + theId,
contentType: "application/json",
cache: false,
success: onSuccess
});
});
function onSuccess(data,status)
{
if (HasErrors(data)){
return;
}
$("#resultLog").html("Result: " + data.personName + " status:" + status);
}
});
function HasErrors(data) {
var data_str = JSON.stringify(data);
alert(data_str);
// check for redirect to login page
if (data_str.search(/j_spring_security_check/i) != -1) {
top.location.href = './openidlogin.jsp';
return true;
}
// check for IIS error page
if (data_str.search(/Internal Server Error/) != -1) {
alert('Server Error.');
return true;
}
// check for our custom error handling page
if (data_str.search(/Error.jsp/) != -1) {
alert('An error occurred on the server. The Technical Support Team has been provided with the error details.');
return true;
}
return false;
}
</script>
But this solution is not good.
2. Better Way to Handle this with Spring Security Server Side
Spring Configuration Changes
<security:http access-denied-page="/denied.jsp" use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint">
<bean id="authenticationProcessingFilterEntryPoint" class="com.sillycat.easyopenidgoogle.security.AjaxAwareAuthenticationEntryPoint">
<property name="loginFormUrl" value="/openidlogin.jsp"/>
<property name="forceHttps" value="false"/>
</bean>
The customized Java class is as follow:
package com.sillycat.easyopenidgoogle.security;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
public class AjaxAwareAuthenticationEntryPoint extends
LoginUrlAuthenticationEntryPoint {
private final Log log = LogFactory.getLog(this.getClass());
public void commence(HttpServletRequest request,
HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
if (request.getHeader("X-AjaxRequest") != null
&& request.getHeader("X-AjaxRequest").equals("1")) {
((HttpServletResponse) response).sendError(403, "");
log.debug("Ajax parameter: " + request.getHeader("X-AjaxRequest"));
} else {
super.commence(request, response, authException);
log.debug("Ajax parameter: " + request.getHeader("X-AjaxRequest"));
}
}
}
And our AJAX client codes will be as follow:
$.ajax({
type: "GET",
url: "../service/person/" + theId,
'beforeSend': function(data) {
data.setRequestHeader("X-AjaxRequest", "1");
},
contentType: "application/json",
cache: false,
success: onSuccess,
complete: function(data) {
//alert(data + " " + data.status);
if (data.status == 403) {
window.location.reload();
}
}
});
references:
http://forum.springsource.org/showthread.php?95881-Ajax-request-session-timeout
http://forum.springsource.org/showthread.php?85088-Spring-security-session-timeout-and-JQuery
http://stackoverflow.com/questions/3339431/how-to-handle-expired-session-using-spring-security-and-jquery
http://blog.csdn.net/foamflower/article/details/5802743
http://cyr520.blog.51cto.com/714067/759731
1. Simple But not Best Solution
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="../resources/components/json/json2.js"></script>
<script type="text/javascript">
$(function() {
$("#GetAjax").click(function() {
var theId = $.trim($("#theId").val());
$.ajax({
type: "GET",
url: "../service/person/" + theId,
contentType: "application/json",
cache: false,
success: onSuccess
});
});
function onSuccess(data,status)
{
if (HasErrors(data)){
return;
}
$("#resultLog").html("Result: " + data.personName + " status:" + status);
}
});
function HasErrors(data) {
var data_str = JSON.stringify(data);
alert(data_str);
// check for redirect to login page
if (data_str.search(/j_spring_security_check/i) != -1) {
top.location.href = './openidlogin.jsp';
return true;
}
// check for IIS error page
if (data_str.search(/Internal Server Error/) != -1) {
alert('Server Error.');
return true;
}
// check for our custom error handling page
if (data_str.search(/Error.jsp/) != -1) {
alert('An error occurred on the server. The Technical Support Team has been provided with the error details.');
return true;
}
return false;
}
</script>
But this solution is not good.
2. Better Way to Handle this with Spring Security Server Side
Spring Configuration Changes
<security:http access-denied-page="/denied.jsp" use-expressions="true" entry-point-ref="authenticationProcessingFilterEntryPoint">
<bean id="authenticationProcessingFilterEntryPoint" class="com.sillycat.easyopenidgoogle.security.AjaxAwareAuthenticationEntryPoint">
<property name="loginFormUrl" value="/openidlogin.jsp"/>
<property name="forceHttps" value="false"/>
</bean>
The customized Java class is as follow:
package com.sillycat.easyopenidgoogle.security;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
public class AjaxAwareAuthenticationEntryPoint extends
LoginUrlAuthenticationEntryPoint {
private final Log log = LogFactory.getLog(this.getClass());
public void commence(HttpServletRequest request,
HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
if (request.getHeader("X-AjaxRequest") != null
&& request.getHeader("X-AjaxRequest").equals("1")) {
((HttpServletResponse) response).sendError(403, "");
log.debug("Ajax parameter: " + request.getHeader("X-AjaxRequest"));
} else {
super.commence(request, response, authException);
log.debug("Ajax parameter: " + request.getHeader("X-AjaxRequest"));
}
}
}
And our AJAX client codes will be as follow:
$.ajax({
type: "GET",
url: "../service/person/" + theId,
'beforeSend': function(data) {
data.setRequestHeader("X-AjaxRequest", "1");
},
contentType: "application/json",
cache: false,
success: onSuccess,
complete: function(data) {
//alert(data + " " + data.status);
if (data.status == 403) {
window.location.reload();
}
}
});
references:
http://forum.springsource.org/showthread.php?95881-Ajax-request-session-timeout
http://forum.springsource.org/showthread.php?85088-Spring-security-session-timeout-and-JQuery
http://stackoverflow.com/questions/3339431/how-to-handle-expired-session-using-spring-security-and-jquery
http://blog.csdn.net/foamflower/article/details/5802743
http://cyr520.blog.51cto.com/714067/759731
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 466NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 361Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 363Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 419Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 285Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ...
相关推荐
赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...
赠送jar包:spring-security-crypto-5.6.1.jar; 赠送原API文档:spring-security-crypto-5.6.1-javadoc.jar; 赠送源代码:spring-security-crypto-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-...
赠送jar包:spring-security-core-5.5.2.jar; 赠送原API文档:spring-security-core-5.5.2-javadoc.jar; 赠送源代码:spring-security-core-5.5.2-sources.jar; 赠送Maven依赖信息文件:spring-security-core-...
赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...
赠送jar包:spring-security-core-5.3.9.RELEASE.jar; 赠送原API文档:spring-security-core-5.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-rsa-1.0.10.RELEASE.jar; 赠送原API文档:spring-security-rsa-1.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-security-rsa-1.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-core-5.2.0.RELEASE.jar; 赠送原API文档:spring-security-core-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-oauth2-2.3.5.RELEASE.jar; 赠送原API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-2.3.5.RELEASE-sources.jar; 赠送Maven依赖信息...
赠送jar包:spring-security-core-5.0.7.RELEASE.jar; 赠送原API文档:spring-security-core-5.0.7.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.0.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-web-5.6.1.jar; 赠送原API文档:spring-security-web-5.6.1-javadoc.jar; 赠送源代码:spring-security-web-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-web-5.6.1....
赠送jar包:spring-security-jwt-1.0.10.RELEASE.jar; 赠送原API文档:spring-security-jwt-1.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-security-jwt-1.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
spring-boot-security-saml, Spring Security saml与 Spring Boot的集成 spring-boot-security-saml这个项目在处理 spring-security-saml 和 Spring Boot 之间的平滑集成的同时,在处理内部的配置的gritty和锅炉板的...
赠送jar包:spring-security-oauth2-2.3.5.RELEASE.jar; 赠送原API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-2.3.5.RELEASE-sources.jar; 赠送Maven依赖信息...
赠送jar包:spring-security-core-5.6.1.jar; 赠送原API文档:spring-security-core-5.6.1-javadoc.jar; 赠送源代码:spring-security-core-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-core-...
赠送jar包:spring-security-oauth2-autoconfigure-2.1.8.RELEASE.jar; 赠送原API文档:spring-security-oauth2-autoconfigure-2.1.8.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-autoconfigure-...
赠送jar包:spring-security-crypto-5.6.1.jar; 赠送原API文档:spring-security-crypto-5.6.1-javadoc.jar; 赠送源代码:spring-security-crypto-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-...
赠送jar包:spring-security-web-5.2.0.RELEASE.jar; 赠送原API文档:spring-security-web-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-security-web-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-oauth2-autoconfigure-2.1.8.RELEASE.jar; 赠送原API文档:spring-security-oauth2-autoconfigure-2.1.8.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-autoconfigure-...
赠送jar包:spring-security-web-5.0.7.RELEASE.jar; 赠送原API文档:spring-security-web-5.0.7.RELEASE-javadoc.jar; 赠送源代码:spring-security-web-5.0.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-security-core-5.5.2.jar; 赠送原API文档:spring-security-core-5.5.2-javadoc.jar; 赠送源代码:spring-security-core-5.5.2-sources.jar; 赠送Maven依赖信息文件:spring-security-core-...