xusonglin 5 роки тому
батько
коміт
61774397f8

+ 8 - 7
pom.xml

@@ -101,19 +101,20 @@
             <version>2.6</version>
         </dependency>
         <dependency>
+            <groupId>commons-beanutils</groupId>
+            <artifactId>commons-beanutils</artifactId>
+            <version>${commons.beanutils.version}</version>
+        </dependency>
+        <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
-            <version>2.8.0</version>
+            <version>2.6.1</version>
         </dependency>
+
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger-ui</artifactId>
-            <version>2.8.0</version>
-        </dependency>
-        <dependency>
-            <groupId>commons-beanutils</groupId>
-            <artifactId>commons-beanutils</artifactId>
-            <version>${commons.beanutils.version}</version>
+            <version>2.6.1</version>
         </dependency>
     </dependencies>
 

+ 3 - 2
src/main/java/com/jkcredit/sysnews/config/SecurityConfiguration.java

@@ -90,8 +90,9 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 
                 .and()
                 .authorizeRequests()
-                .antMatchers("/swagger-ui.html")
-                .permitAll()
+                .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui",
+                        "/swagger-resources", "/swagger-resources/configuration/security",
+                        "/swagger-ui.html", "/webjars/**").permitAll()
 
                 .anyRequest()
                 .access("@rbacauthorityservice.hasPermission(request,authentication)") // RBAC 动态 url 认证

+ 19 - 6
src/main/java/com/jkcredit/sysnews/config/SwaggerConfiguration.java

@@ -2,42 +2,55 @@ package com.jkcredit.sysnews.config;
 
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 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.List;
+
 /**
  * @description:
  * @author: xusonglin
  * @create: 2020/3/1 15:59
  * @version: V1.0
  **/
+@Configuration
+@EnableSwagger2
 public class SwaggerConfiguration {
-    // todo
     @Bean
     public Docket createRestApi() {
+        ParameterBuilder tokenPar = new ParameterBuilder();
+        List<Parameter> pars = new ArrayList<Parameter>();
+        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
+        pars.add(tokenPar.build());
+
         return new Docket(DocumentationType.SWAGGER_2)
+                .enable(true)
                 .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                 .select()
                 //控制暴露出去的路径下的实例
                 //如果某个接口不想暴露,可以使用以下注解
                 //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
-                .apis(RequestHandlerSelectors.basePackage("com.example.zwd.springbootswagger2.controller"))
+                .apis(RequestHandlerSelectors.basePackage("com.jkcredit.sysnews.resource"))
                 .paths(PathSelectors.any())
-                .build();
+                .build().globalOperationParameters(pars);
     }
     //构建 api文档的详细信息函数
     private ApiInfo apiInfo() {
         return new ApiInfoBuilder()
                 //页面标题
-                .title("Spring Boot Swagger2 构建RESTful API")
+                .title("新闻后台管理接口")
                 //条款地址
-                .termsOfServiceUrl("http://despairyoke.github.io/")
-                .contact("zwd")
+                .termsOfServiceUrl("")
                 .version("1.0")
                 //描述
                 .description("API 描述")

+ 6 - 0
src/main/java/com/jkcredit/sysnews/resource/web/newsArticle/NewsArticleResource.java

@@ -13,6 +13,7 @@ import com.jkcredit.sysnews.resource.web.photo.fb.PhotoFB;
 import com.jkcredit.sysnews.service.newsArticle.NewsArticleService;
 import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
 import com.jkcredit.sysnews.spi.web.data.ResponseData;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -30,6 +31,7 @@ public class NewsArticleResource extends WebResource {
     @Autowired
     NewsArticleService newsArticleService;
 
+    @ApiOperation(value = "获取新闻列表")
     @GetMapping("/page/newsArticle")
     @PreAuthorize("hasPermission('article','read')")
     public ResponseData getNewsArticle(Page page, NewsArticleQueryFB queryFB) {
@@ -44,6 +46,7 @@ public class NewsArticleResource extends WebResource {
 
     @GetMapping("/newsArticle/{id}")
     @PreAuthorize("hasPermission('article','read')")
+    @ApiOperation(value = "根据id获取新闻")
     public ResponseData getNewsArticleById(@PathVariable("id") Long id) {
         try {
             NewsArticleVo newsArticleVo = newsArticleService.getNewsArticleById(id);
@@ -55,6 +58,7 @@ public class NewsArticleResource extends WebResource {
 
     @PostMapping("/newsArticle")
     @PreAuthorize("hasPermission('article','edit')")
+    @ApiOperation(value = "新增新闻")
     public ResponseData saveNewsArticle(NewsArticleFB newsArticleFB) {
         try {
             validate(newsArticleFB);
@@ -71,6 +75,7 @@ public class NewsArticleResource extends WebResource {
 
     @PutMapping("/newsArticle")
     @PreAuthorize("hasPermission('article','edit')")
+    @ApiOperation(value = "编辑新闻")
     public ResponseData updateNewsArticle(NewsArticleFB newsArticleFB) {
         try {
             validate(newsArticleFB);
@@ -85,6 +90,7 @@ public class NewsArticleResource extends WebResource {
 
     @PutMapping("/newsArticle/{id}")
     @PreAuthorize("hasPermission('article','edit')")
+    @ApiOperation(value = "删除新闻")
     public ResponseData deleteNewsArticle(@PathVariable("id") Long id) {
         try {
             newsArticleService.deleteNewsArticle(id);

+ 7 - 0
src/main/java/com/jkcredit/sysnews/resource/web/photo/PhotoResource.java

@@ -11,6 +11,7 @@ import com.jkcredit.sysnews.resource.web.photo.fb.PhotoQueryFB;
 import com.jkcredit.sysnews.service.photo.PhotoService;
 import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
 import com.jkcredit.sysnews.spi.web.data.ResponseData;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
@@ -31,6 +32,7 @@ public class PhotoResource extends WebResource {
 
     @PostMapping("/upload")
     @PreAuthorize("hasPermission('photo','upload')")
+    @ApiOperation(value = "上传图片")
     public ResponseData upload(@RequestParam("photo") MultipartFile photo) {
         if (photo == null) {
             return ResponseData.failed("上传失败,图片不能为空!");
@@ -45,6 +47,7 @@ public class PhotoResource extends WebResource {
 
     @GetMapping("/page/photos")
     @PreAuthorize("hasPermission('photo','read')")
+    @ApiOperation(value = "获取图片列表")
     public ResponseData getPhotos(Page page, PhotoQueryFB queryFB) {
         try {
             PhotoDto photoDto = mapper.map(queryFB, PhotoDto.class);
@@ -57,6 +60,7 @@ public class PhotoResource extends WebResource {
 
     @GetMapping("/photo/{id}")
     @PreAuthorize("hasPermission('photo','read')")
+    @ApiOperation(value = "根据id获取图片信息")
     public ResponseData getPhotoById(@PathVariable("id") Long id) {
         try {
             PhotoVo photoVo = photoService.getPhotoById(id);
@@ -68,6 +72,7 @@ public class PhotoResource extends WebResource {
 
     @PostMapping("/photo")
     @PreAuthorize("hasPermission('photo','edit')")
+    @ApiOperation(value = "新增图片信息")
     public ResponseData savePhoto(PhotoFB photoFB) {
         try {
             validate(photoFB);
@@ -84,6 +89,7 @@ public class PhotoResource extends WebResource {
 
     @PutMapping("/photo")
     @PreAuthorize("hasPermission('photo','edit')")
+    @ApiOperation(value = "编辑图片信息")
     public ResponseData updatePhoto(PhotoEditFB photoFB) {
         try {
             validate(photoFB);
@@ -98,6 +104,7 @@ public class PhotoResource extends WebResource {
 
     @PutMapping("/photo/{id}")
     @PreAuthorize("hasPermission('photo','edit')")
+    @ApiOperation(value = "删除图片信息")
     public ResponseData deletePhoto(@PathVariable("id") Long id) {
         try {
             photoService.deletePhoto(id);