usherlight 发表于 2013-1-23 01:19:34

spring security ajax示例

a. applicationContext-security.xml
<form-login login-page="/login" default-target-url="/frames"login-processing-url="/securitycheck" always-use-default-target="true" /><session-management invalid-session-url="/logintimeout" />

b. index.ftl -- freemarker template

<#macro msg key> ${rc.getMessage(key)} </#macro><script src="${rc.getContextPath()}/static/scripts/jquery-1.6.2.min.js" type="text/javascript"></script><script src="${rc.getContextPath()}/static/scripts/jquery.easyui.min.js" type="text/javascript"></script><script src="${rc.getContextPath()}/static/scripts/jquery.validate.min.js" type="text/javascript"></script><form id="loginForm" action="${rc.getContextPath()}/securitycheck" method="post"><table align="center" style="border-spacing: 5px"><tr><td colspan="2" style="color:#FF0000" align="left"><#if errors><@msg key="page.login.fail"/></#if></td></tr><tr><td align="right"><@msg key="page.login.label.username"/>:</td><td><input type='text' name='j_username' class="required" /></td></tr><tr><td align="right"><@msg key="page.login.label.password"/>:</td><td><input type='password' name='j_password' class="required" /></td></tr><tr style="display:none"><td><input type="checkbox" name="_spring_security_remember_me" /></td><td>Remember me for 2 weeks</td></tr><tr><td colspan='2' align="right"><input value='<@msg key="page.login.button.submit"/>' type="submit" class="btnorange" /></td></tr></table></form><script>$('#loginForm').form({      onSubmit: function(){          var valid = $('#loginForm').valid();      if ( ! valid ) {      // return false to prevent submit;      return false;      }      return true;      },      success:function(data){    if ( data == null || data.length < 2 ) {    } else {    $.get('${rc.getContextPath()}/frames', function(data) {    $('body').empty().html(data).find('.easyui-layout').layout();    });    }    }});</script>

c. LoginController.java

@RequestMapping("/login")public String index(Model model, HttpServletRequest request, HttpServletResponse response) {AuthenticationException ae = (AuthenticationException) request.getSession().getAttribute(AUTHENTICATION_EXCEPTION);model.addAttribute("errors", false);if (ae != null) {model.addAttribute("errors", true);}return "/index";}@RequestMapping("/frames")public String frames(Model model, HttpServletRequest request) {logger.trace("redirect to page frames.");String userName = SpringSecurityContext.getUsername();model.addAttribute("name", userName);SecUser user = userManager.getUserByName(userName);if (user != null) {WebUtils.setSessionAttribute(request, GlobalConstant.SESSION_USER, user);}return "/frames";}
页: [1]
查看完整版本: spring security ajax示例