diff --git a/src/main/java/cn/itsource/basic/config/MvcConfig.java b/src/main/java/cn/itsource/basic/config/MvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..ac00d6a04ee4dcd0cd1332c0af757f8aa3db16de --- /dev/null +++ b/src/main/java/cn/itsource/basic/config/MvcConfig.java @@ -0,0 +1,29 @@ +package cn.itsource.basic.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 拦截器的配置类 + */ +@Configuration // 声明当前类是一个配置类 相当于之前的xml配置 +public class MvcConfig implements WebMvcConfigurer { + + /** + * 注入自定义的拦截器 + * @param registry + */ + @Autowired + private MyInterceptor myInterceptor; + + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(myInterceptor) + .addPathPatterns("/**") + .excludePathPatterns("/file/**","/verifyCode/**","/system/**") // 放行特定资源 + .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"); // 放行swagger + } +} diff --git a/src/main/java/cn/itsource/basic/config/MyInterceptor.java b/src/main/java/cn/itsource/basic/config/MyInterceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..6bdab355f2d42a72bb4d9d8dfd57d77ccc777c68 --- /dev/null +++ b/src/main/java/cn/itsource/basic/config/MyInterceptor.java @@ -0,0 +1,85 @@ +package cn.itsource.basic.config; + +import cn.hutool.core.util.StrUtil;; +import cn.itsource.basic.util.AjaxResult; +import cn.itsource.basic.util.RedisService; +import cn.itsource.org.domain.LoginInfo; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * 自定义拦截器 + */ +@Component // 将当前自定义的拦截器交给SPringle 管理 +@Slf4j // 日志 +public class MyInterceptor implements HandlerInterceptor { + + + @Autowired + private RedisService redisService; + + /** + * 这里就是拦截器的核心方法 + * 此方法返回true表示放行请求 + * 此方法返回false表示拦截请求 + * @param request + * @param response + * @param handler + * @return + * @throws Exception + */ + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + // 登录的拦截逻辑 + // 1. 拿到前端请求的请求头里面的token数据 + String token = request.getHeader("token"); + + log.info("拿到的token"); + log.info(token); + // 2. 判断token是否为空 + if ("null".equals(token) || StrUtil.isEmpty(token)) { + visitSubject(response); + return false; + } + + // 3. 使用token拿到存在redis中的用户数据 + LoginInfo loginInfo = redisService.getKeyObjectValue(token, LoginInfo.class); + if (loginInfo == null) { + visitSubject(response); + return false; + } + + // 4. 将用户信息重写保存到redis中 + redisService.setStringKeyAndValue(token, loginInfo,30); // 设置30分钟有效期 + + return true; + } + + /** + * alt + control + m + * @param response + * @throws IOException + */ + private void visitSubject(HttpServletResponse response) throws IOException { + // 设置响应数据格式 + response.setContentType("application/json;charset=utf-8"); + // 1. 自己返回一个错误信息给前端 + AjaxResult result = AjaxResult.createError("6666", "登录去!!"); + // 2. 将ajaxresult转换成一个json字符串 + String jsonString = JSONObject.toJSONString(result); + // 3. 获取返回对象 + PrintWriter writer = response.getWriter(); + // 4. 返回json字符串数据 + writer.write(jsonString); // 返回的是一个json类型的数据 + } +} diff --git a/src/main/java/cn/itsource/org/controller/LoginfoController.java b/src/main/java/cn/itsource/org/controller/LoginfoController.java new file mode 100644 index 0000000000000000000000000000000000000000..8b2d7447cc1427367acd4a47056b77516461d356 --- /dev/null +++ b/src/main/java/cn/itsource/org/controller/LoginfoController.java @@ -0,0 +1,35 @@ +package cn.itsource.org.controller; + +import cn.itsource.basic.util.AjaxResult; +import cn.itsource.org.domain.Employee; +import cn.itsource.org.domain.LoginInfo; +import cn.itsource.org.service.ILoginfoService; +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; + +@RestController +@RequestMapping("/system") +public class LoginfoController { + + @Autowired + private ILoginfoService service; + + // this.$http.post("/system/login",loginParams).then(res => { + @PostMapping("/login") + public AjaxResult login(@RequestBody Employee employee) { + try { + LoginInfo login = service.login(employee); + return AjaxResult.createSuccess(login); + } catch (RuntimeException e) { + e.printStackTrace(); + return AjaxResult.createError(e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + return AjaxResult.createError("系统错误"); + + } + } +} diff --git a/src/main/java/cn/itsource/org/domain/Shop.java b/src/main/java/cn/itsource/org/domain/Shop.java index c14e8eb78328e7eda448e8ad660668619b49ba57..16779cf389d9d873fdf723a76643cf1820c3f93e 100644 --- a/src/main/java/cn/itsource/org/domain/Shop.java +++ b/src/main/java/cn/itsource/org/domain/Shop.java @@ -59,6 +59,6 @@ public class Shop extends BaseDomain { private Long adminId; - @Transient // 临时字段 + @Transient private Employee admin; } \ No newline at end of file diff --git a/src/main/java/cn/itsource/org/mapper/EmployeeMapper.java b/src/main/java/cn/itsource/org/mapper/EmployeeMapper.java index 96a69267335d5d59bc8c6e5ac03efc43ed047cbb..101cf25d484befbf1f926aba048390dd9fdbfc9d 100644 --- a/src/main/java/cn/itsource/org/mapper/EmployeeMapper.java +++ b/src/main/java/cn/itsource/org/mapper/EmployeeMapper.java @@ -19,6 +19,10 @@ public interface EmployeeMapper extends Mapper { */ List getAllManager(); - // 基础的CRUD通过继承获得。。。 + // 基础的CRUD通过继承获得 + + Employee getLoginInfo(String info); + + } diff --git a/src/main/java/cn/itsource/org/service/ILoginfoService.java b/src/main/java/cn/itsource/org/service/ILoginfoService.java new file mode 100644 index 0000000000000000000000000000000000000000..c7c3ea4bc86bcb65cf69099782cff058ad1cdd63 --- /dev/null +++ b/src/main/java/cn/itsource/org/service/ILoginfoService.java @@ -0,0 +1,12 @@ +package cn.itsource.org.service; + + +import cn.itsource.org.domain.Employee; +import cn.itsource.org.domain.LoginInfo; + + +public interface ILoginfoService { + + LoginInfo login(Employee info); + +} diff --git a/src/main/java/cn/itsource/org/service/impl/LoginfoServiceImpl.java b/src/main/java/cn/itsource/org/service/impl/LoginfoServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..dddd1551b51ba51dc115796d4efbdb271a5a395e --- /dev/null +++ b/src/main/java/cn/itsource/org/service/impl/LoginfoServiceImpl.java @@ -0,0 +1,54 @@ +package cn.itsource.org.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.SecureUtil; +import cn.itsource.basic.util.RedisService; +import cn.itsource.org.domain.Employee; +import cn.itsource.org.domain.LoginInfo; +import cn.itsource.org.mapper.EmployeeMapper; +import cn.itsource.org.service.ILoginfoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class LoginfoServiceImpl implements ILoginfoService { + + @Autowired + private EmployeeMapper employeeMapper; + + @Autowired + private RedisService redisService; + + + @Override + public LoginInfo login(Employee info) { + + //1.查询用户名是否存在 + Employee employee = employeeMapper.getLoginInfo(info.getUsername()); + //2.根据数据库的info是否为空 + Optional.ofNullable(employee).orElseThrow(()-> new RuntimeException("用户不存在")); + + //3.判断密码是否正确 + //3.1加盐加密 + String digestHex = SecureUtil.md5().setSalt(employee.getSalt().getBytes()).digestHex(info.getPassword()); + + //3.2校验密码 + if (!StrUtil.equals(digestHex,employee.getPassword())){ + throw new RuntimeException("密码错误请重新输入"); + } + //4.生成token + String token = IdUtil.fastSimpleUUID(); + //5.将用户登录信息保存到redis + LoginInfo logininfo = new LoginInfo(); + BeanUtil.copyProperties(employee,logininfo); + + //6用户信息保存到redis + logininfo.setToken(token); + redisService.setStringKeyAndValue(token, logininfo); + return logininfo; + } +} diff --git a/src/main/java/cn/itsource/org/service/impl/UserServiceImpl.java b/src/main/java/cn/itsource/org/service/impl/UserServiceImpl.java index b4d4c49486db5950d2b0ccf2c555cc8b4a84ab2b..e4dfe6bd1b6ddd93037ebdebd569f87944695ff6 100644 --- a/src/main/java/cn/itsource/org/service/impl/UserServiceImpl.java +++ b/src/main/java/cn/itsource/org/service/impl/UserServiceImpl.java @@ -71,7 +71,7 @@ public class UserServiceImpl extends BaseServiceImpl implements // String s = request.getRemoteAddr() + request.getRequestURI(); // // String res = redisService.getKeyObjectValue(s, String.class); -// if (res != null){ //如果redis里面有数据,代表你访问过于频繁 +// if (res != null){ // throw new RuntimeException("获取验证码过于频繁"); // } // //存ip到redis中,保持1分钟,1分钟内重复获取抛出上面的异常 @@ -81,7 +81,7 @@ public class UserServiceImpl extends BaseServiceImpl implements // String code = RandomUtil.randomNumbers(4); // //phone:手机号 code:验证码 // redisService.setStringKeyAndValue(phone,code,5); -// //[宠物乐园验证码]:1905,手机号:17799996666 +// //[ 验证码]:1905,手机号:17799996666 // log.debug("["+ phone +"发送的短信验证码]:" + code); diff --git a/src/main/resources/cn/itsource/org/mapper/EmployeeMapper.xml b/src/main/resources/cn/itsource/org/mapper/EmployeeMapper.xml index 0b0bd8da30428ed32b5b0cacbc5305f6b24472b0..6d8e82639761c6b7d9ef9219ccd8e05eb313fa33 100644 --- a/src/main/resources/cn/itsource/org/mapper/EmployeeMapper.xml +++ b/src/main/resources/cn/itsource/org/mapper/EmployeeMapper.xml @@ -5,4 +5,9 @@ + + \ No newline at end of file diff --git a/src/main/resources/generator.xml b/src/main/resources/generator.xml index 5dc40cf29165fcc71089cd772282f8855b963383..f3bc251149b77bc5dafe291534dfc4e2deb08052 100644 --- a/src/main/resources/generator.xml +++ b/src/main/resources/generator.xml @@ -67,7 +67,7 @@ - +