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是否已将销毁,给前台监听事件返回一个状态,判断是否跳转到登录页面。