session过期返回登录页面

本文介绍了一个基于Spring框架的登录控制器实现,详细解析了登录验证、Session管理及超时处理等核心功能,展示了如何通过Java和JSON进行用户认证和会话维护。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.moofen.cube.controller.ume.login;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moofen.core.constant.AuthConstant;
import com.moofen.core.constant.SessionConstant;
import com.moofen.core.entity.sys.um.RoleBase;
import com.moofen.core.mvc.controller.BaseController;
import com.moofen.core.mvc.view.BaseResult;
import com.moofen.cube.service.ume.login.LoginService;

@Controller
@RequestMapping("/user")
public class LoginController extends BaseController {

	@Resource(name = "loginService")
	private LoginService loginService;

	@ResponseBody
	@PostMapping("/login")
	public JSONObject login(@RequestParam(name = "loginName", required = true) String loginName,
			@RequestParam(name = "password", required = true) String password) {
		JSONObject result = loginService.login(loginName, password);
		BaseResult baseResult = JSON.parseObject(result.toJSONString(), BaseResult.class);
		// session中存储账号
		if (baseResult.isSuccess()) {
			// 设定Session变量
			JSONObject data = result.getJSONObject("data");
			// 当前身份
			RoleBase roldBase = JSON.parseObject(data.getString(SessionConstant.CURR_USER_ROLE), RoleBase.class);
			if (roldBase != null) {
				// 当前用户
				getRequest().getSession().setAttribute(SessionConstant.USER_CODE, data.get(SessionConstant.USER_CODE));
				// 当前角色
				getRequest().getSession().setAttribute(SessionConstant.CURR_USER_ROLE,
						data.get(SessionConstant.CURR_USER_ROLE));
				// 当前系统
				getRequest().getSession().setAttribute(AuthConstant.SYS_CODE_CUBE, AuthConstant.SYS_CODE_CUBE);

			}
		}
		return result;
	}

	/**
	 * 退出系统
	 * 
	 * @param session
	 *            Session
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value = "/logout")
	public String logout(HttpSession session) throws Exception {
		// 清除Session
		session.invalidate();
		return "redirect:../sign_in1.html";
	}
	
	
	@ResponseBody
	@GetMapping("/timeout")
	public JSONObject timeout(HttpSession session) throws Exception {
		
		JSONObject obj = new JSONObject();
		
		String userCode = (String) session.getAttribute(SessionConstant.USER_CODE);
		
		obj.put("data", userCode);
		
		obj.put("code", 0);
		
		obj.put("message", "长时间未操作,身份已过期,请重新登录!");
		
		return obj;
	}
	
	
	
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>Moofen Cube Web</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring/applicationContext*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
	</listener>

	<filter>
		<filter-name>log4jServletFilter</filter-name>
		<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>log4jServletFilter</filter-name>
		<url-pattern>/</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>ERROR</dispatcher>
	</filter-mapping>

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>sessionFilter</filter-name>
		<filter-class>com.moofen.cube.controller.ume.login.SessionFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>sessionFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>login.html</welcome-file>
	</welcome-file-list>

	<error-page>
		<location>/error</location>
	</error-page>

</web-app>

 

 

package com.moofen.cube.controller.ume.login;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import com.moofen.core.constant.SessionConstant;

public class SessionFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {

		// 不过滤的uri
		String[] notFilter = new String[] { "login", "sign_in1", "cube_resetpw", "timeout", "frameworks", "assets" };

		// 请求的uri
		String uri = request.getRequestURI();

		// 是否过滤
		boolean doFilter = true;
		for (String s : notFilter) {
			if (uri.indexOf(s) != -1) {
				// 如果uri中包含不过滤的uri,则不进行过滤
				doFilter = false;
				break;
			}
		}
		if (doFilter) {
			// 执行过滤
			// 从session中获取登录者实体
			Object obj = request.getSession().getAttribute(SessionConstant.USER_CODE);
			if (null == obj) {
				String loginUrl = request.getContextPath() + "/sign_in1.html";
				response.sendRedirect(loginUrl);
			} else {
				// 如果session中存在登录者实体,则继续
				filterChain.doFilter(request, response);
			}
		} else {
			// 如果不执行过滤,则继续
			filterChain.doFilter(request, response);
		}
	}

}

 

/*监听session值有无,如无,则跳转到登录页面*/
window.addEventListener("click", function(){
	var datas = {};
	$.ajax({
		type : "get",
		async : false,
		url : "../user/timeout",
		data : datas,
		success : function(s) {
			if(s.data == null){
				window.location.href="../sign_in1.html";
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			datas = XMLHttpRequest.data;
			console.error("XMLHttpRequest:", XMLHttpRequest);
			console.error("textStatus:", textStatus);
			console.error("errorThrown:", errorThrown);
		}
	});
});

 后台判断session是否已将销毁,给前台监听事件返回一个状态,判断是否跳转到登录页面。

 

 

 

在ASP.NET Core MVC结合layui的情况下,当用户会话过期并尝试访问受保护的页面时,由于浏览器可能会将子窗口或iframe的内容缓存,所以登录页会被错误地显示在这些嵌套的元素中。要解决这个问题,可以采取以下几个步骤: 1. **避免自动重定向到子窗口或iframe**: 在控制器处理会话失效时,直接设置响应头`Location`指向登录页面,而不是使用`Redirect()`方法,因为这会避免自动打开一个新的导航。 ```csharp [Authorize] public IActionResult SomeProtectedAction() { if (User.Identity.IsAuthenticated == false) { HttpContext.Response.Redirect("/Login", true); // 避免 Redirect()自动导航 } return View(); } ``` 2. **清除嵌套窗口的缓存**: 使用JavaScript,当你从服务器收到会话过期的响应时,强制关闭当前的子窗口或iframe,然后跳转到登录页面。 ```javascript window.onbeforeunload = function () { // 清除iframe内容 document.getElementById('myIframe').src = ''; }; // 当接收到登录失败通知时,强制刷新登录页面 document.addEventListener("DOMContentLoaded", function () { window.location.href = "/Login"; }); ``` 3. **优化布局**: 如果可能的话,设计登录过程时,不要让用户的会话状态影响整个应用的布局,而是通过统一的入口页(如App首页)处理登录验证,这样无论在哪个子页面登录过期,都只会跳转到统一的登录界面。 总之,关键在于控制页面跳转的行为,并且在前端处理好登录状态的变化,确保每次需要认证的地方都能正确引导用户到登录页面。同时,考虑到安全性,最好还是推荐用户在新标签页中完成登录操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值