Browse Source

【CHG】代码优化

ZhaoMn 4 năm trước cách đây
mục cha
commit
4267b5eeb9

+ 0 - 17
railway-admin/src/main/java/com/railway/RailwayServletInitializer.java

@@ -1,17 +0,0 @@
-package com.railway;
-
-import org.springframework.boot.builder.SpringApplicationBuilder;
-import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
-
-/**
- * web容器中进行部署
- *
- * @author railway
- */
-public class RailwayServletInitializer extends SpringBootServletInitializer {
-
-  @Override
-  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
-    return application.sources(RailwayApplication.class);
-  }
-}

+ 7 - 7
railway-admin/src/main/java/com/railway/web/controller/system/SysCaptchaController.java → railway-admin/src/main/java/com/railway/web/controller/common/CaptchaController.java

@@ -1,4 +1,4 @@
-package com.railway.web.controller.system;
+package com.railway.web.controller.common;
 
 import java.awt.image.BufferedImage;
 import java.io.IOException;
@@ -23,7 +23,7 @@ import com.railway.framework.controller.BaseController;
  */
 @Controller
 @RequestMapping("/captcha")
-public class SysCaptchaController extends BaseController {
+public class CaptchaController extends BaseController {
 
   @Resource(name = "captchaProducer")
   private Producer captchaProducer;
@@ -46,21 +46,21 @@ public class SysCaptchaController extends BaseController {
       response.setContentType("image/jpeg");
 
       String type = request.getParameter("type");
-      String capStr = null;
+      String capStr;
       String code = null;
-      BufferedImage bi = null;
+      BufferedImage image = null;
       if ("math".equals(type)) {
         String capText = captchaProducerMath.createText();
         capStr = capText.substring(0, capText.lastIndexOf("@"));
         code = capText.substring(capText.lastIndexOf("@") + 1);
-        bi = captchaProducerMath.createImage(capStr);
+        image = captchaProducerMath.createImage(capStr);
       } else if ("char".equals(type)) {
         capStr = code = captchaProducer.createText();
-        bi = captchaProducer.createImage(capStr);
+        image = captchaProducer.createImage(capStr);
       }
       session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);
       out = response.getOutputStream();
-      ImageIO.write(bi, "jpg", out);
+      ImageIO.write(image, "jpg", out);
       out.flush();
 
     } catch (Exception e) {

+ 4 - 5
railway-admin/src/main/java/com/railway/web/controller/common/CommonController.java

@@ -70,7 +70,7 @@ public class CommonController {
    */
   @PostMapping("/common/upload")
   @ResponseBody
-  public AjaxResult uploadFile(MultipartFile file) throws Exception {
+  public AjaxResult uploadFile(MultipartFile file) {
     try {
       // 上传文件路径
       String filePath = RailwayConfig.getUploadPath();
@@ -91,7 +91,7 @@ public class CommonController {
    */
   @PostMapping("/common/uploads")
   @ResponseBody
-  public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
+  public AjaxResult uploadFiles(List<MultipartFile> files) {
     try {
       // 上传文件路径
       String filePath = RailwayConfig.getUploadPath();
@@ -117,9 +117,8 @@ public class CommonController {
    * 本地资源通用下载
    */
   @GetMapping("/common/download/resource")
-  public void resourceDownload(String resource, HttpServletRequest request,
-      HttpServletResponse response)
-      throws Exception {
+  public void resourceDownload(String resource,
+      HttpServletResponse response) {
     try {
       if (!FileUtils.checkAllowDownload(resource)) {
         throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource));

+ 0 - 1
railway-admin/src/main/java/com/railway/web/controller/monitor/DruidController.java

@@ -17,7 +17,6 @@ public class DruidController extends BaseController {
 
   private String prefix = "/druid";
 
-  @RequiresPermissions("monitor:data:view")
   @GetMapping()
   public String index() {
     return redirect(prefix + "/index.html");

+ 1 - 1
railway-admin/src/main/java/com/railway/web/controller/monitor/SysOperlogController.java

@@ -56,7 +56,7 @@ public class SysOperlogController extends BaseController {
   @ResponseBody
   public AjaxResult export(SysOperLog operLog) {
     List<SysOperLog> list = operLogService.selectOperLogList(operLog);
-    ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
+    ExcelUtil<SysOperLog> util = new ExcelUtil<>(SysOperLog.class);
     return util.exportExcel(list, "操作日志");
   }
 

+ 3 - 3
railway-admin/src/main/resources/application.yml

@@ -16,7 +16,7 @@ railway:
 # 开发环境配置
 server:
   # 服务器的HTTP端口,默认为80
-  port: 80
+  port: 2021
   servlet:
     # 应用的访问路径
     context-path: /
@@ -38,7 +38,7 @@ logging:
 user:
   password:
     # 密码错误{maxRetryCount}次锁定10分钟
-    maxRetryCount: 5
+    maxRetryCount: 50
 
 # Spring配置
 spring:
@@ -95,7 +95,7 @@ shiro:
     # 首页地址
     indexUrl: /index
     # 验证码开关
-    captchaEnabled: true
+    captchaEnabled: false
     # 验证码类型 math 数组计算 char 字符
     captchaType: math
   cookie:

+ 2 - 0
railway-common/src/main/java/com/railway/common/core/domain/TreeEntity.java

@@ -1,12 +1,14 @@
 package com.railway.common.core.domain;
 
 import lombok.Data;
+import lombok.EqualsAndHashCode;
 
 /**
  * Tree基类
  *
  * @author railway
  */
+@EqualsAndHashCode(callSuper=false)
 @Data
 public class TreeEntity extends BaseEntity {
 

+ 3 - 8
railway-common/src/main/java/com/railway/common/utils/file/FileUtils.java

@@ -126,14 +126,9 @@ public class FileUtils {
       return false;
     }
 
-    // 检查允许下载的文件规则
-    if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,
-        FileTypeUtils.getFileType(resource))) {
-      return true;
-    }
-
-    // 不在允许下载的文件规则
-    return false;
+    // 检查允许/不允许下载的文件规则
+    return ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION,
+        FileTypeUtils.getFileType(resource));
   }
 
   /**

+ 16 - 20
railway-framework/src/main/java/com/railway/framework/shiro/realm/UserRealm.java

@@ -1,6 +1,16 @@
 package com.railway.framework.shiro.realm;
 
-import java.util.HashSet;
+import com.railway.common.core.domain.entity.SysUser;
+import com.railway.common.exception.user.CaptchaException;
+import com.railway.common.exception.user.RoleBlockedException;
+import com.railway.common.exception.user.UserBlockedException;
+import com.railway.common.exception.user.UserNotExistsException;
+import com.railway.common.exception.user.UserPasswordNotMatchException;
+import com.railway.common.exception.user.UserPasswordRetryLimitExceedException;
+import com.railway.common.utils.ShiroUtils;
+import com.railway.framework.shiro.service.SysLoginService;
+import com.railway.system.service.ISysMenuService;
+import com.railway.system.service.ISysRoleService;
 import java.util.Set;
 import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationInfo;
@@ -20,17 +30,6 @@ import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import com.railway.common.core.domain.entity.SysUser;
-import com.railway.common.exception.user.CaptchaException;
-import com.railway.common.exception.user.RoleBlockedException;
-import com.railway.common.exception.user.UserBlockedException;
-import com.railway.common.exception.user.UserNotExistsException;
-import com.railway.common.exception.user.UserPasswordNotMatchException;
-import com.railway.common.exception.user.UserPasswordRetryLimitExceedException;
-import com.railway.common.utils.ShiroUtils;
-import com.railway.framework.shiro.service.SysLoginService;
-import com.railway.system.service.ISysMenuService;
-import com.railway.system.service.ISysRoleService;
 
 /**
  * 自定义Realm 处理登录 权限
@@ -57,9 +56,9 @@ public class UserRealm extends AuthorizingRealm {
   protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
     SysUser user = ShiroUtils.getSysUser();
     // 角色列表
-    Set<String> roles = new HashSet<String>();
+    Set<String> roles;
     // 功能列表
-    Set<String> menus = new HashSet<String>();
+    Set<String> menus;
     SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
     // 管理员拥有所有权限
     if (user.isAdmin()) {
@@ -89,7 +88,7 @@ public class UserRealm extends AuthorizingRealm {
       password = new String(upToken.getPassword());
     }
 
-    SysUser user = null;
+    SysUser user;
     try {
       user = loginService.login(username, password);
     } catch (CaptchaException e) {
@@ -100,16 +99,13 @@ public class UserRealm extends AuthorizingRealm {
       throw new IncorrectCredentialsException(e.getMessage(), e);
     } catch (UserPasswordRetryLimitExceedException e) {
       throw new ExcessiveAttemptsException(e.getMessage(), e);
-    } catch (UserBlockedException e) {
-      throw new LockedAccountException(e.getMessage(), e);
-    } catch (RoleBlockedException e) {
+    } catch (UserBlockedException | RoleBlockedException e) {
       throw new LockedAccountException(e.getMessage(), e);
     } catch (Exception e) {
       log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
       throw new AuthenticationException(e.getMessage(), e);
     }
-    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
-    return info;
+    return new SimpleAuthenticationInfo(user, password, getName());
   }
 
   /**

+ 7 - 37
railway-framework/src/main/java/com/railway/framework/shiro/service/SysLoginService.java

@@ -1,6 +1,5 @@
 package com.railway.framework.shiro.service;
 
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 import com.railway.common.constant.Constants;
@@ -29,11 +28,14 @@ import com.railway.system.service.ISysUserService;
 @Component
 public class SysLoginService {
 
-  @Autowired
-  private SysPasswordService passwordService;
+  private final SysPasswordService passwordService;
+  private final ISysUserService userService;
 
-  @Autowired
-  private ISysUserService userService;
+  public SysLoginService(SysPasswordService passwordService,
+      ISysUserService userService) {
+    this.passwordService = passwordService;
+    this.userService = userService;
+  }
 
   /**
    * 登录
@@ -71,18 +73,6 @@ public class SysLoginService {
     // 查询用户信息
     SysUser user = userService.selectUserByLoginName(username);
 
-    /**
-     if (user == null && maybeMobilePhoneNumber(username))
-     {
-     user = userService.selectUserByPhoneNumber(username);
-     }
-
-     if (user == null && maybeEmail(username))
-     {
-     user = userService.selectUserByEmail(username);
-     }
-     */
-
     if (user == null) {
       AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL,
           MessageUtils.message("user.not.exists")));
@@ -109,26 +99,6 @@ public class SysLoginService {
     return user;
   }
 
-  /**
-   private boolean maybeEmail(String username)
-   {
-   if (!username.matches(UserConstants.EMAIL_PATTERN))
-   {
-   return false;
-   }
-   return true;
-   }
-
-   private boolean maybeMobilePhoneNumber(String username)
-   {
-   if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
-   {
-   return false;
-   }
-   return true;
-   }
-   */
-
   /**
    * 记录登录信息
    *

+ 2 - 3
railway-framework/src/main/java/com/railway/framework/shiro/web/filter/captcha/CaptchaValidateFilter.java

@@ -44,11 +44,10 @@ public class CaptchaValidateFilter extends AccessControlFilter {
 
   @Override
   protected boolean isAccessAllowed(ServletRequest request, ServletResponse response,
-      Object mappedValue)
-      throws Exception {
+      Object mappedValue) {
     HttpServletRequest httpServletRequest = (HttpServletRequest) request;
     // 验证码禁用 或不是表单提交 允许访问
-    if (captchaEnabled == false || !"post".equals(httpServletRequest.getMethod().toLowerCase())) {
+    if (!captchaEnabled || !"post".equals(httpServletRequest.getMethod().toLowerCase())) {
       return true;
     }
     return validateResponse(httpServletRequest,