Selaa lähdekoodia

增加鉴权,用户登录

ting.yin 3 vuotta sitten
vanhempi
commit
fcbe0687ce

+ 16 - 0
src/main/java/cn/com/qmth/print/manage/config/PmConstants.java

@@ -0,0 +1,16 @@
+package cn.com.qmth.print.manage.config;
+
+import java.nio.charset.Charset;
+
+public interface PmConstants {
+	
+	public static String USER_SESSION_KEY_PREFIX="$USER_SESSION_";
+	
+	public static final long SYS_ROOT_ORG_ID=-1L;
+	
+    public static final String CHARSET_NAME = "UTF-8";
+
+    public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
+    
+    public static final String MD5 = "MD5";
+}

+ 3 - 0
src/main/java/cn/com/qmth/print/manage/controller/BreakRecordController.java

@@ -10,10 +10,13 @@ import org.springframework.web.bind.annotation.RestController;
 import cn.com.qmth.print.manage.service.BreakRecordService;
 import cn.com.qmth.print.manage.service.query.RecordQuery;
 
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/exam/break")
+@Aac(strict = BOOL.FALSE, auth = BOOL.TRUE)
 public class BreakRecordController {
 
     @Resource

+ 3 - 0
src/main/java/cn/com/qmth/print/manage/controller/CheckRecordController.java

@@ -10,10 +10,13 @@ import org.springframework.web.bind.annotation.RestController;
 import cn.com.qmth.print.manage.service.CheckRecordService;
 import cn.com.qmth.print.manage.service.query.RecordQuery;
 
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/exam/check")
+@Aac(strict = BOOL.FALSE, auth = BOOL.TRUE)
 public class CheckRecordController {
 
     @Resource

+ 12 - 7
src/main/java/cn/com/qmth/print/manage/controller/ConditionController.java

@@ -4,7 +4,6 @@ import cn.com.qmth.print.manage.entity.ExamStudentEntity;
 import cn.com.qmth.print.manage.enums.GroupType;
 import cn.com.qmth.print.manage.enums.RecordStatus;
 import cn.com.qmth.print.manage.service.ExamStudentService;
-import cn.com.qmth.print.manage.utils.result.ResultUtil;
 import com.qmth.boot.api.constant.ApiConstant;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
@@ -50,7 +49,8 @@ public class ConditionController {
     /**
      * 准考证列表
      *
-     * @param examId 批次ID
+     * @param examId
+     *            批次ID
      * @return
      */
     @RequestMapping(value = "/list_exam_number", method = RequestMethod.POST)
@@ -58,7 +58,8 @@ public class ConditionController {
         List<ExamStudentEntity> studentEntityList = examStudentService.listByExamId(examId);
         List<String> examNumberList = new ArrayList<>();
         if (!CollectionUtils.isEmpty(studentEntityList)) {
-            examNumberList = studentEntityList.stream().map(m -> m.getExamNumber()).distinct().collect(Collectors.toList());
+            examNumberList = studentEntityList.stream().map(m -> m.getExamNumber()).distinct()
+                    .collect(Collectors.toList());
         }
         return examNumberList;
     }
@@ -66,7 +67,8 @@ public class ConditionController {
     /**
      * 科目列表
      *
-     * @param examId 批次ID
+     * @param examId
+     *            批次ID
      * @return
      */
     @RequestMapping(value = "/list_course_code", method = RequestMethod.POST)
@@ -74,7 +76,8 @@ public class ConditionController {
         List<ExamStudentEntity> studentEntityList = examStudentService.listByExamId(examId);
         List<String> courseCodeList = new ArrayList<>();
         if (!CollectionUtils.isEmpty(studentEntityList)) {
-            courseCodeList = studentEntityList.stream().map(m -> m.getCourseCode()).distinct().collect(Collectors.toList());
+            courseCodeList = studentEntityList.stream().map(m -> m.getCourseCode()).distinct()
+                    .collect(Collectors.toList());
         }
         return courseCodeList;
     }
@@ -82,7 +85,8 @@ public class ConditionController {
     /**
      * 考点列表
      *
-     * @param examId 批次ID
+     * @param examId
+     *            批次ID
      * @return
      */
     @RequestMapping(value = "/list_exam_site", method = RequestMethod.POST)
@@ -98,7 +102,8 @@ public class ConditionController {
     /**
      * 考场列表
      *
-     * @param examId 批次ID
+     * @param examId
+     *            批次ID
      * @return
      */
     @RequestMapping(value = "/list_exam_room", method = RequestMethod.POST)

+ 5 - 0
src/main/java/cn/com/qmth/print/manage/controller/OrgController.java

@@ -2,7 +2,11 @@ package cn.com.qmth.print.manage.controller;
 
 import cn.com.qmth.print.manage.service.OrgService;
 import cn.com.qmth.print.manage.service.query.OrgQuery;
+
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -11,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/org")
+@Aac(strict = BOOL.FALSE, auth = BOOL.TRUE)
 public class OrgController {
 
     @Autowired

+ 15 - 2
src/main/java/cn/com/qmth/print/manage/controller/SysController.java

@@ -1,11 +1,16 @@
 package cn.com.qmth.print.manage.controller;
 
-import com.qmth.boot.api.constant.ApiConstant;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import cn.com.qmth.print.manage.service.AuthService;
+
+import com.qmth.boot.api.constant.ApiConstant;
+
 /**
  * @Date: 2021/11/17.
  */
@@ -13,6 +18,9 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX)
 public class SysController {
 
+    @Autowired
+    private AuthService authService;
+
     /**
      * 登录
      *
@@ -22,6 +30,11 @@ public class SysController {
      */
     @RequestMapping(value = "/login", method = RequestMethod.POST)
     public Object login(@RequestParam String loginName, @RequestParam String password) {
-        return null;
+        return authService.login(loginName, password);
+    }
+
+    @PostMapping("/logout")
+    public void logout() {
+        authService.logout();
     }
 }

+ 5 - 1
src/main/java/cn/com/qmth/print/manage/controller/UserController.java

@@ -3,8 +3,12 @@ package cn.com.qmth.print.manage.controller;
 import cn.com.qmth.print.manage.entity.UserEntity;
 import cn.com.qmth.print.manage.service.UserService;
 import cn.com.qmth.print.manage.service.query.UserQuery;
+
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.exception.StatusException;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -16,6 +20,7 @@ import org.springframework.web.bind.annotation.RestController;
  */
 @RestController
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/user")
+@Aac(strict = BOOL.FALSE, auth = BOOL.TRUE)
 public class UserController {
 
     @Autowired
@@ -42,7 +47,6 @@ public class UserController {
         return userService.listByEnable();
     }
 
-
     /**
      * 新增/修改用户
      *

+ 64 - 0
src/main/java/cn/com/qmth/print/manage/enums/HttpServletRequestAttribute.java

@@ -0,0 +1,64 @@
+package cn.com.qmth.print.manage.enums;
+
+/**
+ * servlet请求属性
+ *
+ */
+public enum HttpServletRequestAttribute {
+
+    /**
+     * 请求映射
+     */
+    $_MAPPING,
+
+    /**
+     * API 信息
+     */
+    $_API_INFO,
+
+    /**
+     * http status恒为200
+     */
+    $_ALWAYS_OK,
+
+    /**
+     * 接入用户
+     */
+    $_ACCESS_USER,
+
+    /**
+     * 自定义顺序锁
+     */
+    $_CUSTOM_SEQUENCE_LOCK,
+
+    /**
+     * 已鉴权(其他拦截器处理)
+     */
+    $_AUTHORIZED_BY_OTHER_INTERCEPTOR,
+
+    /**
+     * 企业顶级机构(对外服务接口)
+     */
+    $_ENTERPRISE_ROOT_ORG_ID,
+
+    /**
+     * 接口调用异常
+     */
+    $_EXCEPTION_HAPPENED,
+
+    /**
+     * METRICS Timer context
+     */
+    $_METRICS_TIMER_CTX,
+
+    /**
+     * ApiStatisticInterceptor 开始时间
+     */
+    API_STATISTIC_INTERCEPTOR_START_TIME,
+
+    /**
+     * 数据权限
+     */
+    $_USER_DATA_RULE
+
+}

+ 57 - 0
src/main/java/cn/com/qmth/print/manage/service/AuthService.java

@@ -0,0 +1,57 @@
+package cn.com.qmth.print.manage.service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.vo.UserVo;
+
+import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.core.security.annotation.AuthorizationComponent;
+import com.qmth.boot.core.security.service.AuthorizationService;
+import com.qmth.boot.tools.signature.SignatureType;
+
+@AuthorizationComponent(prefix = ApiConstant.DEFAULT_URI_PREFIX)
+public class AuthService implements AuthorizationService<PmSession> {
+
+    private Map<String, String> userMap = new HashMap<String, String>();
+
+    @Autowired
+    private UserService userService;
+
+    public UserVo login(String loginName, String password) {
+        UserEntity entity = userService.findByLoginName(loginName);
+        if (entity == null) {
+            throw new StatusException("用户不存在");
+        }
+        if (!entity.getPassword().equals(password)) {
+            throw new StatusException("密码不正确");
+        }
+        if (!entity.isEnable()) {
+            throw new StatusException("用户被禁用");
+        }
+        UserVo user = new UserVo(entity);
+        String token = RandomStringUtils.randomAlphanumeric(32);
+        userMap.put(user.buildKey(), token);
+        user.setToken(token);
+        return user;
+    }
+
+    public void logout() {
+        // userMap.remove(identity);
+    }
+
+    @Override
+    public PmSession findByIdentity(String identity, SignatureType type, String path) {
+        return new PmSession(identity, userMap.get(identity));
+    }
+
+    @Override
+    public boolean hasPermission(PmSession accessEntity, String path) {
+        return true;
+    }
+}

+ 38 - 0
src/main/java/cn/com/qmth/print/manage/service/PmSession.java

@@ -0,0 +1,38 @@
+package cn.com.qmth.print.manage.service;
+
+import com.qmth.boot.core.security.model.AccessEntity;
+
+public class PmSession implements AccessEntity {
+
+    private String identity;
+
+    private String token;
+
+    public PmSession(String identity, String token) {
+        this.identity = identity;
+        this.token = token;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setIdentity(String identity) {
+        this.identity = identity;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    @Override
+    public String getIdentity() {
+        return identity;
+    }
+
+    @Override
+    public String getSecret() {
+        return token;
+    }
+
+}

+ 3 - 0
src/main/java/cn/com/qmth/print/manage/service/UserService.java

@@ -2,6 +2,7 @@ package cn.com.qmth.print.manage.service;
 
 import cn.com.qmth.print.manage.entity.UserEntity;
 import cn.com.qmth.print.manage.service.query.UserQuery;
+
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.IService;
 
@@ -12,4 +13,6 @@ public interface UserService extends IService<UserEntity> {
     IPage<UserEntity> pageQuery(UserQuery query);
 
     List<UserEntity> listByEnable();
+
+    UserEntity findByLoginName(String loginName);
 }

+ 9 - 0
src/main/java/cn/com/qmth/print/manage/service/impl/UserServiceImpl.java

@@ -4,10 +4,12 @@ import cn.com.qmth.print.manage.dao.UserDao;
 import cn.com.qmth.print.manage.entity.UserEntity;
 import cn.com.qmth.print.manage.service.UserService;
 import cn.com.qmth.print.manage.service.query.UserQuery;
+
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
@@ -39,4 +41,11 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
         queryWrapper.lambda().eq(UserEntity::isEnable, true);
         return this.list(queryWrapper);
     }
+
+    @Override
+    public UserEntity findByLoginName(String loginName) {
+        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
+        queryWrapper.lambda().eq(UserEntity::getLoginName, loginName);
+        return this.getOne(queryWrapper);
+    }
 }

+ 109 - 0
src/main/java/cn/com/qmth/print/manage/vo/UserVo.java

@@ -0,0 +1,109 @@
+package cn.com.qmth.print.manage.vo;
+
+import java.io.Serializable;
+
+import cn.com.qmth.print.manage.config.PmConstants;
+import cn.com.qmth.print.manage.entity.UserEntity;
+import cn.com.qmth.print.manage.enums.RoleMeta;
+
+/**
+ * 用户
+ *
+ */
+public class UserVo implements Serializable {
+
+    private static final long serialVersionUID = 4759774295648544266L;
+
+    /**
+     * 全局唯一用户标识符
+     */
+    private String identity;
+
+    /**
+     * 用户ID
+     */
+    private Long userId;
+
+    /**
+     * 显示名
+     */
+    private String loginName;
+
+    /**
+     * 角色集合
+     */
+    private RoleMeta role;
+
+    /**
+     * 鉴权token
+     */
+    private String token;
+
+    private boolean enable;
+
+    public UserVo(UserEntity entity) {
+        this.loginName = entity.getLoginName();
+        this.userId = entity.getId();
+        this.role = entity.getRole();
+        this.enable = entity.isEnable();
+    }
+
+    /**
+     * 构建key
+     *
+     * @return
+     */
+    public String buildKey() {
+        this.identity = new StringBuilder().append(PmConstants.USER_SESSION_KEY_PREFIX).append(userId).toString();
+        return this.identity;
+    }
+
+    public String getIdentity() {
+        return identity;
+    }
+
+    public void setIdentity(String identity) {
+        this.identity = identity;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public String getLoginName() {
+        return loginName;
+    }
+
+    public void setLoginName(String loginName) {
+        this.loginName = loginName;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+
+    public boolean isEnable() {
+        return enable;
+    }
+
+    public void setEnable(boolean enable) {
+        this.enable = enable;
+    }
+
+    public RoleMeta getRole() {
+        return role;
+    }
+
+    public void setRole(RoleMeta role) {
+        this.role = role;
+    }
+
+}