This commit is contained in:
xiangwb
2017-11-07 09:18:24 +08:00
parent f85d2a825c
commit c7a1e35989
18 changed files with 446 additions and 203 deletions
@@ -4,11 +4,12 @@ import com.alibaba.fastjson.JSONObject;
import com.xwbing.service.MailService;
import com.xwbing.util.JSONObjResult;
import com.xwbing.util.RestMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 项目名称: sb-boot-module-demo
* 创建时间: 2017/9/18 13:42
@@ -18,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/mail/")
public class MailTestControl {
@Autowired
@Resource
private MailService mailService;
/**
@@ -12,11 +12,11 @@ import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;
import java.util.List;
/**
* 说明: 系统配置控制层
@@ -28,31 +28,31 @@ import java.util.Objects;
@RestController
@RequestMapping("/config/")
public class SysConfigControl {
private static Logger log = LoggerFactory.getLogger(SysConfigControl.class);
@Autowired
private final Logger logger = LoggerFactory.getLogger(SysConfigControl.class);
@Resource
private SysConfigService sysConfigService;
@ApiOperation(value = "新增", notes = "新增系统配置信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "key", value = "配置项的key,长度为1-50", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "code", value = "配置项的key,长度为1-50", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "value", value = "配置项的值", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "isEnable", value = "是否启用,格式为Y|N", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "enable", value = "是否启用,格式为Y|N", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "name", value = "配置名称,长度为1-10", paramType = "query", required = true, dataType = "string")
})
@PostMapping("save")
public JSONObject save(@Valid SysConfig sysConfig) {
public JSONObject save(@RequestBody @Valid SysConfig sysConfig) {
String logMsg = "新增系统配置信息";
log.info(logMsg);
logger.info(logMsg);
RestMessage result = sysConfigService.save(sysConfig);
return JSONObjResult.toJSONObj(result);
}
@ApiOperation(value = "删除", notes = "根据code删除系统配置信息")
@ApiImplicitParam(name = "key", value = "配置项的code", paramType = "query", required = true, dataType = "string")
@PostMapping("removeByCode/{code}")
@ApiImplicitParam(name = "code", value = "配置项的code", paramType = "query", required = true, dataType = "string")
@GetMapping("removeByCode/{code}")
public JSONObject removeByCode(@PathVariable String code) {
String logMsg = "删除系统配置信息";
log.info(logMsg + " key:{}", code);
logger.info(logMsg + " code:{}", code);
if (StringUtils.isEmpty(code)) {
return JSONObjResult.toJSONObj("code不能为空");
}
@@ -60,19 +60,48 @@ public class SysConfigControl {
return JSONObjResult.toJSONObj(result);
}
@ApiOperation(value = "修改", notes = "修改系统配置信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "code", value = "配置项的key,长度为1-50", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "value", value = "配置项的值", paramType = "query", required = true, dataType = "string"),
@ApiImplicitParam(name = "enable", value = "是否启用,格式为Y|N", paramType = "query", required = true, dataType = "string"),
})
@PostMapping("update")
public JSONObject update(@RequestBody SysConfig sysConfig) {
String logMsg = "修改系统配置信息";
logger.info(logMsg);
if (StringUtils.isEmpty(sysConfig.getCode()))
return JSONObjResult.toJSONObj("配置项的code不能为空");
RestMessage result = sysConfigService.update(sysConfig);
return JSONObjResult.toJSONObj(result);
}
@ApiOperation(value = "查找", notes = "根据key查找系统配置信息")
@ApiImplicitParam(name = "key", value = "配置项的code", paramType = "query", required = true, dataType = "string")
@PostMapping("findByCode")
@ApiImplicitParam(name = "code", value = "配置项的code", paramType = "query", required = true, dataType = "string")
@GetMapping("findByCode")
public JSONObject findByCode(@RequestParam String code) {
String logMsg = "根据code查找系统配置信息";
log.info(logMsg + " key:{}", code);
logger.info(logMsg + " code:{}", code);
if (StringUtils.isEmpty(code)) {
return JSONObjResult.toJSONObj("code不能为空");
}
SysConfig one = sysConfigService.findByKey(code);
if (Objects.isNull(one)) {
return JSONObjResult.toJSONObj("该对象不存在");
SysConfig one = sysConfigService.findByCode(code);
if (one == null) {
return JSONObjResult.toJSONObj("该配置不存在");
}
return JSONObjResult.toJSONObj(one, true, "");
}
@ApiOperation(value = "查找列表", notes = "根据是否启用查找配置列表")
@ApiImplicitParam(name = "enable", value = "是否启用,格式为Y|N", paramType = "query", required = true, dataType = "string")
@GetMapping("listByEnable")
public JSONObject findByEnable(@RequestParam String enable) {
String logMsg = "根据是否启用查找配置列表";
logger.info(logMsg + " enable:{}", enable);
if (StringUtils.isEmpty(enable)) {
return JSONObjResult.toJSONObj("是否启用不能为空");
}
List<SysConfig> byEnable = sysConfigService.findByEnable(enable);
return JSONObjResult.toJSONObj(byEnable, true, "");
}
}
@@ -4,11 +4,12 @@ import com.xwbing.annotation.LogInfo;
import com.xwbing.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 说明:
* 项目名称: boot-module-demo
@@ -18,7 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test/")
public class TestControl {
@Autowired
@Resource
private RedisService redisService;
private final Logger logger = LoggerFactory.getLogger(TestControl.class);
@@ -1,23 +1,20 @@
package com.xwbing.controller;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.annotation.LogInfo;
import com.xwbing.entity.SysUser;
import com.xwbing.service.UserService;
import com.xwbing.util.JSONObjResult;
import com.xwbing.util.RestMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
/**
* 说明: user
* 说明: 用户控制层
* 项目名称: boot-module-demo
* 创建时间: 2017/5/10 16:36
* 作者: xiangwb
@@ -25,17 +22,30 @@ import java.util.Objects;
@RestController
@RequestMapping("/user/")
public class UserControl {
private final static Logger log = LoggerFactory.getLogger(UserControl.class);
@Autowired
@Resource
private UserService userService;
/**
* 添加用户
*
* @param sysUser
* @return
*/
@LogInfo("添加用户")
@PostMapping("save")
public JSONObject save(@Valid SysUser sysUser) {
public JSONObject save(@RequestBody @Valid SysUser sysUser) {
RestMessage result = userService.save(sysUser);
return JSONObjResult.toJSONObj(result);
}
@PostMapping("removeById")
/**
* 删除用户
*
* @param id
* @return
*/
@LogInfo("删除用户")
@GetMapping("removeById")
public JSONObject removeById(@RequestParam String id) {
if (StringUtils.isEmpty(id)) {
return JSONObjResult.toJSONObj("主键不能为空");
@@ -44,8 +54,15 @@ public class UserControl {
return JSONObjResult.toJSONObj(result);
}
/**
* 修改用户信息
*
* @param sysUser
* @return
*/
@LogInfo("修改用户信息")
@PostMapping("update")
public JSONObject update(SysUser sysUser) {
public JSONObject update(@RequestBody @Valid SysUser sysUser) {
if (StringUtils.isEmpty(sysUser.getId())) {
return JSONObjResult.toJSONObj("主键不能为空");
}
@@ -53,41 +70,80 @@ public class UserControl {
return JSONObjResult.toJSONObj(result);
}
/**
* 获取用户详情
*
* @param id
* @return
*/
@LogInfo("获取用户详情")
@GetMapping("findById")
public JSONObject findById(@RequestParam String id) {
if (StringUtils.isEmpty(id)) {
return JSONObjResult.toJSONObj("主键不能为空");
}
SysUser sysUser = userService.findOne(id);
if (Objects.isNull(sysUser)) {
if (sysUser == null) {
return JSONObjResult.toJSONObj("未查到该对象");
}
return JSONObjResult.toJSONObj(sysUser, true, "");
}
/**
* 列表查询所有用户
*
* @return
*/
@LogInfo("列表查询所有用户")
@GetMapping("findList")
public JSONObject findList() {
List<SysUser> list = userService.listAll();
return JSONObjResult.toJSONObj(list, true, "");
}
/**
* 登录
*
* @param userName
* @param passWord
* @param checkCode
* @return
*/
@LogInfo("登录")
@GetMapping("login")
public JSONObject login(HttpServletRequest req, @RequestParam String userName, @RequestParam String passWord,@RequestParam String checkCode) {
public JSONObject login(@RequestParam String userName, @RequestParam String passWord, @RequestParam String checkCode) {
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord))
return JSONObjResult.toJSONObj("用户名或密码不能为空");
if (StringUtils.isEmpty(checkCode) )
if (StringUtils.isEmpty(checkCode))
return JSONObjResult.toJSONObj("请输入验证码");
RestMessage login = userService.login(req, userName, passWord, checkCode);
RestMessage login = userService.login(userName, passWord, checkCode);
return JSONObjResult.toJSONObj(login);
}
/**
* 修改密码
*
* @param newPassWord
* @param oldPassWord
* @return
*/
@LogInfo("修改密码")
@GetMapping("updatePassWord")
public JSONObject updatePassWord(HttpServletRequest request, @RequestParam String newPassWord, @RequestParam String oldPassWord) {
if (StringUtils.isEmpty(newPassWord))
return JSONObjResult.toJSONObj("新密码不能为空");
if (StringUtils.isEmpty(oldPassWord))
return JSONObjResult.toJSONObj("旧密码不能为空");
RestMessage restMessage = userService.updatePassWord(request, newPassWord, oldPassWord);
public JSONObject updatePassWord(@RequestParam String newPassWord, @RequestParam String oldPassWord, @RequestParam String id) {
if (StringUtils.isEmpty(newPassWord) || StringUtils.isEmpty(oldPassWord))
return JSONObjResult.toJSONObj("原密码或新密码不能为空");
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
RestMessage restMessage = userService.updatePassWord(newPassWord, oldPassWord, id);
return JSONObjResult.toJSONObj(restMessage);
}
@LogInfo("重置密码")
@GetMapping("resetPassWord")
public JSONObject resetPassWord(@RequestParam String id) {
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
RestMessage restMessage = userService.resetPassWord(id);
return JSONObjResult.toJSONObj(restMessage);
}
}
@@ -7,7 +7,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -56,11 +58,9 @@ public class GlobalExceptionHandler {
*/
@ExceptionHandler(value = BindException.class)
@ResponseBody
public JSONObject handlerBindException(HttpServletRequest request,
HttpServletResponse response, BindException ex) {
public JSONObject handlerBindException(HttpServletRequest request, HttpServletResponse response, BindException ex) {
List<ObjectError> list = ex.getAllErrors();
StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuffer = new StringBuilder();
for (ObjectError objectError : list) {
if (stringBuffer.length() > 0)
stringBuffer.append(" && ");
@@ -71,6 +71,29 @@ public class GlobalExceptionHandler {
return JSONObjResult.toJSONObj(stringBuffer.toString());
}
/**
* 表单检验(validator) 异常 (@RequestBody)
*
* @param ex
* @return
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public JSONObject handlerMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
StringBuilder stringBuffer = new StringBuilder();
for (ObjectError objectError : allErrors) {
if (stringBuffer.length() > 0)
stringBuffer.append(" && ");
stringBuffer.append(objectError.getDefaultMessage());
}
logger.error(stringBuffer.toString());
return JSONObjResult.toJSONObj(stringBuffer.toString());
}
/**
* @param request
* @param ex
@@ -2,13 +2,13 @@ package com.xwbing.handler;
import com.xwbing.configuration.DispatcherServletConfig;
import com.xwbing.constant.CommonConstant;
import com.xwbing.util.CommonDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;
@@ -21,20 +21,27 @@ import java.util.Set;
public class LoginInterceptor extends HandlerInterceptorAdapter {
private final Logger logger = LoggerFactory.getLogger(DispatcherServletConfig.class);
private static final Set<String> set = new HashSet<>();//拦截器白名单
static {
set.add("/v2/api-docs");
set.add("/servlet/captchaCode");
set.add("/swagger-ui.html");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if(!set.contains(servletPath) && !servletPath.contains("login")){
HttpSession session = request.getSession();
if (session.getAttribute(CommonConstant.SESSION_CURRENT_USER)!=null) {
if (!set.contains(servletPath) && !servletPath.contains("login")) {
// HttpSession session = request.getSession();
// if (session.getAttribute(CommonConstant.CURRENT_USER)!=null) {
// return true;
// }else {
// return false;
// }
if (CommonDataUtil.getToken(CommonConstant.CURRENT_USER) != null) {
// TODO: 2017/10/3
return true;
}else {
} else {
// TODO: 2017/10/3
return false;
}