laowo 3 éve
szülő
commit
09914e6a9d

+ 1 - 1
src/main/java/com/usky/config/CorsConfig.java

@@ -1,5 +1,6 @@
 package com.usky.config;
 
+
 import com.usky.filter.XssFilter;
 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 import org.springframework.context.annotation.Bean;
@@ -22,7 +23,6 @@ public class CorsConfig implements WebMvcConfigurer {
         registry.addMapping("/**").allowedOrigins("*").allowCredentials(true)
                 .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").maxAge(3600);
     }
-
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         /*静态资源的位置*/

+ 2 - 2
src/main/java/com/usky/config/shiro/MyRealm.java

@@ -43,9 +43,9 @@ public class MyRealm extends AuthorizingRealm {
     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
         SysUserVO user = ShiroUtils.getSysUserVo();
         // 角色列表
-        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()) {

+ 5 - 3
src/main/java/com/usky/config/shiro/ShiroConfig.java

@@ -151,17 +151,19 @@ public class ShiroConfig {
         shiroFilterFactoryBean.setSecurityManager(securityManager);
         // 拦截器
         // 配置不会被拦截的链接 顺序判断
-        //登录
-        shiroFilterFactoryBean.setLoginUrl("/sys/login");
         //控制 访问xx资源 需要xx权限
         Map<String, String> filterChainMap = new LinkedHashMap<>();
         //swagger接口权限 开放
         filterChainMap.put("/doc.html", "anon");
+        filterChainMap.put("/v2/**", "anon");
         filterChainMap.put("/webjars/**/**", "anon");
         filterChainMap.put("/swagger-ui.html", "anon");
         filterChainMap.put("/webjars/**", "anon");
-        filterChainMap.put("/v2/**", "anon");
         filterChainMap.put("/swagger-resources/**", "anon");
+        filterChainMap.put("/druid/**", "anon");
+        filterChainMap.put("/swagger**/**", "anon");
+        //登录
+        shiroFilterFactoryBean.setLoginUrl("/sys/login");
         //退出
         filterChainMap.put("/logout", "logout");
         filterChainMap.put("/static/**", "anon");

+ 5 - 2
src/main/java/com/usky/controller/login/LoginController.java

@@ -1,6 +1,5 @@
 package com.usky.controller.login;
 
-import com.usky.exception.user.UserPasswordNotMatchException;
 import com.usky.utils.Result;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -42,9 +41,13 @@ public class LoginController {
         } catch (AuthenticationException e) {
             String msg = "用户或密码错误";
             if (StringUtils.isEmpty(e.getMessage())) {
-                throw new UserPasswordNotMatchException();
+                throw new AuthenticationException("用户或密码错误!");
             }
             return Result.error(msg);
         }
     }
+
+
+  
+
 }

+ 0 - 100
src/main/java/com/usky/exception/BaseException.java

@@ -1,100 +0,0 @@
-package com.usky.exception;
-
-import com.usky.utils.SpringUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.context.MessageSource;
-import org.springframework.context.i18n.LocaleContextHolder;
-import sun.misc.MessageUtils;
-
-public class BaseException extends RuntimeException
-{
-    private static final long serialVersionUID = 1L;
-
-    /**
-     * 所属模块
-     */
-    private String module;
-
-    /**
-     * 错误码
-     */
-    private String code;
-
-    /**
-     * 错误码对应的参数
-     */
-    private Object[] args;
-
-    /**
-     * 错误消息
-     */
-    private String defaultMessage;
-
-    public BaseException(String module, String code, Object[] args, String defaultMessage)
-    {
-        this.module = module;
-        this.code = code;
-        this.args = args;
-        this.defaultMessage = defaultMessage;
-    }
-
-    public BaseException(String module, String code, Object[] args)
-    {
-        this(module, code, args, null);
-    }
-
-    public BaseException(String module, String defaultMessage)
-    {
-        this(module, null, null, defaultMessage);
-    }
-
-    public BaseException(String code, Object[] args)
-    {
-        this(null, code, args, null);
-    }
-
-    public BaseException(String defaultMessage)
-    {
-        this(null, null, null, defaultMessage);
-    }
-
-    @Override
-    public String getMessage()
-    {
-        String message = null;
-        if (!StringUtils.isEmpty(code))
-        {
-            message = message(code, args);
-        }
-        if (message == null)
-        {
-            message = defaultMessage;
-        }
-        return message;
-    }
-
-    public String getModule()
-    {
-        return module;
-    }
-
-    public String getCode()
-    {
-        return code;
-    }
-
-    public Object[] getArgs()
-    {
-        return args;
-    }
-
-    public String getDefaultMessage()
-    {
-        return defaultMessage;
-    }
-    public static String message(String code, Object... args)
-    {
-        MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
-        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
-    }
-}

+ 0 - 25
src/main/java/com/usky/exception/BusinessException.java

@@ -1,25 +0,0 @@
-package com.usky.exception;
-
-public class BusinessException extends RuntimeException
-{
-    private static final long serialVersionUID = 1L;
-
-    protected final String message;
-
-    public BusinessException(String message)
-    {
-        this.message = message;
-    }
-
-    public BusinessException(String message, Throwable e)
-    {
-        super(message, e);
-        this.message = message;
-    }
-
-    @Override
-    public String getMessage()
-    {
-        return message;
-    }
-}

+ 38 - 66
src/main/java/com/usky/exception/GloableExceptionResolver.java

@@ -1,19 +1,19 @@
 package com.usky.exception;
 
 import com.usky.utils.Result;
-import com.usky.utils.ServletUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.ShiroException;
+import org.apache.shiro.authc.IncorrectCredentialsException;
+import org.apache.shiro.authc.LockedAccountException;
+import org.apache.shiro.authc.UnknownAccountException;
 import org.apache.shiro.authz.AuthorizationException;
-import org.apache.shiro.util.PermissionUtils;
-import org.springframework.validation.BindException;
-import org.springframework.validation.BindingResult;
-import org.springframework.validation.FieldError;
+import org.apache.shiro.authz.UnauthorizedException;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.data.redis.connection.PoolException;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
-import org.springframework.web.servlet.ModelAndView;
-
-import javax.servlet.http.HttpServletRequest;
+import org.springframework.web.servlet.NoHandlerFoundException;
 
 /**
  * @author laowo
@@ -24,72 +24,44 @@ import javax.servlet.http.HttpServletRequest;
 @RestControllerAdvice
 @Slf4j
 public class GloableExceptionResolver {
-    /**
-     * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面
-     */
-    @ExceptionHandler(AuthorizationException.class)
-    public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e) {
+
+    @ExceptionHandler(Exception.class)
+    public Result<?> handleException(Exception e) {
         log.error(e.getMessage(), e);
-        if (ServletUtils.isAjaxRequest(request)) {
-            return Result.error("权限错误!");
-        } else {
-//            ModelAndView modelAndView = new ModelAndView();
-//            modelAndView.setViewName("error/unauth");
-//            return modelAndView;
-            return Result.error("权限错误!");
-        }
+        return Result.error("操作失败," + e.getMessage());
     }
-    /**
-     * 参数校验异常
-     */
-    @ExceptionHandler(value = BindException.class)
-    public Result<?> validationExceptionHandler(BindException e) {
-        BindingResult bindingResult = e.getBindingResult();
-        String errorMesssage = "";
-        for (FieldError fieldError : bindingResult.getFieldErrors()) {
-            errorMesssage += fieldError.getDefaultMessage() + "!";
-        }
-        return Result.error(errorMesssage);
-    }
-    /**
-     * 请求方式不支持
-     */
-    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
-    public Result handleException(HttpRequestMethodNotSupportedException e) {
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public Result<?> HttpRequestMethodNotSupportedException(Exception e) {
         log.error(e.getMessage(), e);
-        return Result.error("不支持' " + e.getMethod() + "'请求");
-    }
-    /**
-     * 拦截未知的运行时异常
-     */
-    @ExceptionHandler(RuntimeException.class)
-    public Result notFount(RuntimeException e) {
-        log.error("运行时异常:", e);
-        return Result.error("运行时异常:" + e.getMessage());
+        return Result.error("请求方式异常," + e.getMessage());
     }
-    /**
-     * 系统异常
-     */
-    @ExceptionHandler(Exception.class)
-    public Result handleException(Exception e) {
+    @ExceptionHandler(DataIntegrityViolationException.class)
+    public Result<?> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
         log.error(e.getMessage(), e);
-        return Result.error("服务器错误,请联系管理员");
+        return Result.error("字段太长,超出数据库字段的长度");
     }
-    /**
-     * 业务异常
-     */
-    @ExceptionHandler(BusinessException.class)
-    public Object businessException(HttpServletRequest request, BusinessException e) {
+
+    @ExceptionHandler(PoolException.class)
+    public Result<?> handlePoolException(PoolException e) {
         log.error(e.getMessage(), e);
-        if (ServletUtils.isAjaxRequest(request)) {
-            return Result.error(e.getMessage());
-        } else {
-            // ModelAndView modelAndView = new ModelAndView();
-            // modelAndView.addObject("errorMessage", e.getMessage());
-            // modelAndView.setViewName("error/business");
-            // return modelAndView;
-            return Result.error(e.getMessage());
+        return Result.error("Redis 连接异常!");
+    }
+
+
+    @ExceptionHandler(ShiroException.class)
+    public Result<?> doHandleShiroException(
+            ShiroException e) {
+        if (e instanceof UnknownAccountException) {
+            return Result.error("账户不存在");
+        } else if (e instanceof LockedAccountException) {
+            return Result.error("账户被禁用");
+        } else if (e instanceof IncorrectCredentialsException) {
+            return Result.error("密码不正确");
+        } else if (e instanceof AuthorizationException) {
+            return Result.error("没有此操作权限");
         }
+        return Result.error("系统维护中");
+
     }
 
 

+ 0 - 18
src/main/java/com/usky/exception/user/CaptchaException.java

@@ -1,18 +0,0 @@
-package com.usky.exception.user;
-
-import com.usky.exception.BaseException;
-
-/**
- * 验证码错误异常类
- * 
- * @author 
- */
-public class CaptchaException extends BaseException
-{
-    private static final long serialVersionUID = 1L;
-
-    public CaptchaException()
-    {
-        super("验证码错误");
-    }
-}

+ 0 - 18
src/main/java/com/usky/exception/user/RoleBlockedException.java

@@ -1,18 +0,0 @@
-package com.usky.exception.user;
-
-import com.usky.exception.BaseException;
-
-/**
- * 角色锁定异常类
- * 
- * @author 
- */
-public class RoleBlockedException extends BaseException
-{
-    private static final long serialVersionUID = 1L;
-
-    public RoleBlockedException()
-    {
-        super("角色错误");
-    }
-}

+ 0 - 14
src/main/java/com/usky/exception/user/UserBlockedException.java

@@ -1,14 +0,0 @@
-package com.usky.exception.user;
-
-/**
- * 用户锁定异常类
- *
- * @author 
- */
-public class UserBlockedException extends UserException {
-    private static final long serialVersionUID = 1L;
-
-    public UserBlockedException() {
-        super("账户已锁定");
-    }
-}

+ 0 - 18
src/main/java/com/usky/exception/user/UserDeleteException.java

@@ -1,18 +0,0 @@
-package com.usky.exception.user;
-
-import com.usky.exception.BaseException;
-
-/**
- * 用户账号已被删除
- * 
- * @author 
- */
-public class UserDeleteException extends BaseException
-{
-    private static final long serialVersionUID = 1L;
-
-    public UserDeleteException()
-    {
-        super("用户账户已删除");
-    }
-}

+ 0 - 19
src/main/java/com/usky/exception/user/UserException.java

@@ -1,19 +0,0 @@
-package com.usky.exception.user;
-
-
-import com.usky.exception.BaseException;
-
-/**
- * 用户信息异常类
- * 
- * @author 
- */
-public class UserException extends BaseException
-{
-    private static final long serialVersionUID = 1L;
-
-    public UserException(String message)
-    {
-        super(message);
-    }
-}

+ 0 - 16
src/main/java/com/usky/exception/user/UserNotExistsException.java

@@ -1,16 +0,0 @@
-package com.usky.exception.user;
-
-import com.usky.exception.BaseException;
-
-/**
- * 用户不存在异常类
- *
- * @author 
- */
-public class UserNotExistsException extends BaseException {
-    private static final long serialVersionUID = 1L;
-
-    public UserNotExistsException() {
-        super("用户不存在");
-    }
-}

+ 0 - 14
src/main/java/com/usky/exception/user/UserPasswordNotMatchException.java

@@ -1,14 +0,0 @@
-package com.usky.exception.user;
-
-/**
- * 用户密码不正确或不符合规范异常类
- *
- * @author 
- */
-public class UserPasswordNotMatchException extends UserException {
-    private static final long serialVersionUID = 1L;
-
-    public UserPasswordNotMatchException() {
-        super("用户名或密码错误!");
-    }
-}

+ 0 - 16
src/main/java/com/usky/exception/user/UserPasswordRetryLimitCountException.java

@@ -1,16 +0,0 @@
-package com.usky.exception.user;
-
-/**
- * 用户错误记数异常类
- * 
- * @author 
- */
-public class UserPasswordRetryLimitCountException extends UserException
-{
-    private static final long serialVersionUID = 1L;
-
-    public UserPasswordRetryLimitCountException(int retryLimitCount)
-    {
-        super("user.password.retry.limit.count");
-    }
-}

+ 0 - 16
src/main/java/com/usky/exception/user/UserPasswordRetryLimitExceedException.java

@@ -1,16 +0,0 @@
-package com.usky.exception.user;
-
-/**
- * 用户错误最大次数异常类
- * 
- * @author 
- */
-public class UserPasswordRetryLimitExceedException extends UserException
-{
-    private static final long serialVersionUID = 1L;
-
-    public UserPasswordRetryLimitExceedException(int retryLimitCount)
-    {
-        super("user.password.retry.limit.exceed");
-    }
-}

+ 2 - 2
src/main/java/com/usky/service/sys/user/LoginServiceImpl.java

@@ -5,9 +5,9 @@ import com.usky.dao.impl.BaseDaoImpl;
 import com.usky.entity.sys.SysRoleDTO;
 import com.usky.entity.sys.SysUserDTO;
 import com.usky.entity.sys.vo.SysUserVO;
-import com.usky.exception.user.UserPasswordNotMatchException;
 import com.usky.utils.BeanHelp;
 import com.usky.utils.ListUtil;
+import org.apache.shiro.authc.AuthenticationException;
 import org.hibernate.transform.Transformers;
 import org.springframework.stereotype.Service;
 
@@ -27,7 +27,7 @@ public class LoginServiceImpl extends BaseDaoImpl implements LoginService {
 
         List<SysUserDTO> list = getSession().createQuery("from SysUserDTO where loginName ='" + username + "'").list();
         if (ListUtil.isBlank(list)) {
-            throw new UserPasswordNotMatchException();
+            throw new AuthenticationException("未登录!");
         }
         List<SysRoleDTO> sysRoleDTOS = queryRoleByLoginName(username);
         List<SysUserVO> sysUserVOS = BeanHelp.copyWithCollection(list, SysUserVO.class);

+ 2 - 4
src/main/java/com/usky/utils/ShiroUtils.java

@@ -2,15 +2,13 @@ package com.usky.utils;
 
 import com.usky.entity.sys.SysUserDTO;
 import com.usky.entity.sys.vo.SysUserVO;
-import com.usky.exception.BusinessException;
-import com.usky.exception.user.UserException;
 import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.crypto.SecureRandomNumberGenerator;
 import org.apache.shiro.session.Session;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.apache.shiro.subject.SimplePrincipalCollection;
 import org.apache.shiro.subject.Subject;
-import org.springframework.beans.BeanUtils;
 
 public class ShiroUtils {
     public static Subject getSubject() {
@@ -44,7 +42,7 @@ public class ShiroUtils {
           //  BeanUtils.copyProperties(user, obj);
             user = (SysUserVO) getSubject().getPrincipal();
         }else {
-            throw new BusinessException("用户未登录");
+            throw new AuthenticationException("token为空!");
         }
         return user;
     }