From f4d00f349f87b25b6d878cdd45bb593d1957c588 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 25 Mar 2020 15:51:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Swagger;=E5=BC=95=E5=85=A5Res?= =?UTF-8?q?tful=20API=E6=A0=87=E5=87=86;Exception=E5=BD=92=E7=B1=BB;?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=AE=A1=E7=90=86=E5=AD=90=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=AE=8C=E5=96=84;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 22 ++++- .../QuickJSONRespond.java | 13 ++- .../auth/ASEAccessDeniedHandler.java | 4 +- .../auth/ASEAuthenticationEntryPoint.java | 6 +- .../auth/ASEAuthenticationFailureHandler.java | 5 +- .../auth/ASEAuthenticationSuccessHandler.java | 5 +- .../datamanager/DataExcelGenerator.java | 2 +- .../datamanager/DataExcelReader.java | 1 + .../ase/component/json/model/JsonableTag.java | 41 ++++++++ .../json/model/JsonableUidGetter.java | 6 ++ .../ase/configure/ASESwaggerConfigure.java | 57 +++++++++++ .../configure/CustomWebSecurityConfig.java | 8 +- .../ase/controller/ASEControllerAdvice.java | 32 ++++++- .../ase/controller/ASEErrorController.java | 12 +-- .../ase/controller/LoginController.java | 13 +-- .../ase/controller/PermissionController.java | 94 +++++-------------- .../ase/controller/RegisterController.java | 2 +- .../exception/DataFileNotFoundException.java | 15 --- .../notfound/DataFileNotFoundException.java | 15 +++ .../exception/notfound/NotFoundException.java | 12 +++ .../notfound/TagNotFoundException.java | 11 +++ .../{ => notfound}/UserNotFoundException.java | 10 +- .../ase/service/ASEUserDetailsService.java | 3 +- .../codesdream/ase/service/UserService.java | 2 +- src/main/resources/application-dev.properties | 23 +++++ 25 files changed, 275 insertions(+), 139 deletions(-) rename src/main/java/com/codesdream/ase/component/{datamanager => api}/QuickJSONRespond.java (85%) create mode 100644 src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java create mode 100644 src/main/java/com/codesdream/ase/component/json/model/JsonableUidGetter.java create mode 100644 src/main/java/com/codesdream/ase/configure/ASESwaggerConfigure.java delete mode 100644 src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java create mode 100644 src/main/java/com/codesdream/ase/exception/notfound/DataFileNotFoundException.java create mode 100644 src/main/java/com/codesdream/ase/exception/notfound/NotFoundException.java create mode 100644 src/main/java/com/codesdream/ase/exception/notfound/TagNotFoundException.java rename src/main/java/com/codesdream/ase/exception/{ => notfound}/UserNotFoundException.java (56%) create mode 100644 src/main/resources/application-dev.properties diff --git a/pom.xml b/pom.xml index 53a9200..900747a 100644 --- a/pom.xml +++ b/pom.xml @@ -139,11 +139,12 @@ - org.mariadb.jdbc - mariadb-java-client - 2.5.4 - + org.mariadb.jdbc + mariadb-java-client + 2.5.4 + + com.h2database h2 @@ -163,6 +164,19 @@ 3.2.0 + + + io.springfox + springfox-swagger2 + 2.9.2 + + + + io.springfox + springfox-swagger-ui + 2.9.2 + + diff --git a/src/main/java/com/codesdream/ase/component/datamanager/QuickJSONRespond.java b/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java similarity index 85% rename from src/main/java/com/codesdream/ase/component/datamanager/QuickJSONRespond.java rename to src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java index 0838a41..dfe654d 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/QuickJSONRespond.java +++ b/src/main/java/com/codesdream/ase/component/api/QuickJSONRespond.java @@ -1,5 +1,6 @@ -package com.codesdream.ase.component.datamanager; +package com.codesdream.ase.component.api; +import com.codesdream.ase.component.datamanager.JSONParameter; import com.codesdream.ase.component.json.respond.EmptyDataObjectRespond; import com.codesdream.ase.component.json.respond.JSONBaseRespondObject; import org.springframework.stereotype.Component; @@ -36,6 +37,11 @@ public class QuickJSONRespond { return getJSONStandardRespond(404, "Not Found", info); } + // 获得标准的JSON响应字符串返回(404状态) + public String getRespond404(String info, Object object){ + return getJSONStandardRespond(404, "Not Found", info, object); + } + // 获得标准的JSON响应字符串返回(500状态) public String getRespond500(String info){ return getJSONStandardRespond(500, "Internal Server Error", info); @@ -76,6 +82,11 @@ public class QuickJSONRespond { return getJSONStandardRespond(400, "Bad Request", info); } + // 获得标准的JSON响应字符串返回(404状态) + public String getRespond400(String info, Object object){ + return getJSONStandardRespond(400, "Bad Request", info, object); + } + // 获得标准的JSON响应字符串返回(400状态) public String getRespond409(String info){ return getJSONStandardRespond(409, "Conflict", info); diff --git a/src/main/java/com/codesdream/ase/component/auth/ASEAccessDeniedHandler.java b/src/main/java/com/codesdream/ase/component/auth/ASEAccessDeniedHandler.java index 3ab64a1..0c3350e 100644 --- a/src/main/java/com/codesdream/ase/component/auth/ASEAccessDeniedHandler.java +++ b/src/main/java/com/codesdream/ase/component/auth/ASEAccessDeniedHandler.java @@ -1,8 +1,6 @@ package com.codesdream.ase.component.auth; -import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; -import com.codesdream.ase.component.json.respond.UserLoginCheckerJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import lombok.extern.slf4j.Slf4j; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.web.access.AccessDeniedHandler; diff --git a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationEntryPoint.java b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationEntryPoint.java index a72840e..296cc2b 100644 --- a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationEntryPoint.java +++ b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationEntryPoint.java @@ -1,16 +1,12 @@ package com.codesdream.ase.component.auth; -import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; -import com.codesdream.ase.component.json.respond.JSONBaseRespondObject; -import com.codesdream.ase.component.json.respond.UserLoginCheckerJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; diff --git a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationFailureHandler.java b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationFailureHandler.java index b3a9dae..d90a784 100644 --- a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationFailureHandler.java +++ b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationFailureHandler.java @@ -1,16 +1,13 @@ package com.codesdream.ase.component.auth; -import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.respond.ErrorInfoJSONRespond; -import com.codesdream.ase.component.json.respond.UserLoginCheckerJSONRespond; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; diff --git a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationSuccessHandler.java b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationSuccessHandler.java index fcd62e3..f73d445 100644 --- a/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationSuccessHandler.java +++ b/src/main/java/com/codesdream/ase/component/auth/ASEAuthenticationSuccessHandler.java @@ -1,8 +1,6 @@ package com.codesdream.ase.component.auth; -import com.codesdream.ase.component.auth.JSONTokenAuthenticationToken; -import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.respond.UserLoginCheckerJSONRespond; import com.codesdream.ase.model.permission.User; @@ -19,7 +17,6 @@ import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.Optional; diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java index 1ca17b2..83c1948 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelGenerator.java @@ -1,6 +1,6 @@ package com.codesdream.ase.component.datamanager; -import com.codesdream.ase.exception.DataFileNotFoundException; +import com.codesdream.ase.exception.notfound.DataFileNotFoundException; import com.codesdream.ase.exception.DataIOException; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; diff --git a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java index ae2e2e6..f5471f2 100644 --- a/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java +++ b/src/main/java/com/codesdream/ase/component/datamanager/DataExcelReader.java @@ -2,6 +2,7 @@ package com.codesdream.ase.component.datamanager; import com.codesdream.ase.exception.*; +import com.codesdream.ase.exception.notfound.DataFileNotFoundException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java new file mode 100644 index 0000000..b5397cd --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableTag.java @@ -0,0 +1,41 @@ +package com.codesdream.ase.component.json.model; + +import com.codesdream.ase.model.permission.Tag; +import com.codesdream.ase.model.permission.User; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import net.bytebuddy.implementation.bind.annotation.DefaultMethod; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Data +@ApiModel("标签") +public class JsonableTag { + @ApiModelProperty(value = "标签id") + private Integer id = null; + @ApiModelProperty(value = "标签名", example = "系统管理员") + private String name; + @ApiModelProperty(value = "标签说明", example = "该系统的管理员") + private String description; + @ApiModelProperty(value = "用户列表", hidden = true) + private Set users = new HashSet<>(); + + public JsonableTag(){ + + } + + public JsonableTag(Tag tag){ + this.id = tag.getId(); + this.name = tag.getName(); + this.description = tag.getDescription(); + // 构建用户的ID列表 + for(User user : tag.getUsers()) { + users.add(user.getId()); + } + } + +} diff --git a/src/main/java/com/codesdream/ase/component/json/model/JsonableUidGetter.java b/src/main/java/com/codesdream/ase/component/json/model/JsonableUidGetter.java new file mode 100644 index 0000000..9eee677 --- /dev/null +++ b/src/main/java/com/codesdream/ase/component/json/model/JsonableUidGetter.java @@ -0,0 +1,6 @@ +package com.codesdream.ase.component.json.model; + +public class JsonableUidGetter { + private String checkType; + private String username; +} diff --git a/src/main/java/com/codesdream/ase/configure/ASESwaggerConfigure.java b/src/main/java/com/codesdream/ase/configure/ASESwaggerConfigure.java new file mode 100644 index 0000000..1e2061e --- /dev/null +++ b/src/main/java/com/codesdream/ase/configure/ASESwaggerConfigure.java @@ -0,0 +1,57 @@ +package com.codesdream.ase.configure; + +import com.google.common.collect.Sets; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.core.parameters.P; +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.ArrayList; +import java.util.Date; +import java.util.List; + +@Configuration +@EnableSwagger2 +public class ASESwaggerConfigure { + @Bean + public Docket createRestApi() { + + List pars = new ArrayList(); + + pars.add(new ParameterBuilder().name("username").description("真实用户名").hidden(true).order(1) + .modelRef(new ModelRef("string")).parameterType("header") + .required(false).defaultValue("u_id_88883b9e023c8824310760d8bb8b6542e5a3f16a0d67253214e01ee7ab0e96a1").build()); + pars.add(new ParameterBuilder().name("signed").description("客户端签名").hidden(true).order(2) + .modelRef(new ModelRef("string")).parameterType("header") + .required(false).defaultValue("6d4923fca4dcb51f67b85e54a23a8d763d9e02af").build()); + pars.add(new ParameterBuilder().name("timestamp").description("时间戳").hidden(true).order(3) + .modelRef(new ModelRef("string")).parameterType("header") + .required(false).defaultValue(Long.toString(new Date().getTime())).build()); + + return new Docket(DocumentationType.SWAGGER_2) + .protocols(Sets.newHashSet("http")) + .apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.codesdream.ase.controller")) + .paths(PathSelectors.any()) + .build() + .globalOperationParameters(pars); + } + + private ApiInfo apiInfo() { + return new ApiInfoBuilder() + .title("全员育人管理系统后端接口定义") + .version("0.0.1") + .description("用于对后端接口进行说明") + .build(); + } +} diff --git a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java index 96d414a..b2d3413 100644 --- a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java +++ b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java @@ -87,7 +87,13 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { "/forget/**", "/not_found/**", "/error/**", - "/login/**"); + "/login/**", + "/swagger-ui.html", + "/webjars/**", + "/swagger-resources/**", + "/v2/api-docs", + "/configuration/ui", + "/configuration/security"); } //注册自定义的UsernamePasswordAuthenticationFilter diff --git a/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java index 332f5ba..755d930 100644 --- a/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java +++ b/src/main/java/com/codesdream/ase/controller/ASEControllerAdvice.java @@ -1,7 +1,11 @@ package com.codesdream.ase.controller; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.error.ErrorResponse; import com.codesdream.ase.component.json.respond.ErrorInfoJSONRespond; +import com.codesdream.ase.exception.notfound.NotFoundException; +import com.sun.xml.bind.v2.model.annotation.Quick; +import org.apache.poi.openxml4j.opc.internal.ContentType; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationServiceException; @@ -11,21 +15,39 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.context.request.WebRequest; +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.Date; import java.util.List; @RestControllerAdvice public class ASEControllerAdvice { - @ExceptionHandler(value = {RuntimeException.class}) - public final Object handleRuntimeException(RuntimeException e, WebRequest webRequest){ + + @Resource + private QuickJSONRespond quickJSONRespond; + + @ExceptionHandler(value = {NullPointerException.class}) + public ResponseEntity handleBadRequest(Exception ex) { + + String json = quickJSONRespond.getRespond400(null, getJSONRespondObject(ex)); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(json); + } + + @ExceptionHandler(value = {NotFoundException.class}) + public ResponseEntity handleNotFound(Exception ex) { + String json = quickJSONRespond.getRespond404(null, getJSONRespondObject(ex)); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(json); + } + + private Object getJSONRespondObject(Exception ex){ ErrorInfoJSONRespond errorInfoJSONRespond = new ErrorInfoJSONRespond(); + errorInfoJSONRespond.setException(ex.getClass().getName()); + errorInfoJSONRespond.setExceptionMessage(ex.getMessage()); errorInfoJSONRespond.setDate(new Date()); - errorInfoJSONRespond.setExceptionMessage(e.getMessage()); - errorInfoJSONRespond.setException(e.getClass().getName()); return errorInfoJSONRespond; } - } diff --git a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java index ab33dc1..23d166d 100644 --- a/src/main/java/com/codesdream/ase/controller/ASEErrorController.java +++ b/src/main/java/com/codesdream/ase/controller/ASEErrorController.java @@ -1,24 +1,16 @@ package com.codesdream.ase.controller; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; -import com.codesdream.ase.component.error.ErrorResponse; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.respond.ErrorInfoJSONRespond; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.context.request.WebRequest; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import java.util.ArrayList; import java.util.Date; -import java.util.List; @Controller public class ASEErrorController implements ErrorController { @@ -80,7 +72,7 @@ public class ASEErrorController implements ErrorController { return quickJSONRespond.getJSONStandardRespond( statusCode, - "Error Controller Handle", + "Internal Server Error", null, errorInfoJSONRespond); } diff --git a/src/main/java/com/codesdream/ase/controller/LoginController.java b/src/main/java/com/codesdream/ase/controller/LoginController.java index 36f7522..fc3a5ce 100644 --- a/src/main/java/com/codesdream/ase/controller/LoginController.java +++ b/src/main/java/com/codesdream/ase/controller/LoginController.java @@ -2,15 +2,17 @@ package com.codesdream.ase.controller; import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.respond.JSONStandardFailedRespond; import com.codesdream.ase.component.json.request.UserLoginChecker; import com.codesdream.ase.component.json.respond.UserLoginCheckerJSONRespond; import com.codesdream.ase.service.IAuthService; import com.codesdream.ase.service.IUserService; +import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +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.ResponseBody; @@ -25,6 +27,7 @@ import java.util.Optional; */ @Slf4j @Controller +@Api(tags = "用户登录有关接口") public class LoginController { @Resource @@ -39,13 +42,7 @@ public class LoginController { @Resource private IAuthService authService; - - @RequestMapping(value = "/login") - String printLogin(Model model) { - return "login"; - } - - @RequestMapping(value = "/login/check_exists", method = RequestMethod.POST) + @PostMapping(value = "/login/check_exists") @ResponseBody String checkExists(HttpServletRequest request){ diff --git a/src/main/java/com/codesdream/ase/controller/PermissionController.java b/src/main/java/com/codesdream/ase/controller/PermissionController.java index 358e3a2..d702bee 100644 --- a/src/main/java/com/codesdream/ase/controller/PermissionController.java +++ b/src/main/java/com/codesdream/ase/controller/PermissionController.java @@ -2,16 +2,19 @@ package com.codesdream.ase.controller; import com.alibaba.fastjson.JSONObject; import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; +import com.codesdream.ase.component.json.model.JsonableTag; import com.codesdream.ase.component.json.respond.PermissionJSONRespond; +import com.codesdream.ase.exception.notfound.TagNotFoundException; import com.codesdream.ase.model.permission.Tag; import com.codesdream.ase.model.permission.User; import com.codesdream.ase.service.IUserService; import com.codesdream.ase.service.PermissionService; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -21,6 +24,7 @@ import java.util.Set; @RestController @RequestMapping("pmt") +@Api(tags = "权限管理接口") public class PermissionController { @Resource @@ -36,80 +40,34 @@ public class PermissionController { private QuickJSONRespond jsonRespond; // 根据名字创建新的标签 - @PostMapping("tag/create") - public String createTag(HttpServletRequest request){ - Optional jsonObjectOptional = jsonParameter.getJSONByRequest(request); - if(!jsonObjectOptional.isPresent()) return jsonRespond.getRespond400("Illegal JSON Format"); - JSONObject jsonObject = jsonObjectOptional.get(); + @PostMapping("tag") + @ApiOperation(value = "创建新的标签", notes = "创建标签时其ID自动分配,指定ID无效") + public JsonableTag createTag(@RequestBody JsonableTag tag){ + + String tagName = tag.getName(); +/* if(tagName == null) return jsonRespond.getRespond406("Missing Tag Name");*/ - String tagName = jsonObject.getString("name"); - if(tagName == null) return jsonRespond.getRespond406("Missing Tag Name"); - // 检查JSON是否合法 Optional tagOptional = permissionService.findTag(tagName); - if(tagOptional.isPresent()) return jsonRespond.getRespond409("Tag Name Already Exist"); +/* if(tagOptional.isPresent()) return jsonRespond.getRespond409("Tag Name Already Exist");*/ Tag newTag = permissionService.getDefaultTag(tagName); - newTag = permissionService.save(newTag); - - PermissionJSONRespond respond = new PermissionJSONRespond(); - respond.setActionSuccess(true); - respond.setTagId(newTag.getId()); - respond.setTagName(newTag.getName()); - - return jsonRespond.getRespond200(null, respond); + if(tag.getDescription() != null) { + newTag.setDescription(tag.getDescription()); + } + return new JsonableTag(permissionService.save(newTag)); } // 根据名字搜索标签的简要信息 - @PostMapping("tag/search") - public String checkTag(HttpServletRequest request){ - Optional jsonObjectOptional = jsonParameter.getJSONByRequest(request); - if(!jsonObjectOptional.isPresent()) return jsonRespond.getRespond400("Illegal JSON Format"); - - JSONObject jsonObject = jsonObjectOptional.get(); - String tagName = jsonObject.getString("name"); - if(tagName == null) return jsonRespond.getRespond406("Problem With Tag Name"); - Optional tagOptional = permissionService.findTag(tagName); - - PermissionJSONRespond respond = new PermissionJSONRespond(); - respond.setActionSuccess(true); - + @GetMapping("tag") + @ApiOperation("搜索标签信息") + @ApiImplicitParam(name = "name", value = "标签名") + public JsonableTag checkTag(@RequestParam(value = "name") String name){ + Optional tagOptional = permissionService.findTag(name); if(tagOptional.isPresent()){ - respond.setTagExist(true); - respond.setTagId(tagOptional.get().getId()); - respond.setTagName(tagOptional.get().getName()); + return new JsonableTag(tagOptional.get()); } - else respond.setTagExist(false); - - return jsonRespond.getRespond200(null, respond); - } - - // 由标签ID找到用户ID列表 - @PostMapping("tag/get/users") - public String getUsersTag(HttpServletRequest request){ - Optional jsonObjectOptional = jsonParameter.getJSONByRequest(request); - if(!jsonObjectOptional.isPresent()) return jsonRespond.getRespond400("Illegal JSON Format"); - - JSONObject jsonObject = jsonObjectOptional.get(); - Integer tagId = jsonObject.getInteger("id"); - if(tagId == null) return jsonRespond.getRespond406("Problem With Tag ID"); - Optional tagOptional = permissionService.findTag(tagId); - - PermissionJSONRespond respond = new PermissionJSONRespond(); - respond.setActionSuccess(true); - - if(tagOptional.isPresent()){ - respond.setTagExist(true); - respond.setTagId(tagOptional.get().getId()); - Set userIds = new HashSet<>(); - for(User user : permissionService.getUsersFromTag(tagOptional.get())) { - userIds.add(user.getId()); - } - respond.setUsers(userIds); - } - else respond.setTagExist(false); - - return jsonRespond.getRespond200(null, respond); + else throw new TagNotFoundException(name); } // 将用户添加到Tag中 diff --git a/src/main/java/com/codesdream/ase/controller/RegisterController.java b/src/main/java/com/codesdream/ase/controller/RegisterController.java index 88b4827..0908a34 100644 --- a/src/main/java/com/codesdream/ase/controller/RegisterController.java +++ b/src/main/java/com/codesdream/ase/controller/RegisterController.java @@ -1,7 +1,7 @@ package com.codesdream.ase.controller; import com.codesdream.ase.component.datamanager.JSONParameter; -import com.codesdream.ase.component.datamanager.QuickJSONRespond; +import com.codesdream.ase.component.api.QuickJSONRespond; import com.codesdream.ase.component.json.request.UserRegisterChecker; import com.codesdream.ase.model.information.BaseStudentInfo; import com.codesdream.ase.model.permission.User; diff --git a/src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java b/src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java deleted file mode 100644 index c85958a..0000000 --- a/src/main/java/com/codesdream/ase/exception/DataFileNotFoundException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.codesdream.ase.exception; - -import lombok.Data; -import lombok.EqualsAndHashCode; - -@EqualsAndHashCode(callSuper = true) -@Data -public class DataFileNotFoundException extends RuntimeException { - private String path; - - public DataFileNotFoundException(String filePath){ - super(); - this.path = filePath; - } -} diff --git a/src/main/java/com/codesdream/ase/exception/notfound/DataFileNotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/DataFileNotFoundException.java new file mode 100644 index 0000000..6ff26be --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/notfound/DataFileNotFoundException.java @@ -0,0 +1,15 @@ +package com.codesdream.ase.exception.notfound; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Data +public class DataFileNotFoundException extends NotFoundException { + private String path; + + public DataFileNotFoundException(String msg){ + super(msg); + this.path = msg; + } +} diff --git a/src/main/java/com/codesdream/ase/exception/notfound/NotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/NotFoundException.java new file mode 100644 index 0000000..a632648 --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/notfound/NotFoundException.java @@ -0,0 +1,12 @@ +package com.codesdream.ase.exception.notfound; + + +public class NotFoundException extends RuntimeException { + public NotFoundException(String msg){ + super(msg); + } + + public NotFoundException(){ + super(); + } +} diff --git a/src/main/java/com/codesdream/ase/exception/notfound/TagNotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/TagNotFoundException.java new file mode 100644 index 0000000..a7509ac --- /dev/null +++ b/src/main/java/com/codesdream/ase/exception/notfound/TagNotFoundException.java @@ -0,0 +1,11 @@ +package com.codesdream.ase.exception.notfound; + + +public class TagNotFoundException extends NotFoundException { + String tagName; + + public TagNotFoundException(String tagName){ + super(tagName); + this.tagName = tagName; + } +} diff --git a/src/main/java/com/codesdream/ase/exception/UserNotFoundException.java b/src/main/java/com/codesdream/ase/exception/notfound/UserNotFoundException.java similarity index 56% rename from src/main/java/com/codesdream/ase/exception/UserNotFoundException.java rename to src/main/java/com/codesdream/ase/exception/notfound/UserNotFoundException.java index c721ae9..65a58c0 100644 --- a/src/main/java/com/codesdream/ase/exception/UserNotFoundException.java +++ b/src/main/java/com/codesdream/ase/exception/notfound/UserNotFoundException.java @@ -1,22 +1,20 @@ -package com.codesdream.ase.exception; +package com.codesdream.ase.exception.notfound; import lombok.Data; import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = true) @Data -public class UserNotFoundException extends RuntimeException { +public class UserNotFoundException extends NotFoundException { Integer id; String username; - String message; public UserNotFoundException(Integer id, String username){ super(); this.id = id; this.username = username; } - public UserNotFoundException(String message){ - super(); - this.message = message; + public UserNotFoundException(String msg){ + super(msg); } } diff --git a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java index 7778168..6aae6c0 100644 --- a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java +++ b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java @@ -1,12 +1,11 @@ package com.codesdream.ase.service; import com.codesdream.ase.component.permission.UserAuthoritiesGenerator; -import com.codesdream.ase.exception.UserNotFoundException; +import com.codesdream.ase.exception.notfound.UserNotFoundException; import com.codesdream.ase.model.permission.User; import org.springframework.security.authentication.AuthenticationServiceException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import javax.annotation.Resource; diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index faae155..3fd26b1 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -4,7 +4,7 @@ import com.codesdream.ase.component.auth.ASEPasswordEncoder; import com.codesdream.ase.component.auth.ASEUsernameEncoder; import com.codesdream.ase.component.permission.UserRolesListGenerator; import com.codesdream.ase.exception.UserInformationIllegalException; -import com.codesdream.ase.exception.UserNotFoundException; +import com.codesdream.ase.exception.notfound.UserNotFoundException; import com.codesdream.ase.exception.UsernameAlreadyExistException; import com.codesdream.ase.model.information.BaseStudentInfo; import com.codesdream.ase.model.permission.User; diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties new file mode 100644 index 0000000..240adf1 --- /dev/null +++ b/src/main/resources/application-dev.properties @@ -0,0 +1,23 @@ +server.port=8081 + +spring.thymeleaf.prefix=classpath:templates/ +spring.thymeleaf.suffix=.html +spring.thymeleaf.mode=HTML +spring.thymeleaf.encoding=UTF-8 + +spring.jpa.generate-ddl=false +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=update +spring.jooq.sql-dialect=org.hibernate.dialect.MariaDB102Dialect +spring.jpa.open-in-view=true +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true + +spring.datasource.url=jdbc:mariadb://localhost:3306/ase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC +spring.datasource.username=root +spring.datasource.password=#a9b9fa6422 +spring.datasource.driver-class-name=org.mariadb.jdbc.Driver + +server.error.whitelabel.enabled=false + +logging.level.root=info +logging.level.org.springframework.security=info \ No newline at end of file