1、创建service下的子模块service-ucenter
2、复制代码生成器
生成对应结构
3、配置application.properties
# 服务端口
server.port=8001
# 服务名
spring.application.name=service-edu
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#Springboot2.0以上要加时区
spring.datasource.url=jdbc:mysql://localhost:3306/onlineedu?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/dm/eduservice/mapper/xml/*.xml
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#开启熔断机制
feign.hystrix.enabled=true
# 设置hystrix超时时间,默认1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4.书写方法
controller
@PostMapping("login") public R login(@RequestBody LoginVo loginVo) { String token = memberService.login(loginVo); return R.OK().data("token", token); }
MD5
package com.dm.commonutils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class MD5 { public static String encrypt(String strSrc) { try { char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; byte[] bytes = strSrc.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); bytes = md.digest(); int j = bytes.length; char[] chars = new char[j * 2]; int k = 0; for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; chars[k++] = hexChars[b >>> 4 & 0xf]; chars[k++] = hexChars[b & 0xf]; } return new String(chars); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("MD5加密出错!!+" + e); } } public static void main(String[] args) { System.out.println(MD5.encrypt("111111")); } }
serviceImpl
@Override public String login(LoginVo loginVo) { String mobile = loginVo.getMobile(); String password = loginVo.getPassword(); //校验参数 if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile)) { throw new onlineEduException(20001,"error"); } //获取会员 UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile)); if(null == member) { throw new onlineEduException(20001,"error"); } //校验密码 if(!MD5.encrypt(password).equals(member.getPassword())) { throw new onlineEduException(20001,"error"); } //校验是否被禁用 if(member.getIsDisabled()) { throw new onlineEduException(20001,"error"); } //使用JWT生成token字符串 String token = JwtUtils.getJwtToken(member.getId(), member.getNickname()); return token; }
但是为了测试我们先不加密
if(!password.equals(member.getPassword())) { throw new onlineEduException(20001,"error"); }
进行测试:
等后期注册接口写完我们再把这个加密加上去
测试成功返回了一个token