1.环境搭建
1.创建项目
?
2.编辑pom.xml文件
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.1</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis依赖包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--jdbc依赖包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--支持热部署的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--引入插件lombok 自动的set/get/构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build><!--测试打包 发布-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<mainClass>com.jt.SpringbootDemo3mybatisApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
?3.复制之前项目src目录
?4.统一端口号
?5.导入前端项目
?
6.如何访问的登录页面
如何实现的????用户发起请求http://localhost:8080(根路径)?
?最终看到html登录页面(已被Vue渲染完成的页面)
7.关于axios
?
?
?子组件使用的格式:this.$http
2.完成用户登录
?业务接口文档
2.1编辑SysResult对象(VO层)
vo:前后端进行数据交互的媒介
package com.jt.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class SysResult {
private Integer status;//200表示正常 201表示失败
private String msg; //服务器返回的提示信息
private Object data; //服务器返回的业务数据
/*重载规则:参数不要耦合,否则会产生歧义*/
public static SysResult fail(){
return new SysResult(201,"服务调用失败",null);
}
public static SysResult sucess(Object data){
return new SysResult(200,"服务调用成功",data);
}
public static SysResult sucess(String msg,Object data){
return new SysResult(200,msg,data);
}
}
2.2业务流程
2.2.1用户输入用户名与密码
2.2.2通过Vue的axios发起post请求
2.2.3Controller接收请求 需要将密码进行加密处理
2.2.4查询成功则输入正确
登陆成功,返回token密钥,利用SysResult对象返回
2.2.5查询失败则输入有误
2.3 MD5
特点:只能由明文加密为密文,不可逆,不可破解
升级版本:MD5Hash=明文+盐值? 几乎不可破解
Controller层
package com.jt.controller;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
/*根据userName与password查询数据库
* 类型post
* 返回值SysResult
* */
@PostMapping("login")
public SysResult login(@RequestBody User user){
String token=userService.findUserByUP(user);
//表示用户名和密码错误
if(token==null||"".equals(token)){
return SysResult.fail();
}
//表示用户名与密码正确,返回密钥信息
return SysResult.sucess(token);
}
}
Service层
package com.jt.service;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
//根据u/p查询数据库
@Override
public String findUserByUP(User user) {
// 1.将密码加密
byte[] bytes = user.getPassword().getBytes();
String md5Pass = DigestUtils.md5DigestAsHex(bytes);
user.setPassword(md5Pass);
//2.根据用户名和密文查询数据库
User userDB = userMapper.findUserByUP(user);
//3.判断userDB是否有值
if(userDB == null){
//查询没有数据.返回null
return null;
}
//密钥特点:唯一性、迷惑性 UUID几乎可以保证唯一性
return UUID.randomUUID().toString().replace("-","");
}
}
|