软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

后端第三次作业 — 吕坤蓬(下班下班

  • 吕坤蓬
  • 2022-10-30
  • 0

用户管理系统

框架的实现

Po 持久化对象

数据表,类。 对应 相应的数据表

@TableName(“数据表名“)

@TableId(type = IdType.AUTO) 主键自增

VO 视图对象

对返回数据的包装然后呈现给前端的数据

Result对象

基于SpringMVC

Service

@Service 注解标注了这是一个服务

@Autowired 意为自动装配, bean自动装配

Service 写具体的方法实现。

Controller

Controller负责调用Service 可以说是接口

Service负责逻辑的编写

一般使用Controller 调用 Service 的方法

@RestController == @RestquestBody + @Controller

首先

@Controller

声明这个类是一个控制器类,

@RestquestBody

表示方法的返回值直接以指定的格式写入 Http responseBody中,而不是解析为 跳转路径。

@RestController是@Controller和@ResponseBody两者的结合,使用这个注解后该controller的所有方法都会返回json格式的数据,因为@ResponseBody的作用就是把返回的对象转换为json格式,并把json数据写入response的body中,前台收到response时就可以获取其body中的json数据了。

如果在整个controller类上方添加@RestController,其作用就相当于把该controller下的所有方法都加上@ResponseBody,使每个方法直接返回response对象。

UserMapper

mapper --和去年repository 一样负责数据库数据的CRUD

通过 注解 SQL 语句来实现数据的使用。

筛选语句 Select

格式

  • 选择 某表的 某行的某列数据(不常用)

    select 列名1 from 数据表名 where 列名2=#{变量2}

  • 选择 某表的 某行数据 (常用)

    select * from 数据表名 where 列名1=#{变量1} and 列名2=#{变量2}

注意:返回的一般是对象 ,然后调用其中的数据。

插入语句 Insert

格式:

  • 指定所要插入数据的列

insert into 数据表名 (列名1,列名2)VALUES(#{对应的变量1},#{对应的变量2})

修改语句 Update

格式:

  • 更新某一行中的若干列。

update 数据表名 set 列名1 = #{变量名1} ,列名2 = #{变量名2} where 列名3 = #{变量名3}

更新列名3 为 变量3 的一行的 列名1 和列名2 的数据。

security 安全

对数据采用AES 双向加密

AES加密算法是密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

(合理CV)

(105条消息) JAVA AES加密与解密_码农致富的博客-CSDN博客_jdk6 解除aes加密限制

直接使用AESUtils帮助类即可

但是第十点该解决方法我没能在自己的IDE上找到相应的JRE 文件,所以现在的解决方案是

//源代码 : 
 String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
//替换为
 String AES_encode = Base64.getEncoder().encodeToString(byte_AES);
//源代码:
byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
//替换为:
byte[] byte_content = Base64.getDecoder().decode(content);
 //源代码:
 public static byte[] base64Decode(String base64Code) throws Exception{  
         return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
    }  
 public static String base64Encode(byte[] bytes){  
     return new BASE64Encoder().encode(bytes);  
 }
 //替换为:
 public static byte[] base64Decode(String base64Code) throws Exception {
         return StringUtils.isEmpty(base64Code) ? null : Base64.getDecoder().decode(base64Code);
 }
 public static String base64Encode(byte[] bytes) {
     return Arrays.toString(Base64.getEncoder().encode(bytes));
 }

然后直接使用 加密 解密即可

加密: AESEncode 方法

String fpassword = AESUtils.AESEncode(password);
user.setPassword(fpassword);

解密:AESDecode 方法

String password = user.getPassword();
String fpassword = AESUtils.AESDecode(password);

基本功能

用户登录

    public Result login(String userName,String password){
        User user = userMapper.findUserByUserName(userName);
        if(user==null)return Result.error(600,"该用户名未注册");
        if(!user.getPassword().equals(password)){
            return Result.error(601,"密码错误");
        }
        return Result.success("登录成功");
    }

用户注册

public Result signup(String userName,String password){
    User user = userMapper.findUserByUserName(userName);
    if(user!=null)return Result.error(602,"该用户名已被注册");
    Integer maxUserId = userMapper.getMaxUserId();

    user = new User();

    if(maxUserId!=null)user.setUserId(maxUserId+1);
    else user.setUserId(1);
    user.setId(null);
    user.setUserName(userName);

    String fpassword = AESUtils.AESEncode(password);
    user.setPassword(fpassword);

    user.setProblem(null);
    user.setAns(null);
    userMapper.insert(user);
    return Result.success("注册成功");
}

密码修改

public Result passwordChange(String userName,String password){
    User user = userMapper.findUserByUserName(userName);
    if(user==null)return Result.error(600,"该用户名未注册");

    String fpassword = AESUtils.AESEncode(password);
    user.setPassword(fpassword);

    userMapper.update(user);
    return Result.success("修改成功");
}

密码获取

public Result passwordGet(String userName){
    User user = userMapper.findUserByUserName(userName);
    if(user==null)return Result.error(600,"该用户名未注册");
    String password = user.getPassword();
    String fpassword = AESUtils.AESDecode(password);

    return Result.success(fpassword);
}

用户信息获取

public Result getUser(String userName){
    User user = userMapper.findUserByUserName(userName);
    if(user==null)return Result.error(600,"该用户名未注册");
    return Result.success(user);
}

密保问题和答案

public Result changeUser(String userName,String problem,String ans){
    User user = userMapper.findUserByUserName(userName);
    if(user==null)return Result.error(600,"该用户名未注册");
    //对密保问题和密码输入的判断交给前端了,
    user.setProblem(problem);
    user.setAns(ans);
    userMapper.update(user);
    return Result.success("修改成功");
}

获取所有User的信息 (不该出现的方法 ,只是为了验证@RestController对json的使用

public Result getAllUser(){
    List<User> users =  userMapper.findAllUsers();
    for(User user: users){//foreach来处理所有数据
        String password = user.getPassword();
        String fpassword = AESUtils.AESDecode(password);
        user.setPassword(fpassword);
    }
    return Result.success(users);
}

Controller代码

package com.example.demo.controller;

import com.example.demo.data.vo.Result;
import com.example.demo.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class MainController {
    @Autowired
    MainService mainService;

    @PostMapping("/login")//登录
    public Result login(@RequestHeader String userName,@RequestHeader String password){
        //对数据判空的处理和数据类型判断交给前端
        return mainService.login(userName,password);
    }

    @PostMapping("/signup")//注册
    public Result signup(@RequestHeader String userName,@RequestHeader String password){
        return mainService.signup(userName,password);
    }
    @PostMapping("/passwordChange")//密码修改
    public Result passwordChange(@RequestHeader String userName,@RequestHeader String password){
        return mainService.passwordChange(userName,password);
    }

    @PostMapping("/passwordGet")//密码获取
    public Result passwordGet(@RequestHeader String userName){
        return mainService.passwordGet(userName);
    }

    @PostMapping("/getUser")//获取用户信息
    public Result getUser(@RequestHeader String userName){
        return mainService.getUser(userName);
    }
    @PostMapping("/changeUser")
    public Result changeUser(@RequestHeader String userName,@RequestHeader String problem,@RequestHeader String ans){
        return mainService.changeUser(userName,problem,ans);
    }
    @PostMapping("/getAllUser")
    public Result getAllUser(){
        return mainService.getAllUser();
    }
}
吕坤蓬
吕坤蓬
© 2025 软件园学生在线
Theme by Wing