008 登录(index页面获取token) 同步

文章目录

    • CustomerController.java
    • Customer.java
    • CustomerMapper.java
    • ICustomerService.java
    • CustomerServiceImpl.java
    • JwtUtil.java
    • ServerResult.java
    • ServletInitializer.java
    • SpringbootDemoApplication.java
    • customer.sql
    • CustomerMapper.xml
    • application.yaml
    • index.jsp
    • login.jsp
    • pom.xml

CustomerController.java

package com.example.controller;import com.example.entity.Customer;
import com.example.service.ICustomerService;
import com.example.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;/*** <p>*  前端控制器* </p>** @author dd* @since 2024-04-16*/
@Controller
@RequestMapping("customer")
public class CustomerController {@Autowiredprivate ICustomerService customerService;@PostMapping("login")public ModelAndView login(String custName,String custPwd){ServerResult result = customerService.login(custName, custPwd);ModelAndView mav =   new ModelAndView();if(result.getCode() == 200){mav.addObject("result",result);mav.setViewName("index");}else{mav.addObject("result",result);mav.setViewName("login");}return mav;}}

Customer.java

package com.example.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;/*** <p>* * </p>** @author dd* @since 2024-04-16*/
@TableName("customer")
public class Customer implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "cust_id", type = IdType.AUTO)private Integer custId;private String custName;private Long custTelno;private String custGender;private LocalDate custBirth;/*** 状态*/private Integer status;/*** 版本号用于做乐观锁*/private Integer version;/*** 数据添加的时间*/private LocalDateTime createTime;/*** 数据修改时间*/private LocalDateTime updateTime;private String custPassword;public Integer getCustId() {return custId;}public void setCustId(Integer custId) {this.custId = custId;}public String getCustName() {return custName;}public void setCustName(String custName) {this.custName = custName;}public Long getCustTelno() {return custTelno;}public void setCustTelno(Long custTelno) {this.custTelno = custTelno;}public String getCustGender() {return custGender;}public void setCustGender(String custGender) {this.custGender = custGender;}public LocalDate getCustBirth() {return custBirth;}public void setCustBirth(LocalDate custBirth) {this.custBirth = custBirth;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public Integer getVersion() {return version;}public void setVersion(Integer version) {this.version = version;}public LocalDateTime getCreateTime() {return createTime;}public void setCreateTime(LocalDateTime createTime) {this.createTime = createTime;}public LocalDateTime getUpdateTime() {return updateTime;}public void setUpdateTime(LocalDateTime updateTime) {this.updateTime = updateTime;}public String getCustPassword() {return custPassword;}public void setCustPassword(String custPassword) {this.custPassword = custPassword;}@Overridepublic String toString() {return "Customer{" +"custId=" + custId +", custName=" + custName +", custTelno=" + custTelno +", custGender=" + custGender +", custBirth=" + custBirth +", status=" + status +", version=" + version +", createTime=" + createTime +", updateTime=" + updateTime +", custPassword=" + custPassword +"}";}
}

CustomerMapper.java

package com.example.mapper;import com.example.entity.Customer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;/*** <p>*  Mapper 接口* </p>** @author dd* @since 2024-04-16*/
public interface CustomerMapper extends BaseMapper<Customer> {}

ICustomerService.java

package com.example.service;import com.example.entity.Customer;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.util.ServerResult;/*** <p>*  服务类* </p>** @author dd* @since 2024-04-16*/
public interface ICustomerService  {public ServerResult login(String customerName,String customerPassword);}

CustomerServiceImpl.java


package com.example.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Customer;
import com.example.mapper.CustomerMapper;
import com.example.service.ICustomerService;
import com.example.util.JwtUtil;
import com.example.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** <p>*  服务实现类* </p>** @author dd* @since 2024-04-16*/
@Service
public class CustomerServiceImpl  implements ICustomerService {@Autowiredprivate CustomerMapper customerMapper;@Overridepublic ServerResult login(String customerName,String customerPassword){QueryWrapper<Customer> wrapper = new QueryWrapper<>();wrapper.eq("cust_name",customerName).eq("cust_password",customerPassword);Customer customer = customerMapper.selectOne(wrapper);if(customer != null){String token = JwtUtil.createToken(customer.getCustId(),customer.getCustName());return ServerResult.loginSuccess(token);}return ServerResult.loginFail("用户登录失败");}}

JwtUtil.java


package com.example.util;import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;import java.util.Date;
import java.util.HashMap;
import java.util.Map;public class JwtUtil {private static final String jwtToken = "dahkgag7*$";private static long expireTime = 1000*60*60*24;/*** 创建新token* @param customerId 用户id* @param custmoerName* @return*/public static String createToken(Integer customerId,String custmoerName){Map<String,Object> claims = new HashMap<>();claims.put("customerId",customerId);claims.put("customerName",custmoerName);JwtBuilder jwtBuilder = Jwts.builder().signWith(SignatureAlgorithm.HS256,jwtToken).setClaims(claims).setIssuedAt(new Date()).setExpiration(new Date(System.currentTimeMillis()+expireTime));String token = jwtBuilder.compact();return token;}//验证token是否有效/*** 验证token是否有效* @param token 客户端携带的token* @return 返回是否有效*/public static boolean checkToken(String token){return false;}//解析token/*** 解析token* @param token 客户端携带的token* @return 返回登录用户信息(customerIs)*/public static void parseToken(String token){}}

ServerResult.java


package com.example.util;public class ServerResult {private int code;private String msg;private Object data;public static ServerResult getSuccess(Object data){return new ServerResult(200,"查询成功",data);}public static ServerResult getFail(Object data){return new ServerResult(201,"查询失败",data);}/*** 添加、删除、修改的成功* @param data* @return*/public static ServerResult updateSuccess(Object data){return new ServerResult(200,"处理成功",data);}/*** 添加、删除、修改的失败* @param data* @return*/public static ServerResult updateFail(Object data){return new ServerResult(201,"处理失败",data);}public static ServerResult loginSuccess(Object data){return new ServerResult(200,"登录成功",data);}public static ServerResult loginFail(Object data){return new ServerResult(201,"登失败",data);}public ServerResult() {}public ServerResult(int code, String msg, Object data) {this.code = code;this.msg = msg;this.data = data;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}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;}@Overridepublic String toString() {return "ServerResult{" +"code=" + code +", msg='" + msg + '\'' +", data=" + data +'}';}
}

ServletInitializer.java


package com.example;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringbootDemoApplication.class);}}

SpringbootDemoApplication.java


package com.example;import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.mapper")
public class SpringbootDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDemoApplication.class, args);}}

customer.sql


/*Navicat Premium Data TransferSource Server         : mysql8_3306Source Server Type    : MySQLSource Server Version : 80029Source Host           : localhost:3306Source Schema         : empdbTarget Server Type    : MySQLTarget Server Version : 80029File Encoding         : 65001Date: 11/04/2024 09:38:17
*/SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for customer
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer`  (`cust_id` int(0) NOT NULL AUTO_INCREMENT,`cust_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`cust_telno` bigint(0) NOT NULL,`cust_gender` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`cust_birth` date NULL DEFAULT NULL,`status` int(0) NULL DEFAULT NULL COMMENT '状态',`version` int(0) NULL DEFAULT NULL COMMENT '版本号用于做乐观锁',`create_time` datetime(0) NULL DEFAULT NULL COMMENT '数据添加的时间',`update_time` datetime(0) NULL DEFAULT NULL COMMENT '数据修改时间',`cust_password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,PRIMARY KEY (`cust_id`) USING BTREE,UNIQUE INDEX `cust_telno`(`cust_telno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES (1, 'smith', 1849430033, 'M', '2000-01-01', 1, 1, '2023-08-11 13:39:30', NULL, '123456');
INSERT INTO `customer` VALUES (2, 'allen', 13771940583, 'F', '2001-05-01', 1, 1, '2023-07-31 13:40:09', NULL, '123456');SET FOREIGN_KEY_CHECKS = 1;

CustomerMapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.CustomerMapper"></mapper>

application.yaml


server:servlet:context-path: /appport: 8080spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/dict?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456mvc:view:prefix: / #前缀suffix: .jsp #后缀hiddenmethod:filter:enabled: true # 支持表单 method 转换logging:file:path: d://logger #日志记录level:com.example: debug

index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>${result.data}<script>saveToken();function  saveToken(){console.log(typeof "${result.data}")localStorage.setItem("token","${result.data}")}
</script>
</body>
</html>

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><form method="post" action="${pageContext.request.contextPath}/customer/login">用户名: <input type="text" name="custName">密码: <input type="password" name="custPwd"><input type="submit" value="登录"></form></body>
</html>

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>springboot_demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>springboot_demo</name><description>springboot_demo</description><properties><java.version>1.8</java.version></properties><dependencies><!--====================JWT===============================--><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.9</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.1</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generate --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/822200.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

no_good_file(训练题)

[题目信息]&#xff1a; 题目名称题目难度no_good_file&#xff08;训练题&#xff09;2 [题目考点]&#xff1a; 远程命令执行漏洞&#xff0c;用户通过浏览器提交执行命令&#xff0c;由于服务器端没有针对执行函数做过滤&#xff0c;导致在没有指定绝对路径的情况下就执行…

读《SQL基础教程 第二版 上》的一些总结

1. 数据库语言 DDL: Data Definition Language&#xff0c;数据定义语言&#xff08;库、表的操作&#xff09; DML: Data Manipulation Language&#xff0c; 数据操控语言&#xff08;对表中数据的增删改&#xff09; DQL: Data Query Language&#xff0c;数据库查询语言…

Proxyman Premium for Mac v5.1.1激活版:卓越的网络调试与分析工具

Proxyman Premium for Mac是一款功能强大的网络调试与分析工具&#xff0c;专为开发人员和测试人员精心打造。它集多种功能于一身&#xff0c;为用户提供了全面、高效的网络开发体验。 Proxyman Premium for Mac v5.1.1激活版下载 作为一款跨平台代理工具&#xff0c;Proxyman …

蓝桥杯第859题——旅行

Z 小镇是一个景色宜人的地方&#xff0c;吸引来自各地的观光客来此旅游观光。Z 小镇附近共有 n 个景点&#xff08;编号为 1,2,3,…,n&#xff09;&#xff0c;这些景点被 m 条道路连接着&#xff0c;所有道路都是双向的&#xff0c;两个景点之间可能有多条道路。 也许是为了保…

cesium primitive 移动 缩放 旋转 矩阵

旋转参考&#xff1a;cesium 指定点旋转rectangle Primitive方式 矩阵篇-CSDN博客 平移参考&#xff1a;cesium 调整3dtiles的位置 世界坐标下 相对坐标下 平移矩阵-CSDN博客 一、primitive方式添加polygon let polygonInstance new Cesium.GeometryInstance({geometry: Ce…

逻辑回归模型-逻辑回归算法原理-逻辑回归代码与实现-笔记整合

通过一段时间的学习&#xff0c;总算把逻辑回归模型弄清楚了 《老饼讲解-机器学习》www.bbbdata.com中是讲得最清晰的&#xff0c;结合其它资料&#xff0c;整理一个完整的笔记如下&#xff0c;希望能帮助像我这样入门的新人&#xff0c;快速理解逻辑回归算法原理和逻辑回归实…

Adobe AE(After Effects)2023下载地址及安装教程

Adobe After Effects是一款专业级别的视觉效果和动态图形处理软件&#xff0c;由Adobe Systems开发。它被广泛用于电影、电视节目、广告和其他多媒体项目的制作。 After Effects提供了强大的合成和特效功能&#xff0c;可以让用户创建出令人惊艳的动态图形和视觉效果。用户可以…

06节-51单片机-LCD1602调试工具

欢迎订阅专栏&#xff0c;持续为您更新&#xff01; 1.LCD1602调试工具 使用LCD1602液晶屏作为调试窗口&#xff0c;提供类似printf函数的功能&#xff0c;可实时观察单片机内部数据的变换情况&#xff0c;便于调试和演示。 本文提供的LCD1602代码属于模块化的代码&#xff…

java创建线程池的方法

简介 线程池是一种用于管理和重用线程的机制&#xff0c;它可以有效地管理线程的创建和销毁&#xff0c;减少线程创建和销毁的开销&#xff0c;并且能够控制并发线程数量&#xff0c;避免资源耗尽和系统过载。Java 提供了java.util.concurrent 包来支持线程池的实现。 1.Threa…

蓝桥杯刷题-乌龟棋

312. 乌龟棋 - AcWing题库 /* 状态表示&#xff1a;f[b1,b2,b3,b4]表示所有第 i种卡片使用了 bi张的走法的最大分值。状态计算&#xff1a;将 f[b1,b2,b3,b4]表示的所有走法按最后一步选择哪张卡片分成四类&#xff1a;第 i类为最后一步选择第 i种卡片。比如 i2&#xff0c;则…

基于OKHttp的大文件下载

1.需求&#xff1a; 根据Url下载大文件&#xff0c;本地创建文件&#xff0c;从输入流读取并写入文件&#xff0c;要求能满足大文件的下载&#xff0c;不能出现OOM 2.引入依赖 <!-- OK HTTP --> <dependency><groupId>com.squareup.okhttp3</groupId>…

Selenium(二):WebDriver核心方法和属性

一、WebDriver核心属性 1.river.name 浏览器名称 2.driver.current_url 获取当前URL 3.driver.title 获得当前页面标题 4.driver.page_source 获得当前页面源码 5.driver.current_window_handle 获得当前窗口句柄 6.driver.window_handles 获得当前所有窗口句柄 二、Web…

【学习】jemter中如何高效使用正则表达式

在Jemter的世界里&#xff0c;正则表达式无疑是一把锐利的剑&#xff0c;它可以帮助我们轻松地解决许多问题。在Jemter的性能测试过程中&#xff0c;我们常常需要提取响应中的某些数据&#xff0c;以便在后续的请求中使用。这时&#xff0c;正则表达式就派上用场了。通过学习如…

Java并发体系--LinkedTransferQueue

LinkedTransferQueue LinkedTransferQueue是Java并发包中的一个类&#xff0c;它实现了BlockingQueue接口。 LinkedTransferQueue是一个基于链表的无界阻塞队列&#xff0c;它支持先进先出的顺序&#xff0c;并且具有高并发性能。它的主要特点是能够在生产者线程和消费者…

【web开发网页制作】html+css家乡长沙旅游网页制作(4页面附源码)

家乡长沙网页制作 涉及知识写在前面一、网页主题二、网页效果Page1、主页Page2、历史长沙Page3、著名人物Page4、留言区 三、网页架构与技术3.1 脑海构思3.2 整体布局3.3 技术说明书 四、网页源码HtmlCSS 五、源码获取5.1 获取方式 作者寄语 涉及知识 家乡长沙网页制作&#x…

Linux下的IP地址与主机名

IP和主机名 IP地址和主机名 什么是IP地址 IP地址 每一台联网的电脑都会有一个地址&#xff0c;用于和其它计算机进行通讯 IP地址主要有2个版本&#xff0c;V4版本和V6版本&#xff08;V6很少用&#xff0c;课程暂不涉及&#xff09; IPv4版本的地址格式是&#xff1a;a.b…

【AIGC】AIGC在虚拟数字人中的应用:塑造未来互动体验的革新力量

&#x1f680; &#x1f680; &#x1f680;随着科技的快速发展&#xff0c;AIGC已经成为引领未来的重要力量。其中&#xff0c;AIGC在虚拟数字人领域的应用更是引起了广泛关注。虚拟数字人作为一种先进的数字化表达形式&#xff0c;结合了3D建模、动画技术、人工智能等多种先进…

MySQL-锁篇

文章目录 表级锁和行级锁了解吗&#xff1f;有什么区别&#xff1f;行级锁使用有什么注意事项&#xff1f;InnoDB有哪几类行锁&#xff1f;共享锁和排他锁是什么&#xff1f;意向锁有什么用&#xff1f; 锁是一种常见的并发事务的控制方式 表级锁和行级锁了解吗&#xff1f;有什…

鸿蒙HarmonyOS开发规范-完善中

代码规范 所有文件&#xff0c;包括自动生成的编译文件package.json都要格式化&#xff08;IDE快捷键CtrlAltL&#xff09;&#xff1b;函数命名&#xff0c;C大驼峰&#xff0c;TS、JS小驼峰&#xff0c;函数命名注意动宾结构&#xff1b;静态常量需使用全大写&#xff0c;文…

如何提取音频中的封面

大家在播放MP3音乐的时候经常可以在播放器上看到这个音乐的封面&#xff0c;那么我们要如何提取MP3的音乐里面的封面呢&#xff1f; 打开智游剪辑&#xff08;官网: zyjj.cc&#xff09;&#xff0c;搜索音乐封面提取 然后就只需要拖入MP3文件点击提交就可以了 下面这里简单展示…