實現頁面登入後仍然跳回當前頁面
1 頁面部分
<script type="text/javascript" src="<c:url value = '/resources/thirdparty/jquery.base64.js'/>"></script>
var ctx = "${pageContext.request.contextPath}";
function downloadOpt(item){
var param = {};
param.revitId = $(item).data("id");
param.toUrl = $.base64.encode(location.pathname + "?revitAddInId=" + $(item).data("id"));
$.post(ctx + "/revit/getDownloadPluginLink.html", param, function(data) {
if(data.success){
location.href = data.data;
}else{
var errorMessage = data.message.split(":")[0];
error(errorMessage);
if (errorMessage.indexOf("登入") != -1) {
location = "${pageContext.request.contextPath}" + "/loginPersonal.html";
}
}
});
}
2.攔截器實現
//下載時候要登入
public class RevitInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(RevitInterceptor.class);
private @Autowired RequirementService requirementService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String shortUri = ServletUtils.getShortUri(request);
OnlineUser user = SessionHelper.getOnlineUser(request.getSession());
HttpSession session = request.getSession();
String toUrl = null;
if (shortUri.contains("list.html") || shortUri.contains("details.html")||shortUri.contains("authorInfo.html")
|| shortUri.contains("getPoolMoney.html")|| shortUri.contains("getPersonMoney.html")||shortUri.contains("getBetResult.html")) {
//Long reqId = null;
try {
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return true;
}
if (null == user) {
toUrl = request.getParameter("toUrl");//獲得登陸後回跳地址
if (isAsyncRequest(request)) {
if (toUrl != null) {
session.setAttribute("toUrl", new String(Base64.decodeBase64(toUrl)));//回跳地址寫入session
}
response.setContentType("application/json");
Writer.write(response, Ajax.buildErrorResult("您沒有登入,請先登入!"));
} else {
if (toUrl != null) {
session.setAttribute("toUrl", toUrl);
} else {
session.setAttribute("toUrl", request.getRequestURI() + "?" + request.getQueryString());
}
response.sendRedirect(request.getContextPath() + RedirectURL.LOGIN_PERSONAL);
}
return false;
}
return true;
}
private boolean isAsyncRequest(HttpServletRequest request) {
return request.getHeader("X-Requested-With") != null;
}
}
3.先跳到登入控制器
@RequestMapping(value = "loginPersonal")
public ModelAndView loginDesign(HttpSession session) {
ModelAndView view = new ModelAndView();
String toUrl = (String) session.getAttribute("toUrl");
view.addObject("toUrl", toUrl); //再把url 放到頁面 hidden
view.setViewName("/user/loginDesign");
return view;
}
4
<div class="revitbus-init-page-box">
<div id="personal"class="revitbus-init-page-box-personal personal-enterprise ">
<input type="hidden" name="toUrl" value="${toUrl}">
<input type="text" name="username" class="form-control" placeholder="使用者名稱/手機號" onkeydown="onInputFinishEnter(event);">
<input type="password" name="password" class="form-control" placeholder="密碼" onkeydown="onInputFinishEnter(event);">
</div>
<div>
<input type="text" class="form-control" name="code" placeholder="請輸入右側公式結果" style="width:170px;margin-right:20px;" onkeydown="onInputFinishEnter(event);">
<img class="revitbus-login-page-code" alt="" src="<c:url value='/code.html' />">
</div>
<a class="rvt-login-design" onclick="onLogin();"><button>登入</button></a>
<!-- <input name="Fruit" type="checkbox" value="" style="margin: 0 10px;" />記住密碼 -->
<div class="revitbus-init-page-box-registered">
<div class="pull-left">
<a href="<c:url value='/forgetPwdPersonal.html' />">忘記密碼</a>
</div>
<div class="pull-right">
<a href="<c:url value='/registerPersonal.html' />">註冊</a>
</div>
<div class="clearfix"></div>
</div>
</div>
js登入 跳轉tourl
function onLogin(){
var code = $("input[name='code']").val();
var pwd = $("input[name='password']").val();
var userName = $("input[name='username']").val();
if(userName.length == 0){
info("請輸入賬號");
return;
}
if(pwd.length == 0){
info("請輸入密碼");
return;
}
if(code.length == 0){
info("請輸入驗證碼");
return;
}
var url = '/revitbus/login/personal.html';
var param = {};
param['username'] = userName;
param['password'] = hex_md5(pwd);
param['code'] = code;
var toUrl = $("input[name='toUrl']").val();
var forward = "/revitbus/welcome.html";
if(!(toUrl == "")){
if(toUrl != forward){
forward = toUrl;
}
}
$.post(url,param,function(data) {
if (data.success) {
window.location.href = forward;
} else {
console.log(data);
error(data.message);
}
});
}
另外一種方法 前2步都一樣
再加上以下內容
在加一個登入的攔截器 :類似於第二步
攔截下 tourl 屬性 有的話 跳轉 下面大致寫下 沒有驗證 做一個重定向
在類似以下方法中
- /**
- * 在業務處理器處理請求執行完成後,生成檢視之前執行的動作
- * 可在modelAndView中加入資料,比如當前時間
- */
- @Override
- publicvoid postHandle(HttpServletRequest request,
- HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception {
- log.info("==============執行順序: 2、postHandle================");
- if(modelAndView != null){ //加入當前時間
- modelAndView.addObject("var", "測試postHandle");
- }
- }
加入以下內容
if (null == user) {
toUrl = request.getParameter("toUrl");
if (isAsyncRequest(request)) {
if (toUrl != null) {
session.setAttribute("toUrl", new String(Base64.decodeBase64(toUrl)));
}
response.setContentType("application/json");
response.sendRedirect(toUrl); //可以試試 應該可以 大致思路這樣
} else {
.................
}
return false;
}