当前位置:网站首页>Chapter 3 business function development (to remember account and password)
Chapter 3 business function development (to remember account and password)
2022-07-07 17:54:00 【Make a light】
customer demand : Realization 10 Remember password function within days
Remember the password :
visit :login.jsp----> backstage :.html: If you remember the password last time , Automatically fill in the account number and password ; otherwise , No .
How to judge whether you remember the password last time ?`
-- Last login succeeded , Determine whether you need to remember the password : If you need to remember the password , Then write to the browser cookie; otherwise , Delete cookie.
and cookie The value of must be the user's loginAct and loginPwd
-- The next time you log in , Judge whether the user has cookie: If there is , Then automatically fill in the account number and password ; otherwise , Don't write .
And fill in cookie Value .
-----> Browser display
obtain cookie:
1, Use java Code acquisition cookie:
Cookie[] cs=request.getCookies();
for(Cookie c:cs){
if(c.getName().equals("loginAct")){
String loginAct=c.getValue();
}else if(c.getName().equals("loginPwd")){
String loginPwd=c.getValue();
}
}
2, Use EL Expression acquisition cookie:
${cookie.loginAct.value}
${cookie.loginPwd.value}
1. The control layer determines whether the check box is selected , If selected, then isRemPwd The value of will be true, If not selected, then isRemPwd The value of is false, The duty of true Get the entered account and password and save them in cookie in , Set up cookie Time to remember the account and password within ten days , Set it up cookie Output to browser . The duty of false when , Continue to set to the same name cookie, In order to deal with what may have been produced in the front cookie To cover ,cookie Set the value of , But I have to write , No matter value Nothing written will work , Because it's time to cookie The life cycle of is 0 second , Finally, put the cookie Output to browser .

2. introduce JSTL Core library , In order to be in jsp Page using JSTL Statement to make logical judgment

3. adopt JSTL The sentence goes on cookie The judgment of existence

Here is the detailed code
involves UserController Classes and login.jsp page
UserController class
package com.it.crm.settings.web.controller;
import com.it.crm.commons.contants.Contants;
import com.it.crm.commons.entity.ReturnObject;
import com.it.crm.commons.utils.DateUtils;
import com.it.crm.settings.entity.User;
import com.it.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
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;
@RequestMapping(value = "/settings/qx/user/toLogin.do")
public String toLogin(){
return "settings/qx/user/login";
}
@RequestMapping(value = "/settings/qx/user/login.do")
@ResponseBody
public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session){
// Package parameters
Map<String,Object> map=new HashMap<>();
map.put("loginAct",loginAct);
map.put("loginPwd",loginPwd);
map.put("isRemPwd",isRemPwd);
// call service Layer method , Query the user
User user = userService.queryUserByActAndPwd(map);
// Generate response information according to query results
ReturnObject returnObject=new ReturnObject();
if (user==null){
// Login failed , Wrong user name or password
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage(" Wrong user name or password ");
}else {// Further judge whether the account number is legal
if (DateUtils.formateDateTime(new Date()).compareTo(user.getExpireTime())>0){
// Login failed , Account has expired
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage(" The account has expired ");
}else if ("0".equals(user.getLockState())){
// Login failed , The status is locked
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage(" The status is locked ");
}else if (!user.getAllowIps().contains(request.getRemoteAddr())){
// Login failed ,ip be limited to
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage("ip be limited to ");
}else{
// Login successful
returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
// hold user Save to session in
session.setAttribute(Contants.SESSION_USER,user);
// If you need to remember the password , Then write out cookie
if("true".equals(isRemPwd)){
Cookie loginAct1 = new Cookie("loginAct", user.getLoginAct());
loginAct1.setMaxAge(60*60*24*10);
response.addCookie(loginAct1);
Cookie loginPwd1 = new Cookie("loginPwd", user.getLoginPwd());
loginPwd1.setMaxAge(60*60*24*10);
response.addCookie(loginPwd1);
}else{
// Put those that have not expired cookie Delete
Cookie c1=new Cookie("loginAct","1");
c1.setMaxAge(0);
response.addCookie(c1);
Cookie c2=new Cookie("loginPwd","2");
c2.setMaxAge(0);
response.addCookie(c2);
}
}
}
return returnObject;
}
}
login.jsp page
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_1_1" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
<meta charset="UTF-8">
<base href="<%=basePath%>">
<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 () {
// Add a keyboard press event to the entire browser window
$(window).keydown(function (event) {
// If you press enter , Then submit the login request
if (event.keyCode==13){
$("#loginBtn").click();
}
})
// Add a click event to the login button
$("#loginBtn").click(function () {
// Collection parameters
var loginAct=$.trim($("#loginAct").val());
var loginPwd=$.trim($("#loginPwd").val());
var isRemPwd=$("#isRemPwd").prop("checked");
// Form validation
if (loginAct==""){
alert(" The username cannot be empty !");
return;
}if (loginPwd==""){
alert(" The password cannot be empty !");
return;
}
// After clicking login, it will show that you are verifying
$("#msg").html(" Trying to verify ...")
// Send a request
$.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"){
// Login successful , Jump to the main page of the business
window.location.href="workbench/index.do";
}else {
// Login failed , Display prompt message
$("#msg").html(data.message);
}
}
});
});
});
</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 <span style="font-size: 12px;"> Customer relationship management system </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> Sign in </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=" user name ">
</div>
<div style="width: 350px; position: relative;top: 20px;">
<input class="form-control" id="loginPwd" type="password" value="${cookie.loginPwd.value}" placeholder=" password ">
</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 and empty cookie.loginPwd}">
<input type="checkbox" id="isRemPwd">
</c:if>
Login free for ten days
</label>
<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;"> Sign in </button>
</div>
</form>
</div>
</div>
</body>
</html>test result
Enter the page for the first time and choose to remember the password , Click login

Enter the front page of the workbench normally

Close the browser , Reopen the browser and enter the login page , Automatically remembering the account and password can be achieved
http://127.0.0.1:8080/crm/settings/qx/user/toLogin.do

Click Cancel to login free within ten days , Close the browser and enter the login interface again , Realize the cancellation of login free within ten days , Enter the login interface without remembering the account and password .

边栏推荐
- 手机app外卖订餐个人中心页面
- 使用popupwindow創建对话框风格的窗口
- Functions and usage of imageswitch
- Ansible 学习总结(9)—— Ansible 循环、条件判断、触发器、处理失败等任务控制使用总结
- 本周小贴士131:特殊成员函数和`= default`
- TaffyDB开源的JS数据库
- 利用七种方法对一个文件夹里面的所有图像进行图像增强实战
- TabHOST 选项卡的功能和用法
- Automated testing: a practical skill that everyone wants to know about robot framework
- actionBar 导航栏学习
猜你喜欢

运行yolo v5-5.0版本报错找不到SPPF错误,进行解决

Cartoon | who is the first ide in the universe?

Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally

Function and usage of textswitch text switcher

什么是敏捷测试

【可信计算】第十三次课:TPM扩展授权与密钥管理

Taffydb open source JS database

本周小贴士#136:无序容器

Face recognition attendance system based on Baidu flying plasma platform (easydl)

【解惑】App处于前台,Activity就不会被回收了?
随机推荐
Deep learning machine learning various data sets summary address
青年时代历练和职业发展
JS pull down the curtain JS special effect display layer
Threshold segmentation based on RGB image and threshold adjustment by sliding
简单的loading动画
基于PyTorch利用CNN对自己的数据集进行分类
《世界粮食安全和营养状况》报告发布:2021年全球饥饿人口增至8.28亿
zdog. JS rocket turn animation JS special effects
物联网OTA技术介绍
Vscode three configuration files about C language
深度学习机器学习各种数据集汇总地址
Face recognition attendance system based on Baidu flying plasma platform (easydl)
< code random recording two brushes> linked list
2021-06-28
【网络攻防原理与技术】第4章:网络扫描技术
Alertdialog create dialog
Function and usage of numberpick
Functions and usage of ratingbar
Use onedns to perfectly solve the optimization problem of office network
Actionbar navigation bar learning