deason 7 سال پیش
والد
کامیت
fc860fc339

+ 4 - 7
src/main/java/cn/com/qmth/examcloud/app/SwaggerConfig.java

@@ -7,23 +7,17 @@
 
 package cn.com.qmth.examcloud.app;
 
-import com.google.common.collect.Lists;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import springfox.documentation.builders.ApiInfoBuilder;
-import springfox.documentation.builders.ParameterBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
-import springfox.documentation.schema.ModelRef;
 import springfox.documentation.service.ApiInfo;
-import springfox.documentation.service.Parameter;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
-import java.util.List;
-
 @Configuration
 @EnableSwagger2
 public class SwaggerConfig {
@@ -33,6 +27,7 @@ public class SwaggerConfig {
         //定义要生成API文档的基础包
         String basePackage = "cn.com.qmth.examcloud.app.controller";
         //全局参数
+        /*
         List<Parameter> parameters = Lists.newArrayList();
         ParameterBuilder key = new ParameterBuilder();
         key.name("key").modelRef(new ModelRef("String")).parameterType("header").build();
@@ -40,6 +35,7 @@ public class SwaggerConfig {
         ParameterBuilder token = new ParameterBuilder();
         token.name("token").modelRef(new ModelRef("String")).parameterType("header").build();
         parameters.add(token.build());
+        */
         return new Docket(DocumentationType.SWAGGER_2)
                 .groupName("Version 1.0")
                 .apiInfo(buildApiInfo())
@@ -48,7 +44,8 @@ public class SwaggerConfig {
                 //.apis(RequestHandlerSelectors.basePackage(basePackage))
                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                 .paths(PathSelectors.any())
-                .build().globalOperationParameters(parameters);
+                .build();
+        //.build().globalOperationParameters(parameters);
     }
 
     public ApiInfo buildApiInfo() {

+ 11 - 7
src/main/java/cn/com/qmth/examcloud/app/controller/v1/UserAuthRestController.java

@@ -12,9 +12,7 @@ import cn.com.qmth.examcloud.app.service.UserAuthService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 /**
  * 认证中心业务服务接口
@@ -28,10 +26,16 @@ public class UserAuthRestController {
     @Autowired
     private UserAuthService userAuthService;
 
-    @ApiOperation(value = "获取XXX列表")
-    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
-    public Result list() {
-        return new Result().error();
+    @ApiOperation(value = "登录接口")
+    @RequestMapping(value = "user/login", method = {RequestMethod.GET, RequestMethod.POST})
+    public Result login(@RequestParam String account, @RequestParam String password, @RequestParam String rootOrgId) throws Exception {
+        return userAuthService.login(account, password, rootOrgId);
+    }
+
+    @ApiOperation(value = "修改密码接口")
+    @RequestMapping(value = "user/update/password", method = {RequestMethod.GET, RequestMethod.POST})
+    public Result updatePassword(@RequestHeader String key, @RequestHeader String token, @RequestParam String userId, @RequestParam String password) throws Exception {
+        return userAuthService.updatePassword(key, token, userId, password);
     }
 
 }

+ 11 - 0
src/main/java/cn/com/qmth/examcloud/app/model/Constants.java

@@ -9,8 +9,19 @@ package cn.com.qmth.examcloud.app.model;
 
 public interface Constants {
 
+    String CHARSET_JSON_UTF8 = "application/json; charset=utf-8";
+
     String PARAM_KEY = "key";
 
     String PARAM_TOKEN = "token";
 
+    String ERROR_500 = "\"code\":\"500\"";
+
+    String ERROR_403 = "\"code\":\"403\"";
+
+    /* 常用状态码 */
+    String CODE_200 = "200";//成功
+    String CODE_500 = "500";//失败
+    String CODE_403 = "403";//认证失败
+
 }

+ 33 - 0
src/main/java/cn/com/qmth/examcloud/app/model/ResBody.java

@@ -0,0 +1,33 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-07-17 16:14:47.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.app.model;
+
+import java.io.Serializable;
+
+public class ResBody implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private String code;//状态码
+    private String desc;//描述信息
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public void setDesc(String desc) {
+        this.desc = desc;
+    }
+
+}

+ 9 - 10
src/main/java/cn/com/qmth/examcloud/app/model/Result.java

@@ -18,10 +18,6 @@ public class Result<T> implements Serializable {
     private String desc;//描述信息
     private T data;//数据
 
-    /* 常用状态码 */
-    public static String CODE_200 = "200";//成功
-    public static String CODE_500 = "500";//失败
-    public static String CODE_403 = "403";//认证失败
     public static String DESC_SUCCESS = "操作成功";
     public static String DESC_FAIL = "操作失败";
 
@@ -39,14 +35,14 @@ public class Result<T> implements Serializable {
      * 成功结果
      */
     public Result success(T data) {
-        this.code = CODE_200;
+        this.code = Constants.CODE_200;
         this.desc = DESC_SUCCESS;
         this.data = data;
         return this;
     }
 
     public Result success() {
-        this.code = CODE_200;
+        this.code = Constants.CODE_200;
         this.desc = DESC_SUCCESS;
         return this;
     }
@@ -55,19 +51,22 @@ public class Result<T> implements Serializable {
      * 错误结果
      */
     public Result error(String desc) {
-        this.code = CODE_500;
+        this.code = Constants.CODE_500;
         this.desc = desc;
         return this;
     }
 
     public Result error() {
-        this.code = CODE_500;
+        this.code = Constants.CODE_500;
         this.desc = DESC_FAIL;
         return this;
     }
 
-    public Result tokenError() {
-        this.code = CODE_403;
+    /**
+     * 认证错误
+     */
+    public Result authError() {
+        this.code = Constants.CODE_403;
         this.desc = "No login,Token is invalid.";
         return this;
     }

+ 5 - 5
src/main/java/cn/com/qmth/examcloud/app/service/PropertyService.java

@@ -18,15 +18,15 @@ import org.springframework.stereotype.Component;
 @Component
 public class PropertyService {
     private static Logger log = LoggerFactory.getLogger(PropertyService.class);
-    @Value("{examcloud.exam.admin.url}")
+    @Value("${examcloud.exam.admin.url}")
     private String examAdminUrl;
-    @Value("{examcloud.online.exam.url}")
+    @Value("${examcloud.online.exam.url}")
     private String onlineExamUrl;
-    @Value("{examcloud.question.pool.url}")
+    @Value("${examcloud.question.pool.url}")
     private String questionPoolUrl;
-    @Value("{examcloud.user.auth.url}")
+    @Value("${examcloud.user.auth.url}")
     private String userAuthUrl;
-    @Value("{examcloud.upyun.url}")
+    @Value("${examcloud.upyun.url}")
     private String upYunUrl;
 
     public String getExamAdminUrl() {

+ 67 - 3
src/main/java/cn/com/qmth/examcloud/app/service/UserAuthService.java

@@ -7,12 +7,21 @@
 
 package cn.com.qmth.examcloud.app.service;
 
+import cn.com.qmth.examcloud.app.model.Constants;
+import cn.com.qmth.examcloud.app.model.ResBody;
 import cn.com.qmth.examcloud.app.model.Result;
+import cn.com.qmth.examcloud.app.utils.HttpBuilder;
+import cn.com.qmth.examcloud.app.utils.HttpUtils;
+import cn.com.qmth.examcloud.app.utils.JsonMapper;
+import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * 认证中心业务服务接口
  */
@@ -22,9 +31,64 @@ public class UserAuthService {
     @Autowired
     private PropertyService propertyService;
 
-    public Result init() throws Exception {
-        log.debug("init...");
-        return new Result().success();
+    public Result login(String account, String password, String rootOrgId) throws Exception {
+        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/auth/login";
+        //封装请求参数
+        Map<String, String> params = new HashMap<>();
+        params.put("accountValue", account);
+        params.put("password", password);
+        params.put("accountType", "COMMON_LOGIN_NAME");
+        params.put("rootOrgId", rootOrgId);
+        params.put("domain", "");
+        String json = new JsonMapper().toJson(params);
+        RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .post(formBody)
+                .build();
+        //执行请求
+        Response response = HttpBuilder.client.getInstance().newCall(request).execute();
+        String bodyStr = response.body().string();
+        if (response.isSuccessful()) {
+            return new Result().success(bodyStr);
+        } else {
+            log.debug(bodyStr);
+            ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
+            if (body != null && body.getCode() != null) {
+                return new Result().error(body.getDesc());
+            }
+            return new Result().error(bodyStr);
+        }
+    }
+
+    public Result updatePassword(String key, String token, String userId, String password) throws Exception {
+        final String requestUrl = propertyService.getUserAuthUrl() + "/api/ecs_core/user/password";
+        //封装请求参数
+        RequestBody formBody = new FormBody.Builder()
+                .add("userId", userId)
+                .add("password", password)
+                .build();
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .put(formBody)
+                .addHeader("key", key)
+                .addHeader("token", token)
+                .build();
+        //执行请求
+        return HttpUtils.call(request);
+    }
+
+    public Result demo(String key, String token, String userId, String password) throws Exception {
+        final String requestUrl = String.format(propertyService.getUserAuthUrl() + "/api/ecs_core/user/password?userId=%s&password=%s", userId, password);
+        //封装请求参数
+        Request request = new Request.Builder()
+                .url(requestUrl)
+                .get()
+                .addHeader("key", key)
+                .addHeader("token", token)
+                .build();
+        //执行请求
+        return HttpUtils.call(request);
     }
 
 }

+ 39 - 0
src/main/java/cn/com/qmth/examcloud/app/utils/HttpUtils.java

@@ -0,0 +1,39 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-07-17 17:32:00.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.app.utils;
+
+import cn.com.qmth.examcloud.app.model.Constants;
+import cn.com.qmth.examcloud.app.model.ResBody;
+import cn.com.qmth.examcloud.app.model.Result;
+import okhttp3.Request;
+import okhttp3.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpUtils {
+    private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
+
+    public static Result call(Request request) throws Exception {
+        Response response = HttpBuilder.client.getInstance().newCall(request).execute();
+        String bodyStr = response.body().string();
+        log.debug("Response is " + bodyStr);
+        if (response.isSuccessful()) {
+            return new Result().success();
+        } else {
+            ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
+            if (body != null && body.getCode() != null) {
+                if (Constants.CODE_403.equals(body.getCode())) {
+                    return new Result().authError();
+                }
+                return new Result().error(body.getDesc());
+            }
+            return new Result().error(bodyStr);
+        }
+    }
+
+}