Springboot初始化项目并完成登入注册的全过程
作者:万伏小太阳 发布时间:2023-07-31 15:09:09
标签:springboot,初始化项目,注册
idea spring Initializr创建项目
勾选项目所需要的依赖
pom.xml
文件会加载勾选的依赖,也可以不勾选后面通过自己常用的pom.xml
统一导入。
Lombok 可以通过注解省去一些get,set方法。
简单项目常用pom.xml:
mybatis-plus、mybatis、
整体项目结构
创建User实体类放入Enity下
@TableName("user")
是实体对应的表的名字
@TableId(type = IdType.AUTO)
表的主键
@TableField(exist = false)
不存在,就不会返回空。
package com.example.community.enity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Integer userId;
private String email;
private String username;
private String password;
private String avatar;
private String phone;
@TableField(exist = false)
private String code;
public User(String email, String password) {
this.email = email;
this.password = password;
}
}
创建通用返回的结果类
可以使用泛型传入对应的实体类,这里我就不传了。
package com.example.community.common;
import lombok.Data;
@Data
public class R {
private Integer code;
private String msg;
private Object data;
public R(Integer code, String msg, Object ob) {
this.code = code;
this.msg = msg;
this.data = ob;
}
public R(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public static R success(String msg){
R r= new R(1,msg);
return r;
}
public static R success(String msg,Object ob){
R r= new R(1,msg,ob);
return r;
}
public static R error(String msg){
R r= new R(0,msg,null);
return r;
}
}
创建controller
在controller里可以对前端的请求进行一个分发,处理。根据地址链接找到对应的controller。
登入模块,可以创建一个UserController
处理用户的相关信息。
package com.example.community.controller;
import com.example.community.common.R;
import com.example.community.enity.User;
import com.example.community.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Slf4j
@RestController()
@RequestMapping("/user")
@CrossOrigin(origins = {"http://localhost:8090", "null"})
public class UserController {
@Autowired
UserService service;
@PostMapping("/login")
public R login(@RequestBody User user) {
log.info("user"+user.toString());
return service.login(user.getEmail(), user.getPassword());
}
@PostMapping("/register")
@ResponseBody
public R register(@RequestBody Map
创建service层
service
层给controller
层提供服务,调用dao
层mapper
查询数据库的数据,提供相对应的服务。
在这里我是用了mybtis-plus
来进行数据库简单的查询。
用户模块的service层代码:
UserService
接口
继承mybatis-plus
的IService
package com.example.community.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.community.common.R;
import com.example.community.enity.User;
public interface UserService extends IService
UserService
实现类
在这个类中,调用了mapper提供的方法。
继承ServiceImpl
package com.example.community.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.community.Mapper.UserMapper;
import com.example.community.common.R;
import com.example.community.enity.User;
import com.example.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl
创建Mapper / Dao层
使用了mybatis-plus
,所以只需要继承BaseMapper
既可。
package com.example.community.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.community.enity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper
邮箱验证码、生成Token
使用邮箱进行登入验证码验证
使用
jwt
生成token
返回给前端,服务端可以用来判断用户是否登入。并且支持跨域
实现这两个功能需要使用到一些工具类和配置,如下:
package com.example.community.utils;
import io.jsonwebtoken.Jwt;
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 JWTUtils {
private static final String jwtToken = "123456Mszlu!@#$$";
public static String createToken(Long userId) {
Map
* 的配置
package com.example.community.handler;
import com.alibaba.druid.util.StringUtils;
import com.example.community.common.R;
import com.example.community.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
log.info("请求被拦截{}",request);
if(!(handler instanceof HandlerMethod)){
return true;
}
String token = request.getHeader("Authorization");
log.info("token:{}",token);
if(StringUtils.isEmpty(token)){
R r= R.error("未登录");
response.setContentType("application/json;charset=utf-8");
log.info("{}",r);
response.getWriter().print(JsonUtil.objectToJson(r));
return false;
}
return true;
}
}
package com.example.community.config;
import com.example.community.handler.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 用户 *
registry.addInterceptor(new LoginInterceptor())
// 需要拦截的请求
.addPathPatterns("/user/**","/article/**")
// 需要放行的请求
.excludePathPatterns("/user/login","/user/register","/mail/**")
// 添加swagger-ui的放行路径
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**")
;
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
}
实现邮箱认证登入,之前写过一个,传送门
来源:https://blog.csdn.net/weixin_51009975/article/details/127510534
0
投稿
猜你喜欢
- 知识点:1.使用SQL Helper创建数据库2.数据的增删查改(PRDU:Put、Read、Delete、Update)背景知识:上篇文章
- 样例代码在讲 Flutter 的盒子模型前,先看看HTML 中的盒子模型。如下图所示,一个页面元素包括了与父级容器的外边距(margin),
- 以前就遇到过这个问题,今天重新拾起来。跑马灯效果其实就是当文字超过TextView控件宽度的时候,使用滚动的方式显示出来:方法1:(直接xm
- 摘要:这个问题算是老生常谈了,我也是一段时间没弄过了,所以感觉有些忘了,就记录一下。一、后端通过shiro在session中存储数据://
- 前言我们在 页面切换转场动画,英雄救场更有趣!介绍了 Hero 动画效果,使用 Hero 用于转场能够提供非常不错的体验。既然称之
- 本文实例为大家分享了Java实现简单幸运抽奖的具体代码,供大家参考,具体内容如下代码模块:User类:package test1;publi
- 前言在网络通信中,通信传输数据容易被截取或篡改,如果在传输用户隐私数据过程中,被不法分子截取或篡改,就可能导致用户受到伤害,比如被诈 骗,所
- 本文实例为大家分享了C# GDI+实现时钟表盘的具体代码,供大家参考,具体内容如下一、设计如下图界面按键“打开时钟&am
- 一、整体设计1、需求分析池化技术是计算机中的一种设计模式,内存池是常见的池化技术之一,它能够有效的提高内存的申请和释放效率以及内存碎片等问题
- 走马灯是一种常见的效果,本文讲一下如何用 PageView 在 Flutter 里实现一个走马灯, 效果如下,当前页面的高度比其它页面高,切
- instanceof判断某个对象是否是某个类的实例或者某个类的子类的实例。它的判断方式大概是这样的:public<T> bool
- 如果有哪一个做程序员的小伙伴说自己没有遇到中文乱码问题,我是不愿意相信的。今天在做微信订阅号的智能回复时,又一时迷乱的跳进了中文乱码这个火坑
- 上篇介绍了几种图表的公共组件X、Y轴、背景Board的绘制。这章节介绍柱状图表的绘制,相对其它图表而言简单一些,这里主要介绍图表主体的绘制,
- 格式要求:SU MO TU WE TH FR SA &nb
- 背景:在Android中按照数据保存的方式,可以分为如下几种Content Provider (用的SQLite实现),SQLite,Sha
- 在谈 JVM 内存区域划分之前,我们先来看一下 Java 程序的具体执行过程,我画了一幅图。Java 源代码文件经过编译器编译后生成字节码文
- 一、demo简介1.效果展示如下图,我截了三个瞬间,但其实这是一个连续的动画,就是这个大圆不停地吞下小圆。2.这个动画可以拆分为两部分,首先
- 只能输入数字:"^[0-9]*$"。只能输入n位的数字:"^\d{n}$"。只能输入至少n位的数字:
- 序言在flutter开发中,我们使用 bloc 框架,基于状态变更进行响应式开发。本篇文章,小轰将 bloc 核心业务块进行拆解简化,聊一聊
- strftime函数主要用于时间格式化,它的函数原型如下:size_t __cdecl strftime(char * __restrict