From d11c499e819054c2f2160e3e994cbdbfb9309e84 Mon Sep 17 00:00:00 2001 From: MaxKey Date: Tue, 5 Apr 2022 10:08:19 +0800 Subject: [PATCH] FileUpload&UserInfo&Accounts --- .../main/java/org/maxkey/entity/Accounts.java | 12 +- .../java/org/maxkey/entity/FileUpload.java | 122 ++++++++++++++++++ .../main/java/org/maxkey/entity/UserInfo.java | 33 ++--- .../persistence/mapper/FileUploadMapper.java | 32 +++++ .../service/FileUploadService.java | 39 ++++++ .../persistence/service/UserInfoService.java | 5 +- .../java/org/maxkey/MaxKeyMgtMvcConfig.java | 3 + .../web/contorller/AccountsController.java | 8 +- .../web/contorller/FileUploadEndpoint.java | 74 +++++++++++ .../web/contorller/UserInfoController.java | 25 +++- 10 files changed, 324 insertions(+), 29 deletions(-) create mode 100644 maxkey-core/src/main/java/org/maxkey/entity/FileUpload.java create mode 100644 maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/FileUploadMapper.java create mode 100644 maxkey-persistence/src/main/java/org/maxkey/persistence/service/FileUploadService.java create mode 100644 maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/FileUploadEndpoint.java diff --git a/maxkey-core/src/main/java/org/maxkey/entity/Accounts.java b/maxkey-core/src/main/java/org/maxkey/entity/Accounts.java index 9a93509bf..e0783f5cb 100644 --- a/maxkey-core/src/main/java/org/maxkey/entity/Accounts.java +++ b/maxkey-core/src/main/java/org/maxkey/entity/Accounts.java @@ -71,6 +71,8 @@ public class Accounts extends JpaBaseEntity implements Serializable { @Column private String strategyId; @Column + private String strategyName; + @Column private int status; @Column @@ -214,7 +216,15 @@ public class Accounts extends JpaBaseEntity implements Serializable { } } - public String getInstId() { + public String getStrategyName() { + return strategyName; + } + + public void setStrategyName(String strategyName) { + this.strategyName = strategyName; + } + + public String getInstId() { return instId; } diff --git a/maxkey-core/src/main/java/org/maxkey/entity/FileUpload.java b/maxkey-core/src/main/java/org/maxkey/entity/FileUpload.java new file mode 100644 index 000000000..a033dc038 --- /dev/null +++ b/maxkey-core/src/main/java/org/maxkey/entity/FileUpload.java @@ -0,0 +1,122 @@ +package org.maxkey.entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.apache.mybatis.jpa.persistence.JpaBaseEntity; +import org.springframework.web.multipart.MultipartFile; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +@Entity +@Table(name = "MXK_FILE_UPLOAD") +public class FileUpload extends JpaBaseEntity { + private static final long serialVersionUID = -4338400992411166457L; + + @Id + @Column + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") + String id; + + @Column + byte[] uploaded; + + @JsonIgnore + MultipartFile uploadFile; + + @Column + String fileName; + + @Column + String contentType; + + @Column + long contentSize; + + @Column + String createdBy; + + String createdDate; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public byte[] getUploaded() { + return uploaded; + } + + public void setUploaded(byte[] uploaded) { + this.uploaded = uploaded; + } + + public MultipartFile getUploadFile() { + return uploadFile; + } + + public void setUploadFile(MultipartFile uploadFile) { + this.uploadFile = uploadFile; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public long getContentSize() { + return contentSize; + } + + public void setContentSize(long contentSize) { + this.contentSize = contentSize; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + public String getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(String createdDate) { + this.createdDate = createdDate; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("FileUpload [id="); + builder.append(id); + builder.append(", uploadFile="); + builder.append(uploadFile); + builder.append(", createdBy="); + builder.append(this.createdBy); + builder.append("]"); + return builder.toString(); + } + +} diff --git a/maxkey-core/src/main/java/org/maxkey/entity/UserInfo.java b/maxkey-core/src/main/java/org/maxkey/entity/UserInfo.java index a8fb7800d..5de31bdf9 100644 --- a/maxkey-core/src/main/java/org/maxkey/entity/UserInfo.java +++ b/maxkey-core/src/main/java/org/maxkey/entity/UserInfo.java @@ -18,7 +18,6 @@ package org.maxkey.entity; import com.fasterxml.jackson.annotation.JsonIgnore; -import java.io.IOException; import java.util.Base64; import java.util.HashMap; import java.util.List; @@ -31,7 +30,6 @@ import javax.persistence.Id; import javax.persistence.Table; import org.apache.mybatis.jpa.persistence.JpaBaseEntity; import org.maxkey.util.StringUtils; -import org.springframework.web.multipart.MultipartFile; /** * . @@ -110,8 +108,7 @@ public class UserInfo extends JpaBaseEntity { @Column protected byte[] picture; protected String pictureBase64; - @JsonIgnore - protected MultipartFile pictureFile; + protected String pictureId; @Column protected int idType; @Column @@ -413,16 +410,6 @@ public class UserInfo extends JpaBaseEntity { this.password = password; } - public byte[] getPicture() { - if (pictureFile != null && !pictureFile.isEmpty()) { - try { - picture = pictureFile.getBytes(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return picture; - } public String getPictureBase64() { return pictureBase64; @@ -439,6 +426,10 @@ public class UserInfo extends JpaBaseEntity { } } + public byte[] getPicture() { + return picture; + } + /** * @return the protectedAppsMap */ @@ -628,15 +619,15 @@ public class UserInfo extends JpaBaseEntity { this.birthDate = birthDate; } - public MultipartFile getPictureFile() { - return pictureFile; - } + public String getPictureId() { + return pictureId; + } - public void setPictureFile(MultipartFile pictureFile) { - this.pictureFile = pictureFile; - } + public void setPictureId(String pictureId) { + this.pictureId = pictureId; + } - public int getIdType() { + public int getIdType() { return idType; } diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/FileUploadMapper.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/FileUploadMapper.java new file mode 100644 index 000000000..b017e98bb --- /dev/null +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/FileUploadMapper.java @@ -0,0 +1,32 @@ +/* + * Copyright [2022] [MaxKey of copyright http://www.maxkey.top] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/** + * + */ +package org.maxkey.persistence.mapper; + +import org.apache.mybatis.jpa.persistence.IJpaBaseMapper; +import org.maxkey.entity.FileUpload; + +/** + * @author Crystal.sea + * + */ +public interface FileUploadMapper extends IJpaBaseMapper { + +} diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/FileUploadService.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/FileUploadService.java new file mode 100644 index 000000000..b81406e82 --- /dev/null +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/FileUploadService.java @@ -0,0 +1,39 @@ +/* + * Copyright [2021] [MaxKey of copyright http://www.maxkey.top] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.maxkey.persistence.service; + +import org.apache.mybatis.jpa.persistence.JpaBaseService; +import org.maxkey.entity.FileUpload; +import org.maxkey.persistence.mapper.FileUploadMapper; +import org.springframework.stereotype.Repository; + +@Repository +public class FileUploadService extends JpaBaseService{ + + public FileUploadService() { + super(FileUploadMapper.class); + } + + /* (non-Javadoc) + * @see com.connsec.db.service.BaseService#getMapper() + */ + @Override + public FileUploadMapper getMapper() { + return (FileUploadMapper)super.getMapper(); + } +} diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java index 45d1a4448..fd4e9f15c 100644 --- a/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/service/UserInfoService.java @@ -212,7 +212,7 @@ public class UserInfoService extends JpaBaseService { public UserInfo passwordEncoder(UserInfo userInfo) { //密码不为空,则需要进行加密处理 - if(userInfo.getPassword()!=null && !userInfo.getPassword().equals("")) { + if(!StringUtils.isBlank(userInfo.getPassword())) { String password = passwordEncoder.encode(userInfo.getPassword()); userInfo.setDecipherable(PasswordReciprocal.getInstance().encode(userInfo.getPassword())); _logger.debug("decipherable : "+userInfo.getDecipherable()); @@ -220,6 +220,9 @@ public class UserInfoService extends JpaBaseService { userInfo.setPasswordLastSetTime(DateUtils.getCurrentDateTimeAsString()); userInfo.setModifiedDate(DateUtils.getCurrentDateTimeAsString()); + }else { + userInfo.setPassword(null); + userInfo.setDecipherable(null); } return userInfo; } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/MaxKeyMgtMvcConfig.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/MaxKeyMgtMvcConfig.java index 85ca4a846..569cc9d79 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/MaxKeyMgtMvcConfig.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/MaxKeyMgtMvcConfig.java @@ -121,6 +121,7 @@ public class MaxKeyMgtMvcConfig implements WebMvcConfigurer { .addPathPatterns("/apps/**") .addPathPatterns("/accounts/**") + .addPathPatterns("/access/**") .addPathPatterns("/access/**/**") @@ -136,6 +137,8 @@ public class MaxKeyMgtMvcConfig implements WebMvcConfigurer { .addPathPatterns("/institutions/**") .addPathPatterns("/localization/**") + .addPathPatterns("/file/upload/") + ; _logger.debug("add PermissionAdapter"); diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/AccountsController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/AccountsController.java index 9aaff0b9b..9534e1cb2 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/AccountsController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/AccountsController.java @@ -83,6 +83,7 @@ public class AccountsController { @RequestMapping(value = { "/get/{id}" }, produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity get(@PathVariable("id") String id) { Accounts account=accountsService.get(id); + account.setRelatedPassword(PasswordReciprocal.getInstance().decoder(account.getRelatedPassword())); return new Message(account).buildResponse(); } @@ -127,10 +128,13 @@ public class AccountsController { @ResponseBody @RequestMapping(value = "/generate") - public String generate(@ModelAttribute Accounts account) { + public ResponseEntity generate(@ModelAttribute Accounts account) { AccountsStrategy accountsStrategy = accountsStrategyService.get(account.getStrategyId()); UserInfo userInfo = userInfoService.get(account.getUserId()); - return accountsService.generateAccount(userInfo,accountsStrategy); + return new Message( + Message.SUCCESS, + (Object)accountsService.generateAccount(userInfo,accountsStrategy) + ).buildResponse(); } } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/FileUploadEndpoint.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/FileUploadEndpoint.java new file mode 100644 index 000000000..b808e09ec --- /dev/null +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/FileUploadEndpoint.java @@ -0,0 +1,74 @@ +/* + * Copyright [2020] [MaxKey of copyright http://www.maxkey.top] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.maxkey.web.contorller; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.maxkey.authn.annotation.CurrentUser; +import org.maxkey.entity.FileUpload; +import org.maxkey.entity.Message; +import org.maxkey.entity.UserInfo; +import org.maxkey.persistence.service.FileUploadService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class FileUploadEndpoint { + + private static Logger _logger = LoggerFactory.getLogger(FileUploadEndpoint.class); + + @Autowired + FileUploadService fileUploadService; + + @RequestMapping(value={"/file/upload/"}) + @ResponseBody + public ResponseEntity upload( HttpServletRequest request, + HttpServletResponse response, + @ModelAttribute FileUpload fileUpload, + @CurrentUser UserInfo currentUser){ + _logger.debug("FileUpload"); + fileUpload.setId(fileUpload.generateId()); + fileUpload.setContentType(fileUpload.getUploadFile().getContentType()); + fileUpload.setFileName(fileUpload.getUploadFile().getOriginalFilename()); + fileUpload.setContentSize(fileUpload.getUploadFile().getSize()); + fileUpload.setCreatedBy(currentUser.getUsername()); + /* + * upload UploadFile MultipartFile to Uploaded Bytes + */ + if(null!=fileUpload.getUploadFile()&&!fileUpload.getUploadFile().isEmpty()){ + try { + fileUpload.setUploaded(fileUpload.getUploadFile().getBytes()); + fileUploadService.insert(fileUpload); + _logger.trace("FileUpload SUCCESS"); + } catch (IOException e) { + _logger.error("FileUpload IOException",e); + } + } + return new Message(Message.SUCCESS,(Object)fileUpload.getId()).buildResponse(); + } + +} diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java index 5aaa93749..bf3084635 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java @@ -38,6 +38,7 @@ import org.maxkey.constants.ConstsPasswordSetType; import org.maxkey.entity.ExcelImport; import org.maxkey.entity.Message; import org.maxkey.entity.UserInfo; +import org.maxkey.persistence.service.FileUploadService; import org.maxkey.persistence.service.UserInfoService; import org.maxkey.util.DateUtils; import org.maxkey.util.ExcelUtils; @@ -73,6 +74,9 @@ public class UserInfoController { @Autowired private UserInfoService userInfoService; + + @Autowired + FileUploadService fileUploadService; @RequestMapping(value = { "/fetch" }, produces = {MediaType.APPLICATION_JSON_VALUE}) @@ -101,6 +105,8 @@ public class UserInfoController { if(userInfo.getPicture()!=null){ userInfo.transPictureBase64(); } + userInfo.setPassword(""); + userInfo.setDecipherable(""); return new Message(userInfo).buildResponse(); } @@ -110,6 +116,10 @@ public class UserInfoController { _logger.debug("-Add :" + userInfo); userInfo.setId(WebContext.genId()); userInfo.setInstId(currentUser.getInstId()); + if(StringUtils.isNotBlank(userInfo.getPictureId())) { + userInfo.setPicture(fileUploadService.get(userInfo.getPictureId()).getUploaded()); + fileUploadService.remove(userInfo.getPictureId()); + } if (userInfoService.insert(userInfo)) { return new Message(Message.SUCCESS).buildResponse(); } else { @@ -128,6 +138,10 @@ public class UserInfoController { convertExtraAttribute(userInfo) ; _logger.info(userInfo.getExtraAttribute()); userInfo.setInstId(currentUser.getInstId()); + if(StringUtils.isNotBlank(userInfo.getPictureId())) { + userInfo.setPicture(fileUploadService.get(userInfo.getPictureId()).getUploaded()); + fileUploadService.remove(userInfo.getPictureId()); + } if (userInfoService.update(userInfo)) { return new Message(Message.SUCCESS).buildResponse(); } else { @@ -148,9 +162,12 @@ public class UserInfoController { @ResponseBody - @RequestMapping(value = "/randomPassword") - public String randomPassword() { - return userInfoService.randomPassword(); + @RequestMapping(value = "/randomPassword", produces = {MediaType.APPLICATION_JSON_VALUE}) + public ResponseEntity randomPassword() { + return new Message( + Message.SUCCESS, + (Object)userInfoService.randomPassword() + ).buildResponse(); } @@ -169,7 +186,7 @@ public class UserInfoController { @ResponseBody - @RequestMapping(value="/changePassword") + @RequestMapping(value="/changePassword", produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity changePassword( @ModelAttribute("userInfo")UserInfo userInfo) { _logger.debug(userInfo.getId()); userInfo.setPasswordSetType(ConstsPasswordSetType.PASSWORD_NORMAL); -- GitLab