本文主要是介绍小程序后台管理系统(1)登入,登出,及拦截器讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、拦截器配置
//springmvc.xml(配置拦截器)
<mvc:mapping path="/*/**"/>
表示对所有路径都进行拦截,然后<mvc:exclude-mapping path="/static/**"/>
对静态资源文件进行开放,和springboot里面的拦截器配置是换汤不换药,而对于login
接口是不需要进行拦截的。
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/*/**"/><mvc:exclude-mapping path="/static/**"/><bean class="com.atlantis.interceptor.LoginInterceptor"><property name="unCheckUrls"><list><value>/login.action</value></list></property></bean></mvc:interceptor></mvc:interceptors>
//LoginInterceptor.class(拦截器注册类)
如果请求的路径为放行的Url,则直接放行,如果session失效,则返回登陆页面。
public class LoginInterceptor implements HandlerInterceptor {private List<String> unCheckUrls; // 需要放行的url集合public List<String> getUnCheckUrls() {return unCheckUrls;}public void setUnCheckUrls(List<String> unCheckUrls) {this.unCheckUrls = unCheckUrls;}@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {String requestUrl = request.getRequestURI();requestUrl = requestUrl.replaceAll(request.getContextPath(), "");// 判断是否针对匿名路径需要拦截,如果包含,则表示匿名路径,需要拦截,否则通过拦截器if (unCheckUrls.contains(requestUrl)) {// 包含需要放行的url,直接跳过return true;}if (null == request.getSession().getAttribute("sessionAdmin")) {response.sendRedirect(request.getContextPath() + "/login.action");return false;}// 放行return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}
二、登入
//login接口
这里写死了登陆名和密码,登陆成功后利用UUID随机生成了一个token,然后把token和用户信息封装成一个对象,放到session里面去
@PostMapping("login")
@ResponseBody
public ResultByJSON adminLogin(String username, String password, HttpServletRequest request, HttpServletResponse response) {if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {return ResultByJSON.errorMsg("用户名和密码不能为空");} else if (USERNAME.equals(username) && PASSWORD.equals(password)) {String token = UUID.randomUUID().toString();Admin admin = new Admin(token, username, password);request.getSession().setAttribute("sessionAdmin", admin);return ResultByJSON.ok();}return ResultByJSON.errorMsg("登陆失败,请重试...");}
这篇关于小程序后台管理系统(1)登入,登出,及拦截器讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!