westt %!s(int64=2) %!d(string=hai) anos
pai
achega
e919f1a4de

+ 2 - 5
src/main/java/com/example/onlinejudge/controller/UserController.java

@@ -2,8 +2,6 @@ package com.example.onlinejudge.controller;
 
 import cn.dev33.satoken.stp.StpUtil;
 import cn.dev33.satoken.util.SaResult;
-import com.example.onlinejudge.bean.User;
-import com.example.onlinejudge.mapper.UserMapper;
 import com.example.onlinejudge.service.UserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -41,13 +39,12 @@ public class UserController {
     @RequestMapping("/api/isLogin")
     @ResponseBody
     public String isLogin() {
-        return "当前会话是否登录:" + StpUtil.isLogin();
+        return "当前会话登录了";
     }
 
     /**
      * 测试注销
-     *
-     * @return
+     * @return LinkedHashMap
      */
     @RequestMapping("/api/logout")
     @ResponseBody

+ 7 - 1
src/main/java/com/example/onlinejudge/controller/exception/GlobalExceptionHandler.java

@@ -10,11 +10,17 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
  */
 @RestControllerAdvice
 public class GlobalExceptionHandler {
+    /**
+     * 拦截未登录的用户进行非法权限操作
+     * @param e NotLoginException
+     * @return 返回未登录错误
+     */
     @ExceptionHandler(NotLoginException.class)
     public SaResult handlerException(NotLoginException e) {
         e.printStackTrace();
         return SaResult
                 .error(e.getMessage())
-                .set("error_message", "failed");
+                .set("error_message", "not_login")
+                .setCode(401);
     }
 }

+ 1 - 0
src/main/java/com/example/onlinejudge/mapper/UserMapper.java

@@ -8,4 +8,5 @@ import org.apache.ibatis.annotations.Select;
 public interface UserMapper {
     User selectUserById(Integer id);
     User selectUserByUsernameAndPassword(String username, String password);
+    boolean insertUser(String username, String password);
 }

+ 0 - 1
src/main/java/com/example/onlinejudge/service/UserService.java

@@ -4,5 +4,4 @@ import cn.dev33.satoken.util.SaResult;
 
 public interface UserService {
     SaResult login(String username, String password);
-    String register(String username, String password, String confirmedPassword);
 }

+ 13 - 5
src/main/java/com/example/onlinejudge/service/impl/UserServiceImpl.java

@@ -1,5 +1,6 @@
 package com.example.onlinejudge.service.impl;
 
+import cn.dev33.satoken.stp.SaTokenInfo;
 import cn.dev33.satoken.stp.StpUtil;
 import cn.dev33.satoken.util.SaResult;
 import com.example.onlinejudge.bean.User;
@@ -20,22 +21,29 @@ public class UserServiceImpl implements UserService {
 
     /**
      * 用户登录服务
+     *
      * @param username 用户名
      * @param password 密码
      * @return 登录结果
      */
     @Override
     public SaResult login(String username, String password) {
+        if (username == null || password == null) {
+            return SaResult.error("failed").set("error_message", "用户名或密码不能为空");
+        }
+        username = username.trim();
+        if (username.length() == 0) {
+            return SaResult.error("failed").set("error_message", "用户名长度不能为0");
+        }
+        if (password.length() == 0) {
+            return SaResult.error("failed").set("error_message", "密码长度不能为0");
+        }
         User user = userMapper.selectUserByUsernameAndPassword(username, password);
         if (user != null) {
             StpUtil.login(user.getId());
             return SaResult.ok("success").set("error_message", "success");
         }
-        return SaResult.ok("failed").set("error_message", "用户名或密码错误");
+        return SaResult.error("failed").set("error_message", "用户名或密码错误").setCode(401);
     }
 
-    @Override
-    public String register(String username, String password, String confirmedPassword) {
-        return null;
-    }
 }

+ 3 - 0
src/main/resources/mapper/userMapper.xml

@@ -9,4 +9,7 @@
     <select id="selectUserByUsernameAndPassword" resultType="com.example.onlinejudge.bean.User">
         SELECT * FROM t_user WHERE username = #{param1} and password = #{param2}
     </select>
+    <insert id="insertUser">
+        INSERT INTO t_user VALUES(NULL, #{param1}, #{param2}, NOW())
+    </insert>
 </mapper>