首页 > 其他分享 > 第三章. 业务功能开发--用户登录安全退出

第三章. 业务功能开发--用户登录安全退出

时间:2022-11-22 23:33:07浏览次数:42  
标签:第三章 登录 -- returnObject Cookie user loginAct import

第三章. 业务功能开发--用户登录安全退出

1. 用户登录

需求:

用户在登录页面,输入用户名和密码,点击"登录"按钮或者回车,完成用户登录的功能.
	*用户名和密码不能为空
	*用户名或者密码错误,用户已过期,用户状态被锁定,ip受限 都不能登录成功
	*登录成功之后,所有业务页面显示当前用户的名称
	*实现10天记住密码
	*登录成功之后,跳转到业务主页面
	*登录失败,页面不跳转,提示信息
  1. 编写UserController

    通过前端传入的变量,读取数据库返回user对象,通过对象中的信息对上述的条件做出判断并对上述需求创合适的session

    然后将返回的信息放到object对象中返回出去,然后通过login.jsp中的信息做出判断,是否跳转到业务的控制台;

    package com.bjpowernode.crm.settings.web.controller;
    
    import com.bjpowernode.crm.commons.contants.Contants;
    import com.bjpowernode.crm.commons.domain.ReturnObject;
    import com.bjpowernode.crm.commons.utils.DateUtils;
    import com.bjpowernode.crm.settings.domain.User;
    import com.bjpowernode.crm.settings.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @Controller
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        /**
         * url要和controller方法处理完请求之后,响应信息返回的页面的资源目录保持一致
         */
        @RequestMapping("/settings/qx/user/toLogin.do")
        public String toLogin(){
            //请求转发到登录页面
            return "settings/qx/user/login";
        }
    
        @RequestMapping("/settings/qx/user/login.do")
        public @ResponseBody Object login(String loginAct, String loginPwd, String isRemPwd,
                                          HttpServletRequest request, HttpServletResponse response, HttpSession session){
            //封装参数
            Map<String,Object> map=new HashMap<>();
            map.put("loginAct",loginAct);
            map.put("loginPwd",loginPwd);
            //调用service层方法,查询用户
            User user=userService.queryUserByLoginActAndPwd(map);
    
            //根据查询结果,生成响应信息
            ReturnObject returnObject=new ReturnObject();
            if(user==null){
                //登录失败,用户名或者密码错误
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("用户名或者密码错误");
            }else{//进一步判断账号是否合法
                //user.getExpireTime()   //2019-10-20
                //        new Date()     //2020-09-10
                if(DateUtils.formateDateTime(new Date()).compareTo(user.getExpireTime())>0){
                    //登录失败,账号已过期
                    returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                    returnObject.setMessage("账号已过期");
                }else if("0".equals(user.getLockState())){
                    //登录失败,状态被锁定
                    returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                    returnObject.setMessage("状态被锁定");
                }else if(!user.getAllowIps().contains(request.getRemoteAddr())){
                    //登录失败,ip受限
                    returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                    returnObject.setMessage("ip受限");
                }else{
                    //登录成功
                    returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
    
                    //把user保存到session中
                    session.setAttribute(Contants.SESSION_USER,user);
    
                    //如果需要记住密码,则往外写cookie
                    if("true".equals(isRemPwd)){
                        Cookie c1=new Cookie("loginAct",user.getLoginAct());
                        c1.setMaxAge(10*24*60*60);
                        response.addCookie(c1);
                        Cookie c2=new Cookie("loginPwd",user.getLoginPwd());
                        c2.setMaxAge(10*24*60*60);
                        response.addCookie(c2);
                    }else{
                        //把没有过期cookie删除
                        Cookie c1=new Cookie("loginAct","1");
                        c1.setMaxAge(0);
                        response.addCookie(c1);
                        Cookie c2=new Cookie("loginPwd","1");
                        c2.setMaxAge(0);
                        response.addCookie(c2);
                    }
                }
            }
    
            return returnObject;
        }
    
        @RequestMapping("/settings/qx/user/logout.do")
        public String logout(HttpServletResponse response,HttpSession session){
            //清空cookie
            Cookie c1=new Cookie("loginAct","1");
            c1.setMaxAge(0);
            response.addCookie(c1);
            Cookie c2=new Cookie("loginPwd","1");
            c2.setMaxAge(0);
            response.addCookie(c2);
            //销毁session
            session.invalidate();
            //跳转到首页
            return "redirect:/";//借助springmvc来重定向,response.sendRedirect("/crm/");
        }
    }
    
    
  2. 其中我的数据库中的user信息如下,一直报错,检查页面的返回数据得出,我的数据库密码和数据名称错了,下次注意。

  3. 在index.jsp 中更具返回的信息做出判断:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%
    	String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    %>
    <html>
    <head>
    	<base href="<%=basePath%>">
    <meta charset="UTF-8">
    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript">
    	$(function () {
    		//给整个浏览器窗口添加键盘按下事件
    		$(window).keydown(function (e) {
    			//如果按的是回车键,则提交登录请求
    			if(e.keyCode==13){
    				$("#loginBtn").click();
    			}
    		});
    
    		//给"登录"按钮添加单击事件
    		$("#loginBtn").click(function () {
    			//收集参数
    			var loginAct=$.trim($("#loginAct").val());
    			var loginPwd=$.trim($("#loginPwd").val());
    			var isRemPwd=$("#isRemPwd").prop("checked");
    			//表单验证
    			if(loginAct==""){
    				alert("用户名不能为空");
    				return;
    			}
    			if(loginPwd==""){
    				alert("密码不能为空");
    				return;
    			}
    
    			//显示正在验证
    			$("#msg").text("正在努力验证....");
    			//发送请求
    			$.ajax({
    				url:'settings/qx/user/login.do',
    				data:{
    					loginAct:loginAct,
    					loginPwd:loginPwd,
    					isRemPwd:isRemPwd
    				},
    				type:'post',
    				dataType:'json',
    				success:function (data) {
    					if(data.code=="1"){
    						//跳转到业务主页面
    						window.location.href="workbench/index.do";
    					}else{
    						//提示信息
    						$("#msg").text(data.message);
    					}
    				   },
    				beforeSend:function () {//当ajax向后台发送请求之前,会自动执行本函数;
    					                    //该函数的返回值能够决定ajax是否真正向后台发送请求:
    									    //如果该函数返回true,则ajax会真正向后台发送请求;否则,如果该函数返回false,则ajax放弃向后台发送请求。
    					$("#msg").text("正在努力验证....");
    					return true;
    				}
    			});
    		});
    	});
    </script>
    </head>
    <body>
    	<div style="position: absolute; top: 0px; left: 0px; width: 60%;">
    		<img src="image/IMG_7114.JPG" style="width: 100%; height: 90%; position: relative; top: 50px;">
    	</div>
    	<div id="top" style="height: 50px; background-color: #3C3C3C; width: 100%;">
    		<div style="position: absolute; top: 5px; left: 0px; font-size: 30px; font-weight: 400; color: white; font-family: 'times new roman'">CRM &nbsp;<span style="font-size: 12px;">&copy;2019&nbsp;动力节点</span></div>
    	</div>
    	
    	<div style="position: absolute; top: 120px; right: 100px;width:450px;height:400px;border:1px solid #D5D5D5">
    		<div style="position: absolute; top: 0px; right: 60px;">
    			<div class="page-header">
    				<h1>登录</h1>
    			</div>
    			<form action="workbench/index.html" class="form-horizontal" role="form">
    				<div class="form-group form-group-lg">
    					<div style="width: 350px;">
    						<input class="form-control" id="loginAct" type="text" value="${cookie.loginAct.value}" placeholder="用户名">
    					</div>
    					<div style="width: 350px; position: relative;top: 20px;">
    						<input class="form-control" id="loginPwd" type="password" value="${cookie.loginPwd.value}" placeholder="密码">
    					</div>
    					<div class="checkbox"  style="position: relative;top: 30px; left: 10px;">
    						<label>
    							<c:if test="${not empty cookie.loginAct and not empty cookie.loginPwd}">
    								<input type="checkbox" id="isRemPwd" checked>
    							</c:if>
    							<c:if test="${empty cookie.loginAct or empty cookie.loginPwd}">
    								<input type="checkbox" id="isRemPwd">
    							</c:if>
    							 十天内免登录
    						</label>
    						&nbsp;&nbsp;
    						<span id="msg" style="color: red"></span>
    					</div>
    					<button type="button" id="loginBtn" class="btn btn-primary btn-lg btn-block"  style="width: 350px; position: relative;top: 45px;">登录</button>
    				</div>
    			</form>
    		</div>
    	</div>
    </body>
    </html>
    

2安全退出

需求如下:

用户在任意的业务页面,点击"退出"按钮,弹出确认退出的模态窗口;用户在确认退出的模态窗口,点击"确定"按钮,完成安全退出的功能.
	*安全退出,清空cookie,销毁session
	*退出完成之后,跳转到首页

标签:第三章,登录,--,returnObject,Cookie,user,loginAct,import
From: https://www.cnblogs.com/atao-BigData/p/16916897.html

相关文章

  • golang算法—— 使用两个栈实现一个队列
    前言阅读本文,假定已经了解了基本数据结构概念。队列:先入先出。栈:先进后出。分析使用两个栈串联,可以实现先进先出。但是,得注意以下两点:队列在入列时,stack2必须为空,stac......
  • 初步尝试了一下gitea,果然很好安装很好用~
    官网https://gitea.io/如何本地部署和使用依赖postgres/mysql任选其一,远程服务器也可以,用来存放仓库文件。golang编译最新版源码必备环境,旧release可以直接去官网找以上!......
  • nginx 部署前端项目
    前言nginx如何部署打包成为dist的前端项目。读者须知:nginx如何安装与使用PSdocker安装与使用​​​dockerpullnginx​​​​dockerrun--restart=on-failure:20-itd......
  • golang grpc使用示例
    疑问写前面grpc有内部对心跳的处理吗,还是说,双工需要自己作心跳管理,有懂的留言一下。SEO优化grpc如何双工通信?grpc如何从服务端推送消息给客户端?gprc环境如何搭建?grpc......
  • memcached 使用与go客户端调用
    介绍详细的介绍参考:​​​https://www.runoob.com/memcached/memcached-tutorial.html​​由于memcached仅仅是一个高度内存的kv存储块,如果有更严谨的需求,需要如下选择:集......
  • 消息队列中间件nsq安装与使用
    安装与运行nsq的镜像开启容器时并不是默认开启三个服务的,而是需要手动开启。dockerpullnsqio/nsqdockerrun-itd--restart=on-failure:20-p4150:4150-p4151:4151-p......
  • 打印菱形
    #include<stdio.h>#include<math.h>intmain(){ intline=0; inti=0; scanf("%d",&line);  for(i=0;i<line;i++) { intj=0; for(j=0;j<line-1-i;j++)......
  • 03.大促抗住零点洪峰-缓存架构体系(3) 解决缓存雪崩,击穿,布隆过滤器,缓存一致性
                                                         ......
  • 3.1 首页功能的开发-跳转到首页
    第三章.业务功能开发项目结构如下:3.1首页功能的开发用户访问项目首页,首先进入登录页面。用户登录流程图:我们先写跳转到登录界面,在请求准发到index中如下图所......
  • Codeforces Round #835 (Div4)
    A.MediumNumber题意:输入三个不同的数字,输出中位数思路:没啥说的,比较一下大小即可代码:<bits/stdc++.h>usingnamespacestd;intmain(){int_;cin>>......