本文主要是介绍SSM实现登陆登出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一步:
在springmvc.xml配置文件里面配置一个拦截器的配置
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/*/**"/><mvc:exclude-mapping path="/static/**"/> <bean class="com.lpy.interceptor.LoginInterceptor"><property name="unCheckUrls"> <list> <value>/users/login.action</value></list> </property> </bean></mvc:interceptor></mvc:interceptors>
表示拦截所有路径,除了静态资源的路径
配置了一个拦截器LoginInterceptor,/users/login.action表示不进行拦截
第二步:
书写一个拦截器的类 并实现 HandlerInterceptor
package com.imooc.interceptor;import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;public class LoginInterceptor implements HandlerInterceptor {private List<String> unCheckUrls;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String requestUrl = request.getRequestURI();requestUrl=requestUrl.replaceAll(request.getContextPath(), "");// 判断是否针对匿名路径需要拦截,如果包含,则表示匿名路径,需要拦截,否则通过拦截器if (unCheckUrls.contains(requestUrl)){// 包含公开url,直接跳过return true;}if(null == request.getSession().getAttribute("sessionUser")){response.sendRedirect(request.getContextPath()+"/users/login.action"); return false;}// 放行return true;}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {// TODO Auto-generated method stub}@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {// TODO Auto-generated method stub}public List<String> getUnCheckUrls() {return unCheckUrls;}public void setUnCheckUrls(List<String> unCheckUrls) {this.unCheckUrls = unCheckUrls;}}
第三步:书写usersController
package com.imooc.controller;import java.util.UUID;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;import com.imooc.bean.AdminUser;
import com.imooc.pojo.Users;
import com.imooc.service.UsersService;
import com.imooc.utils.IMoocJSONResult;
import com.imooc.utils.PagedResult;@Controller
@RequestMapping("users")
public class UsersController {@Autowiredprivate UsersService usersService;@GetMapping("/showList")public String showList() {return "users/usersList";}@PostMapping("/list")@ResponseBodypublic PagedResult list(Users user , Integer page) {PagedResult result = usersService.queryUsers(user, page == null ? 1 : page, 10);return result;}@GetMapping("/login")public String login() {return "login";}@PostMapping("login")@ResponseBodypublic IMoocJSONResult userLogin(String username, String password,HttpServletRequest request, HttpServletResponse response) {// TODO 模拟登陆if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {return IMoocJSONResult.errorMap("用户名和密码不能为空");} else if (username.equals("lee") && password.equals("lee")) {String token = UUID.randomUUID().toString();AdminUser user = new AdminUser(username, password, token);request.getSession().setAttribute("sessionUser", user);return IMoocJSONResult.ok();}return IMoocJSONResult.errorMsg("登陆失败,请重试...");}@GetMapping("/logout")public String logout(HttpServletRequest request, HttpServletResponse response) {request.getSession().removeAttribute("sessionUser");return "login";}}
书写UsersController涉及到2个帮助类
第一个:获取的数据封装后返回前端的
package com.imooc.utils;/*** @Description: 自定义响应数据结构* 这个类是提供给门户,ios,安卓,微信商城用的* 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list)* 其他自行处理* 200:表示成功* 500:表示错误,错误信息在msg字段中* 501:bean验证错误,不管多少个错误都以map形式返回* 502:拦截器拦截到用户token出错* 555:异常抛出信息*/
public class IMoocJSONResult {// 响应业务状态private Integer status;// 响应消息private String msg;// 响应中的数据private Object data;private String ok; // 不使用public static IMoocJSONResult build(Integer status, String msg, Object data) {return new IMoocJSONResult(status, msg, data);}public static IMoocJSONResult ok(Object data) {return new IMoocJSONResult(data);}public static IMoocJSONResult ok() {return new IMoocJSONResult(null);}public static IMoocJSONResult errorMsg(String msg) {return new IMoocJSONResult(500, msg, null);}public static IMoocJSONResult errorMap(Object data) {return new IMoocJSONResult(501, "error", data);}public static IMoocJSONResult errorTokenMsg(String msg) {return new IMoocJSONResult(502, msg, null);}public static IMoocJSONResult errorException(String msg) {return new IMoocJSONResult(555, msg, null);}public IMoocJSONResult() {}public IMoocJSONResult(Integer status, String msg, Object data) {this.status = status;this.msg = msg;this.data = data;}public IMoocJSONResult(Object data) {this.status = 200;this.msg = "OK";this.data = data;}public Boolean isOK() {return this.status == 200;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public String getOk() {return ok;}public void setOk(String ok) {this.ok = ok;}}
第二个:分页的帮助类
package com.lpy.utils;import java.util.List;/*** @Description: 封装分页后的数据格式*/
public class PagedResult {private int page; // 当前页数private int total; // 总页数 private long records; // 总记录数private List<?> rows; // 每行显示的内容public int getPage() {return page;}public void setPage(int page) {this.page = page;}public int getTotal() {return total;}public void setTotal(int total) {this.total = total;}public long getRecords() {return records;}public void setRecords(long records) {this.records = records;}public List<?> getRows() {return rows;}public void setRows(List<?> rows) {this.rows = rows;}}
这篇关于SSM实现登陆登出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!