瀏覽代碼

提交配置文件,上传图片

xusonglin 5 年之前
父節點
當前提交
59a8be1c10

+ 16 - 2
src/main/java/com/jkcredit/sysnews/config/CorsConfig.java

@@ -5,6 +5,9 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.web.cors.CorsConfiguration;
 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
 /**
  * @description:
@@ -13,13 +16,15 @@ import org.springframework.web.filter.CorsFilter;
  * @version: V1.0
  **/
 @Configuration
-public class CorsConfig {
+public class CorsConfig implements WebMvcConfigurer {
+
     private CorsConfiguration buildConfig() {
         CorsConfiguration corsConfiguration = new CorsConfiguration();
-        corsConfiguration.setAllowCredentials(true);
+//        corsConfiguration.setAllowCredentials(true);
         corsConfiguration.addAllowedOrigin("*"); //允许任何域名
         corsConfiguration.addAllowedHeader("*"); //允许任何头
         corsConfiguration.addAllowedMethod("*"); //允许任何方法
+        corsConfiguration.addExposedHeader("Authorization");
         return corsConfiguration;
     }
 
@@ -29,4 +34,13 @@ public class CorsConfig {
         source.registerCorsConfiguration("/**", buildConfig()); //注册
         return new CorsFilter(source);
     }
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+                .allowedOrigins("*")
+                .allowCredentials(true)
+                .allowedMethods("GET", "POST")
+                .maxAge(3600);
+    }
 }

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

@@ -15,6 +15,7 @@ import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+import org.springframework.web.cors.CorsUtils;
 import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport;
 
 import javax.annotation.PostConstruct;
@@ -90,6 +91,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 
                 .and()
                 .authorizeRequests()
+                .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
                 .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui",
                         "/swagger-resources", "/swagger-resources/configuration/security",
                         "/swagger-ui.html", "/webjars/**").permitAll()

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

@@ -30,20 +30,20 @@ public class PhotoResource extends WebResource {
     @Autowired
     PhotoService photoService;
 
-//    @PostMapping("/upload")
-//    @PreAuthorize("hasPermission('photo','upload')")
-//    @ApiOperation(value = "上传图片")
-//    public ResponseData upload(@RequestParam("photo") MultipartFile photo) {
-//        if (photo == null) {
-//            return ResponseData.failed("上传失败,图片不能为空!");
-//        }
-//        try {
-//            String url = photoService.upload(photo);
-//            return ResponseData.success((Object) url);
-//        } catch (ServiceException e) {
-//            return ResponseData.failed("图片上传失败");
-//        }
-//    }
+    @PostMapping("/upload")
+    @PreAuthorize("hasPermission('photo','upload')")
+    @ApiOperation(value = "上传图片")
+    public ResponseData upload(@RequestParam("photo") MultipartFile photo) {
+        if (photo == null) {
+            return ResponseData.failed("上传失败,图片不能为空!");
+        }
+        try {
+            String url = photoService.upload(photo);
+            return ResponseData.success((Object) url);
+        } catch (ServiceException e) {
+            return ResponseData.failed("图片上传失败");
+        }
+    }
 
     @GetMapping("/page/photos")
     @PreAuthorize("hasPermission('photo','read')")

+ 1 - 1
src/main/java/com/jkcredit/sysnews/service/photo/PhotoService.java

@@ -13,7 +13,7 @@ import org.springframework.web.multipart.MultipartFile;
  * @version: V1.0
  **/
 public interface PhotoService {
-//    PhotoVo upload(MultipartFile photo);
+    String upload(MultipartFile photo);
 
     IPage<PhotoVo> getPhotos(Page page, PhotoDto photoDto);
 

+ 13 - 18
src/main/java/com/jkcredit/sysnews/service/photo/PhotoServiceImpl.java

@@ -40,24 +40,19 @@ public class PhotoServiceImpl extends BaseService implements PhotoService {
     @Resource
     PhotoMapper photoMapper;
 
-//    @Override
-//    public PhotoVo upload(MultipartFile photo) {
-//        String filePath = absolutePath + relativePath;
-//        String fileName = UUID.randomUUID().toString().replace("-","") + "-" + photo.getOriginalFilename();
-//        try {
-//            UploadUtil.upload(photo, filePath, fileName);
-//        } catch (IOException e) {
-//            log.error("图片上传错误,错误原因:" + e.getMessage());
-//            throw new ServiceException("图片上传失败");
-//        }
-//
-//        if (!StringUtils.isBlank(realPath)) {
-//            return realPath;
-//        } else {
-//            log.error("图片上传失败");
-//            throw new ServiceException("图片上传失败");
-//        }
-//    }
+    @Override
+    public String upload(MultipartFile photo) {
+        String fileName = UUID.randomUUID().toString().replace("-","") + "-" +
+                photo.getOriginalFilename();
+        String filePath = relativePath + File.separator + fileName;
+        try {
+            UploadUtil.upload(photo, filePath);
+            return filePath;
+        } catch (IOException ioe) {
+            log.error("图片上传失败,失败原因:{}", ioe.getMessage());
+            throw new ServiceException("图片上传失败");
+        }
+    }
 
     @Override
     public IPage<PhotoVo> getPhotos(Page page, PhotoDto photoDto) {

+ 1 - 1
src/main/resources/application-prod.yml

@@ -10,4 +10,4 @@ spring:
 mybatis-plus:
   mapper-locations: classpath:mapper/*Mapper.xml
 photo:
-  relativePath:
+  relativePath: /home/jknews/news/photo