内部轻量级BUG管理系统<四>

企业内部开发经常遇到的一个问题是,bug追踪问题,一些公司使用excel表格追踪问题,虽然可以,但是随着业务的增加,变的难以维护。为了有效地管理bug,我们需要要一个强大的错误跟踪解决方案,比如现代化的缺陷跟踪管理软件,它们可以跟踪每个项目阶段的bug,从开发过程到软件测试和发布阶段。

权限处理

首先写两个Controller:AuthController(用于带有权限的控制器),CliController

package com.dguo.web.base.controller;

/**
 * dguo,2021/9/2
 */
public class CliController {

}



package com.dguo.web.base.controller;

import com.dguo.web.domain.User;

/**
 * dguo,2021/9/2
 */
public class AuthController {
    public static final String LOGIN_SYS_COOKIE = "LSC";
    private ThreadLocal<User> tl = new ThreadLocal<User>();

    public User getUser(){
        return tl.get();
    }

    public void setUser(User user) {
        tl.set(user);
    }
}
package com.dguo.web.base.interceptor;

import com.dguo.web.base.controller.AuthController;
import com.dguo.web.base.controller.CliController;
import com.dguo.web.base.exception.AuthException;
import com.dguo.web.base.exception.ErrorException;
import com.dguo.web.domain.Res;
import com.dguo.web.domain.User;
import com.dguo.web.service.ResService;
import com.dguo.web.service.UserService;
import com.dguo.web.utils.CookieUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * dguo,2021/9/2
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;
    @Autowired
    private ResService resService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if(! (handler instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Object o = handlerMethod.getBean();
        String methodName = handlerMethod.getMethod().getName();
        String clazzName = handlerMethod.getBeanType().getName();
        if (o instanceof CliController) {
            return true;
        } else if (o instanceof AuthController) {
            String cookieValue = CookieUtils.getCookieValue(request, AuthController.LOGIN_SYS_COOKIE);
            if (StringUtils.isNotBlank(cookieValue)) {
                User user = userService.getUserWithToken(cookieValue);
                if(user != null){
                    Res res = resService.checkWithUser(2, clazzName + "#" + methodName, user.getCode());
                    if(res != null){
                        AuthController m = (AuthController) o;
                        m.setUser(user);
                        return true;
                    }
                }
            }
            throw new AuthException();
        }else{
            throw new ErrorException();
        }
    }
}

还需要两个服务,分别获取用户信息与资源信息。

创建接口

package com.dguo.web.service;

import com.dguo.web.base.Page;
import com.dguo.web.domain.User;


/**
 * dguo,2021/9/1
 */
public interface UserService {
    int add(User user,String roles);

    int edit(User user,String roles);

    int editInfo(User user);

    User login(String userName, String password);

    int logout(User user);

    User getUserWithToken(String token);

    User getUserWithId(Long id);

    Page<User> list(int pageNum, int pageSize, String userName);

}


package com.dguo.web.service;

import com.dguo.web.base.Page;
import com.dguo.web.domain.Res;

/**
 * dguo,2021/9/6
 */
public interface ResService {

    int add(Res res,String roles);

    Res getWithId(Long id);

    int edit(Res res,String roles);

    Res checkWithUser(int status,String url,int code);

    Page<Res> list(int pageNum,int pageSize,String name);
}

生成实现类

package com.dguo.web.base;

/**
 * dguo,2021/9/8
 */
public enum ExceptionCode {
    req_url_null("1000","请求url不存在")
    ,add_user_username_null("1001","用户名为空")
    ,add_user_password_null("1002","密码为空")
    ,add_user_rols_null("1003","没有用户角色")
    ,add_user_exist("1004","用户名已经存在")
    ,edit_user_notexist("1005","用户不存在")
    ,add_role_name_null("2000","名称不能为空")
    ,add_menu_name_null("3000","没有菜单名称")
    ,add_menu_url_null("3001","没有菜单地址")
    ,add_menu_role_null("3002","没有分配角色")
    ,edit_menu_notexist("3003","菜单对象不存在")
    ,add_res_name_null("4000","资源名称不能为空")
    ,add_res_url_null("4001","处理方法不能为空")
    ,add_res_url_exist("4002","处理方法不能重复")
    ,add_res_role_null("4003","没有分配角色")
    ,edit_res_notexist("4004","资源对象不存在")
    ,add_project_name_null("5000","项目名称不能为空")
    ,edit_project_notexist("5001","项目对象不存在")
    ,add_member_null("6000","增加组员为空")
    ,add_bug_description_null("7000","没有bug描述")
    ;

    private String code;
    private String msg;

    ExceptionCode(String code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}



package com.dguo.web.service.impl;

import com.dguo.web.base.ExceptionCode;
import com.dguo.web.base.Page;
import com.dguo.web.base.exception.ServiceException;
import com.dguo.web.domain.User;
import com.dguo.web.mapper.UserMapper;
import com.dguo.web.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;

/**
 * dguo,2021/9/1
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int add(User user,String roles) {
        if(StringUtils.isBlank(user.getUserName())){
            throw new ServiceException(ExceptionCode.add_user_username_null);
        }
        if(StringUtils.isBlank(user.getPassword())){
            throw new ServiceException(ExceptionCode.add_user_password_null);
        }
        if(StringUtils.isBlank(roles)){
            throw new ServiceException(ExceptionCode.add_user_rols_null);
        }
        String[] split = StringUtils.split(roles, ",");
        if(split.length ==0){
            throw new ServiceException(ExceptionCode.add_user_rols_null);
        }
        User q = new User();
        q.setStatus(0);
        q.setUserName(user.getUserName());
        User user1 = userMapper.selectOne(q);
        if(user1 !=null){
            throw new ServiceException(ExceptionCode.add_user_exist);
        }
        int code = 0;
        for(int i=0;i<split.length;i++){
            code = code | Integer.valueOf(split[i]);
        }
        user.setCode(code);
        user.setRegTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insert(user);
        return 0;
    }

    @Override
    public int edit(User user, String roles) {
        if(user.getId() ==null){
            throw new ServiceException(ExceptionCode.edit_user_notexist);
        }
        User user1 = userMapper.get(user.getId());
        if(user1 == null){
            throw new ServiceException(ExceptionCode.edit_user_notexist);
        }
        if(StringUtils.isBlank(user.getPassword())){
            throw new ServiceException(ExceptionCode.add_user_password_null);
        }
        if(StringUtils.isBlank(roles)){
            throw new ServiceException(ExceptionCode.add_user_rols_null);
        }
        String[] split = StringUtils.split(roles, ",");
        if(split.length ==0){
            throw new ServiceException(ExceptionCode.add_user_rols_null);
        }
        int code = 0;
        for(int i=0;i<split.length;i++){
            code = code | Integer.valueOf(split[i]);
        }
        user.setCode(code);
        userMapper.update(user);
        return 0;
    }

    @Override
    public int editInfo(User user) {
        if(user.getId() ==null){
            throw new ServiceException(ExceptionCode.edit_user_notexist);
        }
        User user1 = userMapper.get(user.getId());
        if(user1 == null){
            throw new ServiceException(ExceptionCode.edit_user_notexist);
        }
        if(StringUtils.isBlank(user.getPassword())){
            throw new ServiceException(ExceptionCode.add_user_password_null);
        }
        userMapper.update(user);
        return 0;
    }

    @Override
    public User login(String userName, String password) {
        User q = new User();
        q.setUserName(userName);
        q.setPassword(password);
        q.setStatus(2);
        User user = userMapper.selectOne(q);
        if(user != null){
            String token = DigestUtils.md5DigestAsHex((userName + RandomStringUtils.randomNumeric(4) + password).getBytes());
            User update = new User();
            update.setId(user.getId());
            update.setToken(token);
            userMapper.update(update);
            return update;
        }
        return null;
    }

    @Override
    public int logout(User user) {
        if(user != null){
            User update = new User();
            update.setId(user.getId());
            String token = DigestUtils.md5DigestAsHex((RandomStringUtils.randomAscii(32)).getBytes());
            update.setToken(token);
            userMapper.update(update);
        }
        return 0;
    }

    @Override
    public User getUserWithToken(String token) {
        User q = new User();
        q.setToken(token);
        q.setStatus(2);
        return userMapper.selectOne(q);
    }

    @Override
    public User getUserWithId(Long id) {
        return userMapper.get(id);
    }

    @Override
    public Page<User> list(int pageNum,int pageSize,String userName) {
        if(StringUtils.isBlank(userName)){
            userName=null;
        }
        Page<User> page = new Page<>(pageNum, pageSize);
        page.setItems(userMapper.list(page.getSqlNum(),page.getSqlSize(),userName));
        page.setTotal(userMapper.count(userName));
        return page;
    }
}


package com.dguo.web.service.impl;

import com.dguo.web.base.ExceptionCode;
import com.dguo.web.base.Page;
import com.dguo.web.base.exception.ServiceException;
import com.dguo.web.domain.Res;
import com.dguo.web.mapper.ResMapper;
import com.dguo.web.service.ResService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * dguo,2021/9/6
 */
@Service
public class ResServiceImpl implements ResService {
    @Autowired
    private ResMapper resMapper;

    @Override
    public int add(Res res, String roles) {
        if(StringUtils.isBlank(res.getName())){
            throw new ServiceException(ExceptionCode.add_res_name_null);
        }
        if(StringUtils.isBlank(res.getUrl())){
            throw new ServiceException(ExceptionCode.add_res_url_null);
        }
        if(StringUtils.isBlank(roles)){
            throw new ServiceException(ExceptionCode.add_res_role_null);
        }
        String[] split = StringUtils.split(roles, ",");
        if(split.length ==0){
            throw new ServiceException(ExceptionCode.add_res_role_null);
        }
        Res q = new Res();
        q.setUrl(res.getUrl());
        q.setStatus(0);
        Res res1 = resMapper.selectOne(res);
        if(res1 != null){
            throw new ServiceException(ExceptionCode.add_res_url_exist);
        }
        int code = 0;
        for(int i=0;i<split.length;i++){
            code = code | Integer.valueOf(split[i]);
        }
        res.setCode(code);
        res.setUpdateTime(new Date());
        resMapper.insert(res);
        return 0;
    }

    @Override
    public Res getWithId(Long id) {
        return resMapper.get(id);
    }

    @Override
    public int edit(Res res, String roles) {
        if(res.getId() ==null){
            throw new ServiceException(ExceptionCode.edit_res_notexist);
        }
        Res q = resMapper.get(res.getId());
        if(q == null){
            throw new ServiceException(ExceptionCode.edit_res_notexist);
        }
        if(StringUtils.isBlank(res.getName())){
            throw new ServiceException(ExceptionCode.add_res_name_null);
        }
        if(StringUtils.isBlank(roles)){
            throw new ServiceException(ExceptionCode.add_res_role_null);
        }
        String[] split = StringUtils.split(roles, ",");
        if(split.length ==0){
            throw new ServiceException(ExceptionCode.add_res_role_null);
        }

        int code = 0;
        for(int i=0;i<split.length;i++){
            code = code | Integer.valueOf(split[i]);
        }
        res.setCode(code);
        resMapper.update(res);
        return 0;
    }

    @Override
    public Res checkWithUser(int status, String url, int code) {
        if (StringUtils.isBlank(url)) {
            throw new ServiceException(ExceptionCode.req_url_null);
        }
        return resMapper.getWithCode(status, url, code);
    }

    @Override
    public Page<Res> list(int pageNum, int pageSize, String name) {
        if (StringUtils.isBlank(name)) {
            name = null;
        }
        Page<Res> page = new Page<>(pageNum, pageSize);
        page.setItems(resMapper.list(page.getSqlNum(), page.getSqlSize(), name));
        page.setTotal(resMapper.count(name));
        return page;
    }
}

异常处理

package com.dguo.web.base.exception;

import org.springframework.web.servlet.ModelAndView;

/**
 * dguo,2021/9/3
 */
public class AuthException extends RuntimeException {
    public ModelAndView result(){
        ModelAndView mav = new ModelAndView("error/auth");
        return mav;
    }
}


package com.dguo.web.base.exception;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

/**
 * dguo,2021/9/3
 */
public class ErrorException extends RuntimeException {
    public ErrorException() {
        super();
    }

    public ModelAndView result(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        String msg = (String) request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
        ModelAndView mav = new ModelAndView("error/runtime_error");
        mav.addObject("statusCode", statusCode);
        mav.addObject("msg", msg);
        return mav;
    }
}


package com.dguo.web.base.exception;

import com.dguo.web.base.ExceptionCode;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

/**
 * dguo,2021/9/6
 */
public class ServiceException extends RuntimeException {
    private String msg;
    private String status;
    public ServiceException(ExceptionCode code){
        this.msg = code.getMsg();
        this.status = code.getCode();
    }
    public ModelAndView result(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("error/runtime_error");
        mav.addObject("statusCode", this.status);
        mav.addObject("msg",this.msg);
        mav.addObject("url",request.getHeader("Referer") );
        return mav;
    }
}


package com.dguo.web.base.controller;

import com.dguo.web.base.exception.AuthException;
import com.dguo.web.base.exception.ErrorException;
import com.dguo.web.base.exception.ServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * dguo,2021/9/3
 */
@ControllerAdvice
public class XExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public ModelAndView ServiceException(ServiceException e,HttpServletRequest request){
        return e.result(request);
    }

    @ExceptionHandler(ErrorException.class)
    public ModelAndView ErrorException(ErrorException e,HttpServletRequest request){
        return e.result(request);
    }

    @ExceptionHandler(AuthException.class)
    public ModelAndView authException(AuthException e){
        return e.result();
    }
}

明天实现全部的服务方法,控制器。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

俗C人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值