Browse Source

老师登录功能

Signed-off-by: guoWenHao <2532478980@qq.com>
guoWenHao 10 tháng trước cách đây
mục cha
commit
5aa45938e6

+ 18 - 0
teacher-common/src/main/java/com/example/constant/MessageConstant.java

@@ -0,0 +1,18 @@
+package com.example.constant;
+
+/**
+ * 信息提示常量类
+ */
+public class MessageConstant {
+
+    public static final String PASSWORD_ERROR = "密码错误";
+    public static final String ACCOUNT_NOT_FOUND = "账号不存在";
+    public static final String ACCOUNT_LOCKED = "账号被锁定";
+    public static final String ALREADY_EXISTS = "已存在";
+    public static final String UNKNOWN_ERROR = "未知错误";
+    public static final String USER_NOT_LOGIN = "用户未登录";
+    public static final String LOGIN_FAILED = "登录失败";
+    public static final String UPLOAD_FAILED = "文件上传失败";
+    public static final String PASSWORD_EDIT_FAILED = "密码修改失败";
+
+}

+ 13 - 0
teacher-common/src/main/java/com/example/constant/StatusConstant.java

@@ -0,0 +1,13 @@
+package com.example.constant;
+
+/**
+ * 状态常量,启用或者禁用
+ */
+public class StatusConstant {
+
+    //启用
+    public static final Integer ENABLE = 1;
+
+    //禁用
+    public static final Integer DISABLE = 0;
+}

+ 15 - 0
teacher-common/src/main/java/com/example/exception/AccountLockedException.java

@@ -0,0 +1,15 @@
+package com.example.exception;
+
+/**
+ * 账号被锁定异常
+ */
+public class AccountLockedException extends BaseException {
+
+    public AccountLockedException() {
+    }
+
+    public AccountLockedException(String msg) {
+        super(msg);
+    }
+
+}

+ 15 - 0
teacher-common/src/main/java/com/example/exception/AccountNotFoundException.java

@@ -0,0 +1,15 @@
+package com.example.exception;
+
+/**
+ * 账号不存在异常
+ */
+public class AccountNotFoundException extends BaseException {
+
+    public AccountNotFoundException() {
+    }
+
+    public AccountNotFoundException(String msg) {
+        super(msg);
+    }
+
+}

+ 15 - 0
teacher-common/src/main/java/com/example/exception/BaseException.java

@@ -0,0 +1,15 @@
+package com.example.exception;
+
+/**
+ * 业务异常
+ */
+public class BaseException extends RuntimeException {
+
+    public BaseException() {
+    }
+
+    public BaseException(String msg) {
+        super(msg);
+    }
+
+}

+ 15 - 0
teacher-common/src/main/java/com/example/exception/PasswordErrorException.java

@@ -0,0 +1,15 @@
+package com.example.exception;
+
+/**
+ * 密码错误异常
+ */
+public class PasswordErrorException extends BaseException {
+
+    public PasswordErrorException() {
+    }
+
+    public PasswordErrorException(String msg) {
+        super(msg);
+    }
+
+}

+ 19 - 0
teacher-pojo/src/main/java/com/example/dto/UserLoginDTO.java

@@ -0,0 +1,19 @@
+package com.example.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@ApiModel(description = "老师登录时传递的数据模型")
+public class UserLoginDTO implements Serializable {
+
+    @ApiModelProperty("用户名")
+    private String username;
+
+    @ApiModelProperty("密码")
+    private String password;
+
+}

+ 45 - 0
teacher-pojo/src/main/java/com/example/entity/User.java

@@ -0,0 +1,45 @@
+package com.example.entity;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+@Data
+@Builder//可通过builder直接设置属性
+@NoArgsConstructor
+@AllArgsConstructor
+@ApiModel(description = "账户信息")
+public class User implements Serializable {
+
+    @ApiModelProperty("主键值")
+    private Long id;
+
+    @ApiModelProperty("用户名")
+    private String userName;
+
+    @ApiModelProperty("姓名")
+    private String name;
+
+    @ApiModelProperty("jwt令牌")
+    private String token;
+
+    @ApiModelProperty("创建时间")
+    private LocalDateTime createTime;
+
+    @ApiModelProperty("账号状态")
+    private Integer status;
+
+    @ApiModelProperty("老师id")
+    private Long teacherId;
+
+    @ApiModelProperty("密码")
+    private String password;
+
+
+}

+ 31 - 0
teacher-pojo/src/main/java/com/example/vo/UserLoginVO.java

@@ -0,0 +1,31 @@
+package com.example.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+@Data
+@Builder//可通过builder直接设置属性
+@NoArgsConstructor
+@AllArgsConstructor
+@ApiModel(description = "老师登录返回的数据格式")
+public class UserLoginVO implements Serializable {
+
+    @ApiModelProperty("主键值")
+    private Long id;
+
+    @ApiModelProperty("用户名")
+    private String userName;
+
+    @ApiModelProperty("姓名")
+    private String name;
+
+    @ApiModelProperty("jwt令牌")
+    private String token;
+
+}

+ 10 - 11
teacher-serve/pom.xml

@@ -17,17 +17,16 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
     <dependencies>
-    <dependency>
-        <groupId>com.example</groupId>
-        <artifactId>teacher-common</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-    </dependency>
-
-    <dependency>
-        <groupId>com.example</groupId>
-        <artifactId>teacher-pojo</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-    </dependency>
+        <dependency>
+            <groupId>com.example</groupId>
+            <artifactId>teacher-common</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>com.example</groupId>
+            <artifactId>teacher-pojo</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 9 - 0
teacher-serve/src/main/java/com/example/TeacherTeamSystemApplication.java

@@ -1,9 +1,18 @@
 package com.example;
 
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
 
 @SpringBootApplication
+@EnableTransactionManagement //开启注解方式的事务管理
+@Slf4j
+@EnableCaching//开启缓存注解
+@EnableScheduling//开启任务注解
 public class TeacherTeamSystemApplication {
 
     public static void main(String[] args) {

+ 3 - 4
teacher-serve/src/main/java/com/example/config/WebMvcConfiguration.java

@@ -50,7 +50,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
      * @return
      */
     @Bean
-    public Docket docket1() {
+    public Docket docket2() {
         log.info("准备生成接口文档");
         ApiInfo apiInfo = new ApiInfoBuilder()
                 .title("老师团队系统")
@@ -58,16 +58,15 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
                 .description("老师团队系统")
                 .build();
         Docket docket = new Docket(DocumentationType.SWAGGER_2)
-                .groupName("管理员接口")
+                .groupName("老师账号接口")
                 .apiInfo(apiInfo)
                 .select()
-                .apis(RequestHandlerSelectors.basePackage("com.sky.controller.admin"))
+                .apis(RequestHandlerSelectors.basePackage("com.example.controller.user"))
                 .paths(PathSelectors.any())
                 .build();
         return docket;
     }
 
-
     /**
      * 设置静态资源映射
      *

+ 1 - 1
teacher-serve/src/main/java/com/example/controller/admin/adminController.java → teacher-serve/src/main/java/com/example/controller/admin/AdminController.java

@@ -1,4 +1,4 @@
 package com.example.controller.admin;
 
-public class adminController {
+public class AdminController {
 }

+ 61 - 0
teacher-serve/src/main/java/com/example/controller/user/UserController.java

@@ -0,0 +1,61 @@
+package com.example.controller.user;
+
+import com.example.constant.JwtClaimsConstant;
+import com.example.dto.UserLoginDTO;
+import com.example.entity.User;
+import com.example.properties.JwtProperties;
+import com.example.result.Result;
+import com.example.service.UserService;
+import com.example.utils.JwtUtil;
+import com.example.vo.UserLoginVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+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.HashMap;
+import java.util.Map;
+
+/**
+ * 账户管理
+ */
+@RestController
+@Slf4j
+@Api(tags = "账户相关接口")
+@RequestMapping("/user")
+public class UserController {
+    @Autowired
+    private UserService userService;
+    @Autowired
+    private JwtProperties jwtProperties;
+
+    /**
+     * 老师登录
+     *
+     * @param userLoginDTO
+     * @return
+     */
+    @PostMapping("/login")
+    @ApiOperation(value = "老师登录")
+    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
+        log.info("老师登录:{}", userLoginDTO);
+        User user = userService.login(userLoginDTO);
+        //登录成功后,生成jwt令牌
+        Map<String, Object> claims = new HashMap<>();
+        claims.put(JwtClaimsConstant.USER_ID, user.getId());
+        String token = JwtUtil.createJWT(
+                jwtProperties.getAdminSecretKey(),
+                jwtProperties.getAdminTtl(),
+                claims);
+        UserLoginVO userLoginVO = UserLoginVO.builder()
+                .id(user.getId())
+                .userName(user.getUserName())
+                .name(user.getName())
+                .token(token).build();
+        return Result.success(userLoginVO);
+    }
+}

+ 16 - 0
teacher-serve/src/main/java/com/example/mapper/UserMapper.java

@@ -0,0 +1,16 @@
+package com.example.mapper;
+
+import com.example.entity.User;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+@Mapper
+public interface UserMapper {
+    /**
+     * 根据用户名查询密码
+     * @param username
+     * @return
+     */
+    @Select("select * from teacherteam_system.user where username = #{username}")
+    User getByUsername(String username);
+}

+ 8 - 0
teacher-serve/src/main/java/com/example/service/UserService.java

@@ -0,0 +1,8 @@
+package com.example.service;
+
+import com.example.dto.UserLoginDTO;
+import com.example.entity.User;
+
+public interface UserService {
+    User login(UserLoginDTO userLoginDTO);
+}

+ 57 - 0
teacher-serve/src/main/java/com/example/service/impl/UserServiceImpl.java

@@ -0,0 +1,57 @@
+package com.example.service.impl;
+
+import com.example.constant.MessageConstant;
+import com.example.constant.StatusConstant;
+import com.example.dto.UserLoginDTO;
+import com.example.entity.User;
+import com.example.exception.AccountNotFoundException;
+import com.example.exception.PasswordErrorException;
+import com.example.mapper.UserMapper;
+import com.example.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.DigestUtils;
+
+import javax.security.auth.login.AccountLockedException;
+
+@Service
+public class UserServiceImpl implements UserService {
+    @Autowired
+    private UserMapper userMapper;
+
+    /**
+     * 老师登录
+     *
+     * @param userLoginDTO
+     * @return
+     */
+    @Override
+    public User login(UserLoginDTO userLoginDTO) {
+
+        String username = userLoginDTO.getUsername();
+        String password = userLoginDTO.getPassword();
+
+        //根据用户用户名查询密码
+        User user = userMapper.getByUsername(username);
+        //2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)
+        if (user == null) {
+            //账号不存在
+            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
+        }
+        //密码比对
+        // 对前端传过来的明文密码加密处理
+        //password = DigestUtils.md5DigestAsHex(password.getBytes());
+        if (!password.equals(user.getPassword())) {
+            //密码错误
+            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
+        }
+
+        if (user.getStatus() == StatusConstant.DISABLE) {
+            //账号被锁定
+            throw new AccountNotFoundException(MessageConstant.ACCOUNT_LOCKED);
+        }
+//3、返回实体对象
+        return user;
+
+    }
+    }

+ 2 - 1
teacher-serve/src/main/resources/application.yml

@@ -28,7 +28,8 @@ logging:
         service: info
         controller: info
 
-sky:
+teacher:
+
   jwt:
     # 设置jwt签名加密时使用的秘钥
     admin-secret-key: teacherTeam