权限和用户管理开发

This commit is contained in:
暮光:城中城
2018-12-15 22:32:28 +08:00
parent 6fca0f89ff
commit 66513ac91d
20 changed files with 1440 additions and 451 deletions

View File

@@ -1,42 +1,47 @@
package com.zyplayer.doc.manage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.util.Optional;
/**
* 程序启动器
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(Application.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
Environment env = application.getEnvironment();
String contextPath = env.getProperty("server.servlet.context-path");
contextPath = Optional.ofNullable(contextPath).orElse("").replaceFirst("/", "");
contextPath = (contextPath.length() <= 0 || contextPath.endsWith("/")) ? contextPath : contextPath + "/";
logger.info("\n----------------------------------------------------------\n\t" +
"\t\t地址列表\n\t" +
"文档地址http://{}:{}/{}document.html\n" +
"----------------------------------------------------------",
InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), contextPath
);
}
}
package com.zyplayer.doc.manage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.util.Optional;
/**
* 程序启动器
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(Application.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
Environment env = application.getEnvironment();
String contextPath = env.getProperty("server.servlet.context-path");
contextPath = Optional.ofNullable(contextPath).orElse("").replaceFirst("/", "");
contextPath = (contextPath.length() <= 0 || contextPath.endsWith("/")) ? contextPath : contextPath + "/";
String hostAddress = InetAddress.getLocalHost().getHostAddress();
String serverPort = env.getProperty("server.port");
logger.info("\n----------------------------------------------------------\n\t" +
"\t\t地址列表\n\t" +
"文档地址http://{}:{}/{}document.html\n\t" +
//"数据库地址http://{}:{}/{}document.html\n " +
"管理地址http://{}:{}/{}statics/manage/home.html\n" +
"----------------------------------------------------------",
hostAddress, serverPort, contextPath,
hostAddress, serverPort, contextPath
);
}
}

View File

@@ -0,0 +1,67 @@
package com.zyplayer.doc.manage.framework.config;
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Configuration
public class MapperConfig {
@Bean
public Mapper dozerBeanMapper() {
DozerBeanMapperBuilder builder = DozerBeanMapperBuilder.create()
.withCustomConverter(new DateStringConvert(Date.class, String.class))
.withCustomConverter(new BigdecimalToStringConvert(BigDecimal.class, String.class));
return builder.build();
// return DozerBeanMapperBuilder.buildDefault();
}
private class DateStringConvert extends DozerConverter<Date, String> {
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public DateStringConvert(Class<Date> prototypeA, Class<String> prototypeB) {
super(prototypeA, prototypeB);
}
@Override
public String convertTo(Date source, String destination) {
destination = dateFormat.format(source);
return destination;
}
@Override
public Date convertFrom(String source, Date destination) {
try {
destination = dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return destination;
}
}
private class BigdecimalToStringConvert extends DozerConverter<BigDecimal, String> {
public BigdecimalToStringConvert(Class<BigDecimal> prototypeA, Class<String> prototypeB) {
super(prototypeA, prototypeB);
}
@Override
public String convertTo(BigDecimal source, String destination) {
return source.toString();
}
@Override
public BigDecimal convertFrom(String source, BigDecimal destination) {
return BigDecimal.valueOf(Double.parseDouble(source));
}
}
}

View File

@@ -0,0 +1,46 @@
package com.zyplayer.doc.manage.framework.interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 记录当前请求信息
*/
@Component
public class RequestInfoInterceptor implements HandlerInterceptor {
private static final Logger logger = LoggerFactory.getLogger(RequestInfoInterceptor.class);
private ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>();
/**
* 把当前请求记录到下来
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
long startTime = startTimeThreadLocal.get();
long totalTime = System.currentTimeMillis() - startTime;// 结束时间
logger.error("总耗时:{}ms URI{}", totalTime, request.getRequestURI());
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object haddler,
ModelAndView modelAndView) throws Exception {
}
/**
* 记录请求信息
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
startTimeThreadLocal.set(System.currentTimeMillis());
return true;
}
}

View File

@@ -1,11 +1,19 @@
package com.zyplayer.doc.manage.web.manage;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.zyplayer.doc.manage.repository.manage.entity.AuthInfo;
import com.zyplayer.doc.manage.repository.manage.entity.UserAuth;
import com.zyplayer.doc.manage.service.manage.AuthInfoService;
import com.zyplayer.doc.manage.service.manage.UserAuthService;
import com.zyplayer.doc.manage.web.manage.vo.AuthInfoVo;
import org.apache.commons.lang.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -25,6 +33,12 @@ public class UserInfoController {
@Autowired
UserInfoService userInfoService;
@Autowired
AuthInfoService authInfoService;
@Autowired
UserAuthService userAuthService;
@Autowired
Mapper mapper;
@PostMapping("/list")
public ResponseJson<Object> list(String userName) {
@@ -32,10 +46,62 @@ public class UserInfoController {
if (StringUtils.isNotBlank(userName)) {
queryWrapper.like("user_name", userName);
}
queryWrapper.eq("del_flag", 0);
List<UserInfo> userInfoList = userInfoService.list(queryWrapper);
if (userInfoList != null && userInfoList.size() > 0) {
userInfoList.forEach(val -> val.setPassword(null));
}
return DocResponseJson.ok(userInfoList);
}
@PostMapping("/auth/list")
public ResponseJson<Object> authList(String userIds) {
List<AuthInfo> authList = authInfoService.list();
QueryWrapper<UserAuth> queryWrapper = new QueryWrapper<>();
queryWrapper.in("user_id", userIds.split(","));
queryWrapper.eq("del_flag", 0);
List<UserAuth> userAuths = userAuthService.list(queryWrapper);
Map<Long, UserAuth> userAuthMap = userAuths.stream().collect(Collectors.toMap(UserAuth::getAuthId, Function.identity(), (val1, val2) -> val1));
List<AuthInfoVo> authInfoVoList = new LinkedList<>();
authList.forEach(val -> {
UserAuth userAuth = userAuthMap.get(val.getId());
AuthInfoVo infoVo = mapper.map(val, AuthInfoVo.class);
infoVo.setChecked((userAuth == null) ? 0 : 1);
authInfoVoList.add(infoVo);
});
return DocResponseJson.ok(authInfoVoList);
}
@PostMapping("/auth/update")
public ResponseJson<Object> updateAuth(String userIds, String authIds) {
List<Long> userIdsList = Arrays.asList(userIds.split(",")).stream().collect(Collectors.mapping(val -> Long.valueOf(val), Collectors.toList()));
List<Long> authIdsList = Arrays.asList(authIds.split(",")).stream().collect(Collectors.mapping(val -> Long.valueOf(val), Collectors.toList()));
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
UserAuth userAuthUp = new UserAuth();
userAuthUp.setDelFlag(1);
userAuthUp.setUpdateTime(new Date());
userAuthUp.setUpdateUid(currentUser.getUserId());
QueryWrapper<UserAuth> queryWrapper = new QueryWrapper<>();
queryWrapper.in("user_id", userIdsList);
userAuthService.update(userAuthUp, queryWrapper);
List<UserAuth> createList = new LinkedList<>();
for (int i = 0; i < userIdsList.size(); i++) {
for (int j = 0; j < authIdsList.size(); j++) {
UserAuth userAuth = new UserAuth();
userAuth.setUserId(userIdsList.get(i));
userAuth.setAuthId(authIdsList.get(j));
userAuth.setCreateUid(currentUser.getUserId());
userAuth.setCreationTime(new Date());
userAuth.setDelFlag(0);
createList.add(userAuth);
}
}
userAuthService.saveBatch(createList);
return DocResponseJson.ok();
}
@PostMapping("/delete")
public ResponseJson<Object> delete(Long id) {
UserInfo userInfo = new UserInfo();
@@ -48,6 +114,11 @@ public class UserInfoController {
@PostMapping("/update")
public ResponseJson<Object> update(UserInfo userInfo) {
String password = userInfo.getPassword();
if (StringUtils.isNotBlank(password)) {
password = DigestUtils.md5DigestAsHex(password.getBytes());
userInfo.setPassword(password);
}
if (userInfo.getId() != null && userInfo.getId() > 0) {
userInfo.setUpdateTime(new Date());
userInfoService.updateById(userInfo);

View File

@@ -0,0 +1,118 @@
package com.zyplayer.doc.manage.web.manage.vo;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author 暮光:城中城
* @since 2018-12-05
*/
public class AuthInfoVo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键自增ID
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 是否选中
*/
private Integer checked;
/**
* 权限名
*/
private String authName;
/**
* 权限说明
*/
private String authDesc;
/**
* 是否可编辑 0=否 1=是
*/
private Integer canEdit;
/**
* 创建人
*/
private Long createUid;
/**
* 创建时间
*/
private Date creationTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAuthName() {
return authName;
}
public void setAuthName(String authName) {
this.authName = authName;
}
public String getAuthDesc() {
return authDesc;
}
public void setAuthDesc(String authDesc) {
this.authDesc = authDesc;
}
public Integer getCanEdit() {
return canEdit;
}
public void setCanEdit(Integer canEdit) {
this.canEdit = canEdit;
}
public Long getCreateUid() {
return createUid;
}
public void setCreateUid(Long createUid) {
this.createUid = createUid;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
@Override
public String toString() {
return "AuthInfo{" +
"id=" + id +
", authName=" + authName +
", authDesc=" + authDesc +
", canEdit=" + canEdit +
", createUid=" + createUid +
", creationTime=" + creationTime +
"}";
}
public Integer getChecked() {
return checked;
}
public void setChecked(Integer checked) {
this.checked = checked;
}
}