后台登陆功能完善

This commit is contained in:
Unique-Jerry 2023-10-18 17:01:50 +08:00
parent b101fc742d
commit 0078c8cbd6
14 changed files with 275 additions and 44 deletions

View File

@ -91,6 +91,7 @@
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -0,0 +1,35 @@
package com.yzdx.AiInterviewer.comment;
import lombok.Data;
@Data
public class R<T>{
private Integer code;
private String message;
private Object data;
public static<T> R<T> success(T object){
R r= new R<>();
r.code=0;
r.data=object;
r.message="成功";
return r;
}
public static<T> R<T> error(String message){
R r=new R<>();
r.code=1;
r.message=message;
return r;
}
public static<T> R<T> error(Integer code,String message){
R r=new R<>();
r.code=code;
r.message=message;
return r;
}
public R() {
}
}

View File

@ -19,7 +19,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
// 需要拦截的请求
.addPathPatterns("/**")
// 需要放行的请求
.excludePathPatterns("/user/login","/user/register","/user/sendCode")
.excludePathPatterns("/admin/login")
// 添加swagger-ui的放行路径
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/doc.html/**")
;

View File

@ -1,36 +0,0 @@
package com.yzdx.AiInterviewer.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
@ApiOperation("用户登录")
@PostMapping("/test")
public Map<String ,Object> testLogin(@ApiParam("用户名") String username ,@ApiParam("密码")String password){
Map<String ,Object> result=new HashMap<>();
result.put("code",0);
result.put("message","登陆成功");
result.put("data","username:"+username+","+"password:"+password);
return result;
}
}

View File

@ -0,0 +1,35 @@
package com.yzdx.AiInterviewer.controller;
import com.yzdx.AiInterviewer.comment.R;
import com.yzdx.AiInterviewer.service.UserService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/admin")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public R adminLogin(@RequestBody @ApiParam("传入的值phone,encoding,password") Map<String,Object> loginForm){
if(loginForm.size()==0){
return R.error("传来的数据有误,请检查输入");
}
String phone=(String)loginForm.get("phone");
String encoding=(String)loginForm.get("encoding");
String password=(String)loginForm.get("password");
return userService.adminLogin(phone, encoding, password);
}
}

View File

@ -0,0 +1,17 @@
package com.yzdx.AiInterviewer.entity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@Data
public class BaseEntity {
@ApiModelProperty("电子邮箱")
private Date createTime;
@ApiModelProperty("电子邮箱")
private Date updateTime;
@ApiModelProperty("电子邮箱")
private Integer createUser;
@ApiModelProperty("电子邮箱")
private Integer updateUser;
}

View File

@ -0,0 +1,45 @@
package com.yzdx.AiInterviewer.entity;
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 io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@Data
@ApiModel("公司主体类")
@TableName("company")
public class Company extends BaseEntity{
@ApiModelProperty("公司id")
@TableId(type = IdType.AUTO)
private Integer id;
@ApiModelProperty("公司名称")
private String companyName;
@ApiModelProperty("公司全称")
@TableField(value = "company_allname")
private String companyAllName;
@ApiModelProperty("公司详情")
private String companyDetail;
@ApiModelProperty("公司类型")
private String type;
@ApiModelProperty("公司地址")
private String address;
@ApiModelProperty("公司待遇")
private String treatment;
@ApiModelProperty("法定代表人")
private String legalRepresentative;
@ApiModelProperty("注册资本")
private String registeredCapital;
@ApiModelProperty("成立日期")
private Date established;
@ApiModelProperty("统一信用编码")
private String creditCode;
@ApiModelProperty("公司图片")
private String images;
@ApiModelProperty("公司使用的登陆编码")
private String encoding;
}

View File

@ -1,12 +1,35 @@
package com.yzdx.AiInterviewer.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("用户实体类")
public class User {
@Data
@TableName("user")
public class User extends BaseEntity{
@ApiModelProperty("用户id")
@TableId(type = IdType.AUTO)
private Integer id;
@ApiModelProperty("用户名")
public String username;
private String username;
@ApiModelProperty("密码")
public String password;
private String password;
@ApiModelProperty("盐值")
private String salt;
@ApiModelProperty("角色")
private String role;
@ApiModelProperty("手机号码")
private String phone;
@ApiModelProperty("性别")
private String sex;
@ApiModelProperty("年龄")
private String age;
@ApiModelProperty("头像地址")
private String avatar;
@ApiModelProperty("电子邮箱")
private String email;
}

View File

@ -0,0 +1,9 @@
package com.yzdx.AiInterviewer.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yzdx.AiInterviewer.entity.Company;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CompanyMapper extends BaseMapper<Company> {
}

View File

@ -0,0 +1,9 @@
package com.yzdx.AiInterviewer.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yzdx.AiInterviewer.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}

View File

@ -0,0 +1,17 @@
package com.yzdx.AiInterviewer.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yzdx.AiInterviewer.comment.R;
import com.yzdx.AiInterviewer.entity.User;
public interface UserService extends IService<User> {
/** 管理员登录业务
* @param phone,encoding,password
* @return boolean,ture登陆成功,false登陆失败
*
*/
R adminLogin(String phone, String encoding, String password);
}

View File

@ -0,0 +1,76 @@
package com.yzdx.AiInterviewer.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzdx.AiInterviewer.comment.R;
import com.yzdx.AiInterviewer.entity.Company;
import com.yzdx.AiInterviewer.entity.User;
import com.yzdx.AiInterviewer.mapper.CompanyMapper;
import com.yzdx.AiInterviewer.mapper.UserMapper;
import com.yzdx.AiInterviewer.service.UserService;
import com.yzdx.AiInterviewer.utiles.JWT;
import com.yzdx.AiInterviewer.utiles.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private CompanyMapper companyMapper;
@Override
public R adminLogin(String phone, String encoding, String password) {
LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper();
queryWrapper.eq(User::getPhone,phone);
User user = userMapper.selectOne(queryWrapper);
//若查询用户为空
if(user==null){
return R.error("账号不存在,请检查输入");
}
if (user.getRole().equals("3") || user.getRole().equals("4")) {
return R.error("输入的账号或密码错误");
}
//查询的用户存在检查密码是否正确
String salt=user.getSalt();
String findPassword=user.getPassword();
password= MD5Util.GetMD5Password(password,salt);
if(!findPassword.equals(password)){
return R.error("输入的账号或密码错误");
}
//密码和账户都输入正确检查公司编码是否正确
LambdaQueryWrapper<Company> queryWrapper1=new LambdaQueryWrapper();
queryWrapper1.eq(Company::getEncoding,encoding);
Company findCompany= companyMapper.selectOne(queryWrapper1);
if(findCompany==null){
return R.error("输入的公司编码有误,请检查输入");
}
//均正确返回token密钥
String token=JWT.getJWToken(user.getId());
Map<String,Object> data=new HashMap<>();
data.put("token",token);
return R.success(data);
}
}

View File

@ -35,7 +35,7 @@ public class JWT {
JWTCreator.Builder builder = com.auth0.jwt.JWT.create();
// 指定标识字段
builder.withClaim("lawyerId", id);
builder.withClaim("userid", id);
// 指定过期时间
token = builder.withExpiresAt(instance.getTime())
@ -63,6 +63,6 @@ public class JWT {
// 从token中获取用户id
public static int getTokenId(String token){
DecodedJWT verify = com.auth0.jwt.JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
return verify.getClaim("Id").asInt();
return verify.getClaim("userid").asInt();
}
}

View File

@ -3,9 +3,9 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
url: jdbc:mysql://101.43.255.47:3306/ai_interviewer?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
password: 2002811
jackson:
default-property-inclusion: non_null