当前位置: 首页 > news >正文

可以做防盗水印的网站德州市建设局质监站网站

可以做防盗水印的网站,德州市建设局质监站网站,建设部网站怎么查询企业业绩,便宜做网站8818我看过一些有关Spring Security 3 Ajax登录的博客#xff0c;但是我找不到解决如何调用基于Ajax的登录的博客#xff0c;匿名用户正在Ajax中访问受保护的资源。 问题 – Web应用程序允许匿名访问某些部分#xff0c;并且某些部分是受保护的资源#xff0c;需要用户登录。 … 我看过一些有关Spring Security 3 Ajax登录的博客但是我找不到解决如何调用基于Ajax的登录的博客匿名用户正在Ajax中访问受保护的资源。 问题 – Web应用程序允许匿名访问某些部分并且某些部分是受保护的资源需要用户登录。 当匿名用户通过Http Get / Post访问受保护的资源时Spring Security会自动调用登录页面并在成功通过身份验证后重定向到所需的资源/页面。 但是如果正在Ajax中访问受保护的资源则登录页面将无法正确显示将在页面的一部分上进行设置。 302代码重定向到登录页面将无法在Ajax中正常运行。 请注意这与启动Ajax登录屏幕不同例如当用户按下登录按钮并调用带有用户/密码字段的弹出窗口时。 那么–我们如何让Spring Security 3通过“常规” HTTP Post基于FORM的身份验证和Ajax调用来处理对受保护资源的访问包括在成功身份验证后重定向到所需资源 因此此博客文章包含两个保护层/部分 1. Spring Security 3标准基于FORM的身份验证 2.配置/扩展Spring Security 3.并且该应用程序还支持Ajax对受保护资源的访问。 关于第1部分-有关此问题的参考很多。 无需详细说明。 关于第2部分–要求以下内容 1.配置Spring Security 3以启用基于Ajax的登录。 2.将客户端Ajax调用配置为受保护的资源以处理身份验证请求。 3.重新执行功能以模拟成功登录后自动进行用户原始方法的调用这在基于FORM的登录中发生 下图描述了详细的流程应有助于遵循客户端/服务器通信。 通过Ajax处理受保护的资源访问 让我们讨论一下图 该流程始于对受保护资源1的匿名用户Ajax请求。 在这种情况下用户希望将商品添加到购物车。 addItem方法是受保护的资源它通过Spring Securitypre_authorize“ SOME_ROLE”2受保护。 这使Spring Secutiry过滤器3发送带有HTTP代码302的登录表单即重定向到该页面。 现在由于这是一个Ajax调用它将无法很好地处理请求因此这里涉及到了登录表单将其放在一边然后调用基于Ajax的登录4 客户端Ajax方法调用了Ajax addItem方法检查​​它是基于表单的登录名还是其他任何答复。 如果是基于FORM的登录它将调用一个对话框模式5该模式将尝试登录Ajax。 Spring将处理Ajax登录认证6并将适当的消息返回给客户端。 如果消息成功则客户端将重新执行原始功能该功能试图访问受保护的资源例如本例中的addItem 。 让我们看看它们如何适合我们的代码 步骤14 –客户端访问受保护的资源并检查是否需要登录 //JavaScript method - Ajax call to protected resource (#1 in flow diagram) function addItem(itemId) { $.ajax({url: /my_url/order/addItem,type: POST,data: ({orderItemId : itemId,...}), success: function(data) {//construct a callback string if user is not logged in.var cllbck addItem(itemId );//Client check if login required//(#4 in flow diagram)if (verifyAuthentication(data,cllbck)){// in here access to protected resource was ok// show message to user, item has been added...}});} 步骤23 –是常规的Spring Security配置。 的大量资源 了 那里 。 步骤4 –客户端检查是否需要登录 function verifyAuthentication(data, cllBackString){//naive check - I put a string in the login form, so I check for existanceif (isNaN(data) (data.indexOf(login_hidden_for_ajax)! -1)){//if got here then data is a loginform login required//set callback in ajax login form hidden input $(#my_callback).val(cllBackString); //show ajax login//Get the window height and widthvar winH $(window).height();var winW $(window).width();//Set the popup window to center$(#ajaxLogin).css(top, winH/2-$(#ajaxLogin).height()/2);$(#ajaxLogin).css(left, winW/2-$(#ajaxLogin).width()/2);$(#ajaxLogin).fadeIn(2000); return false;} // data is not a login form return true to continue with function processingreturn true; } 步骤57 – Ajax登录表单使用以下Ajax登录 function ajaxLogin(form, suffix){var my_callback form.my_callback.value; // The original function which accessed the protected resourcevar user_pass form.j_ajax_password.value;var user_name form.j_ajax_username.value; //Ajax login - we send credentials to j_spring_security_check (as in form based login$.ajax({url: /myContextURL/j_spring_security_check, data: { j_username: user_name , j_password: user_pass }, type: POST,beforeSend: function (xhr) {xhr.setRequestHeader(X-Ajax-call, true);},success: function(result) { //if login is success, hide the login modal and//re-execute the function which called the protected resource//(#7 in the diagram flow)if (result ok) {$(#ajax_login_error_ suffix).html(); $(#ajaxLogin).hide();if (my_callback!null my_callback!undefined my_callback!){eval(my_callback.replace(/_/g,));}return true;}else { $(#ajax_login_error_ suffix).html(span classalert display_b clear_b centeralignBad user/password/span) ;return false; }},error: function(XMLHttpRequest, textStatus, errorThrown){$(#ajax_login_error_ suffix).html(Bad user/password) ;return false; } }); } 我们需要将Spring设置为支持Ajax登录6 设置Spring Security xml配置 beans:beans xmlns:beanshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://www.springframework.org/schema/security xsi:schemalocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsdhttp auto-configfalse use-expressionstrueintercept-url accesshasRole(ROLE_ADMIN) pattern/admin**intercept-url filtersnone pattern/**intercept-url accesspermitAll pattern/signin/**form-login authentication-failure-handler-refajaxAuthenticationFailureHandler authentication-success-handler-refajaxAuthenticationSuccessHandler login-page/common/authentication/login logout invalidate-sessiontrue logout-success-url/common/authentication/logoutcustom-filter beforeLOGOUT_FILTER reflogoutFilter/custom-filter/logout/form-login/intercept-url/intercept-url/intercept-url/http... /beans:beans 定义成功登录的处理程序 Component(ajaxAuthenticationSuccessHandler) public class AjaxAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { public AjaxAuthenticationSuccessHandler() { }Overridepublic void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response, Authentication authentication)throws IOException, ServletException { HttpSession session request.getSession(); DefaultSavedRequest defaultSavedRequest (DefaultSavedRequest) session.getAttribute(WebAttributes.SAVED_REQUEST);//check if login is originated from ajax callif (true.equals(request.getHeader(X-Ajax-call))) {try {response.getWriter().print(ok);//return ok stringresponse.getWriter().flush();} catch (IOException e) { //handle exception...}} else { setAlwaysUseDefaultTargetUrl(false); ...}} } 为登录失败定义一个处理程序–与成功相同但是字符串为“ not-ok”。 我知道这里的某些代码不是最佳做法所以我想听听您的想法。 如果您能看到改进流程或使其更通用的方法请发给我。 鸣谢通过gliffy完成了图表-在线图表工具 参考 Spring security 3 Ajax登录–通过 Gal Levinsky博客博客中的JCG合作伙伴 Gal Levinsky 访问受保护的资源 。 翻译自: https://www.javacodegeeks.com/2012/08/spring-security-3-ajax-login-accessing.html
http://www.sadfv.cn/news/373258/

相关文章:

  • 哈尔滨的网站建设公司石景山做网站的公司
  • 去哪里可以做网站广告公司的电话
  • 网站备案可以强制撤销吗佛山网站建设的大品牌
  • 网站删除留言板功能删除简单的网站构建的基本流程
  • 太原制作网站的工作室网站备案帐号
  • 青岛网站建设服务器wordpress 后台 留言
  • 河南和城乡建设厅网站小程序登陆官网
  • 怎么建立微信网站成都营销网站设计
  • 昆明网站建设大全恐龙网站建设
  • 企业网站提交专做ppt的网站
  • phpstorm做网站做网站前期框架图
  • 东莞市公租房申请网站-建设网深圳专业网站公司
  • 别人盗用我的网站备案号怎么办上海seo公司
  • 网站选择语言怎么做徐州集团网站建设公司
  • sap和国家网站做接口国内搜索引擎排名2022
  • 重庆平台网站建设找哪家wordpress安装模板文件
  • 公司网站包含哪些内容知彼网络网站建设
  • 网站开发常用的数据库网络推广培训网站
  • 网站wap版影响权重么建网站平台哪家好
  • 华大基因 网站公司建设湖北省建设厅网站首页
  • 建站之星网站 seo优化展厅展览
  • 建筑人才招聘网站平台江阴百度推广公司
  • 如果让你建设一个网站网站页面设计素材
  • 建网站 教程重庆装修价格
  • 网站开发需要如何压缩代码html5建站系统
  • 网站如何建设移动端数据上传网站
  • 搭建网站有费用吗营销的目的有哪些
  • 广州建设教育网站快速做课件的网站
  • 网站做点线表格经典软文案例50字
  • cps推广优化大师是什么