xiaoyong931011
2022-05-31 a69b31c6d942b816e1e48e9dc017a6f52bd23ae4
20220527
10 files modified
2 files added
168 ■■■■■ changed files
src/main/java/com/xcong/farmer/cms/common/contants/AppContants.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/configurations/WebMvcConfig.java 17 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/configurations/properties/ApplicationProperties.java 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java 3 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminColumnController.java 2 ●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java 115 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminMenuController.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminNavigationBarController.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminRoleController.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminUserController.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminBase64UploadDto.java 16 ●●●●● patch | view | raw | blame | history
src/main/resources/application.properties 4 ●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/common/contants/AppContants.java
@@ -21,4 +21,7 @@
     */
    public static final String TOKEN_START_WITH = "Bearer ";
    public static final String PICTURE_PATH = "D:\\Tools\\";
}
src/main/java/com/xcong/farmer/cms/configurations/WebMvcConfig.java
@@ -1,11 +1,16 @@
package com.xcong.farmer.cms.configurations;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
@@ -37,4 +42,16 @@
    public SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }
    @Value("${upload.file.location}")
    private String fileLocation;
    @Value("${upload.file.path}")
    private String filePath;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //注册配置类,使用addResourceHandlers方法,将本地路径savePath映射到saveUrl路由上。
        registry.addResourceHandler(filePath).addResourceLocations(fileLocation);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}
src/main/java/com/xcong/farmer/cms/configurations/properties/ApplicationProperties.java
@@ -17,4 +17,8 @@
    private boolean debug;
    private String fileStoragePath;
    private String staticResourceUrl;
}
src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java
@@ -43,6 +43,9 @@
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/**").permitAll()
                .antMatchers("/api/common/login").permitAll()
                .antMatchers("/api/common/doUpload").permitAll()
                .antMatchers("/api/common/uploadFile").permitAll()
                .antMatchers("/image/**").permitAll()
                .anyRequest().authenticated()
                .and().apply(securityConfiguereAdapter());
    }
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminColumnController.java
@@ -28,7 +28,7 @@
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminColumnVo.class)})
    @PostMapping(value = "/columnInPage")
    public Result getColumnInPage(@RequestBody @Valid AdminColumnDto adminColumnDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
//        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iColumnService.getColumnInPage(adminColumnDto);
    }
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
@@ -2,28 +2,32 @@
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.common.system.bean.LoginUserBean;
import com.xcong.farmer.cms.common.system.dto.LoginDto;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminBase64UploadDto;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.utils.RedisUtils;
import com.xcong.farmer.cms.utils.SpringContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.hutool.core.util.ObjectUtil;
@RestController
@RequestMapping(value = "/api/common")
@@ -59,4 +63,93 @@
        return iCommonService.memberLogout();
    }
    private Long maxSize = 1024*1024*100L;
//    /**
//     * 文件上传方法
//     */
//    @ApiOperation(value = "文件上传", notes = "文件上传")
//    @PostMapping(value = "/doUpload")
//    public Result doFileUpload(@RequestBody @Validated AdminBase64UploadDto uploadDto){
//        // 文件保存目录路径
//        String savePath = AppContants.PICTURE_PATH;
//        // 文件保存目录URL
//        String saveUrl = resourceUrl;
//        // 保存和访问路径检查
//        if (StrUtil.isEmpty(saveUrl) || StrUtil.isEmpty(savePath)) {
//            return Result.fail("文件上传失败");
//        }
//        // 检查目录
//        File uploadDir = new File(savePath);
//        if (!uploadDir.isDirectory()) {
//            uploadDir.mkdir();
//        }
//        log.info("uploadDto:" + uploadDto.getBase64Str());
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] bytes = new byte[0];
//        try {
//            bytes = decoder.decodeBuffer(uploadDto.getBase64Str());
//        } catch (IOException e) {
//            return Result.fail("上传文件失败");
//        }
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String ymd = sdf.format(new Date());
//        savePath += ymd + "/";
//        saveUrl += ymd + "/";
//        File dirFile = new File(savePath);
//        if (!dirFile.exists()) {
//            dirFile.mkdirs();
//        }
//        if (bytes.length > maxSize) {
//            return Result.fail("上传文件大小超过限制");
//        }
//        String newFileName = IdUtil.simpleUUID() + IdUtil.simpleUUID() + ".png";
//        File uploadedFile = new File(savePath, newFileName);
//        try {
//            FileCopyUtils.copy(bytes, uploadedFile);
//        } catch (Exception e) {
//            return Result.fail("上传文件失败");
//        }
//        log.info("saveUrl:" + saveUrl);
//        String visitPath = saveUrl + newFileName;
//        log.info("上传一个文件:" + newFileName);
//        log.info("访问路径:" + visitPath);
//
//        return Result.ok("上传成功",visitPath);
//    }
    @Value("${static.resource.url}")
    private String resourceUrl;
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        // 文件保存目录路径
        String savePath = AppContants.PICTURE_PATH;
        // 文件保存目录URL
        String saveUrl = resourceUrl;
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        //获得文件的后缀
        String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File filepath = new File(savePath + filename);
        try {
            //存文件
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String visitPath = (saveUrl + filename);
        return Result.ok("上传成功",visitPath);
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminMenuController.java
@@ -30,7 +30,6 @@
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminMenuVo.class)})
    @PostMapping(value = "/menuInPage")
    public Result getMenuInPage(@RequestBody @Valid AdminMenuDto adminMenuDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iMenuService.getMenuInPage(adminMenuDto);
    }
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminNavigationBarController.java
@@ -28,7 +28,6 @@
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminNavigationBarVo.class)})
    @PostMapping(value = "/barInPage")
    public Result getBarInPage(@RequestBody @Valid AdminNavigationBarDto adminNavigationBarDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iNavigationBarService.getBarInPage(adminNavigationBarDto);
    }
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminRoleController.java
@@ -30,7 +30,6 @@
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminRoleVo.class)})
    @PostMapping(value = "/userInPage")
    public Result getUserInPage(@RequestBody @Valid AdminRoleDto adminRoleDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iRoleService.getRoleInPage(adminRoleDto);
    }
src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminUserController.java
@@ -33,7 +33,6 @@
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = AdminUserVo.class)})
    @PostMapping(value = "/userInPage")
    public Result getUserInPage(@RequestBody @Valid AdminUserDto adminUserDto) {
        log.info("--->{}", SecurityContextHolder.getContext().getAuthentication());
        return iUserService.getUserInPage(adminUserDto);
    }
src/main/java/com/xcong/farmer/cms/modules/system/dto/AdminBase64UploadDto.java
New file
@@ -0,0 +1,16 @@
package com.xcong.farmer.cms.modules.system.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
@ApiModel(value = "AdminBase64UploadDto", description = "参数接收类")
public class AdminBase64UploadDto {
    @NotBlank
    @ApiModelProperty(value = "base64字符串")
    public String base64Str;
}
src/main/resources/application.properties
New file
@@ -0,0 +1,4 @@
# ÅäÖÃÉÏ´«Í¼Æ¬µÄ·¾¶ºÍ·ÃÎÊͼƬͼƬµÄ·¾¶
upload.file.location=file:D:/Tools/
upload.file.path =/image/**
static.resource.url =http://localhost:8878/image/