Commit 99ca6766 authored by 赵啸非's avatar 赵啸非

重构部分功能模块

parent ed0570c4
Pipeline #2313 canceled with stages
package com.mortals.xhx.feign.user;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.user.UserPdu;
import com.mortals.xhx.feign.IFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户信息业务 Feign接口
* @author zxfei
* @date 2022-07-06
*/
@FeignClient(name = "portal-manager", path = "/zwfw", fallbackFactory = UserFeignFallbackFactory.class)
public interface IUserFeign extends IFeign {
/**
* 查看用户信息业务列表
*
* @param userPdu
* @return
*/
@PostMapping(value = "/user/listPage")
Rest<RespData<List<UserPdu>>> list(@RequestBody UserPdu userPdu);
/**
* 查看用户信息业务
*
* @param id
* @return
*/
@GetMapping(value = "/user/info")
Rest<UserPdu> info(@RequestParam(value = "id") Long id);
/**
* 删除用户信息业务
*
* @param ids
* @return
*/
@GetMapping(value = "/user/delete")
Rest<Void> delete(Long[] ids,@RequestHeader("Authorization") String authorization);
/**
* 用户信息业务保存更新
*
* @param userPdu
* @return
*/
@PostMapping(value = "/user/save")
Rest<RespData<UserPdu>> save(@RequestBody UserPdu userPdu,@RequestHeader("Authorization") String authorization);
@PostMapping(value = "/login/login")
String portalLogin(@RequestBody UserPdu userPdu);
/**
* 查询用户列表
*
* @param userPdu
* @return
*/
@PostMapping("/api/user/findAllUser")
String findAllUser(@RequestBody UserPdu userPdu);
/**
* 获取token
*
* @param
* @return
*/
@PostMapping(value = "/user/token")
Rest<String> getToken(@RequestParam(value = "userKey") String userKey);
}
@Slf4j
@Component
class UserFeignFallbackFactory implements FallbackFactory<IUserFeign> {
@Override
public IUserFeign create(Throwable t) {
return new IUserFeign() {
@Override
public Rest<RespData<List<UserPdu>>> list(UserPdu userPdu) {
return Rest.fail("暂时无法获取用户信息业务列表,请稍后再试!");
}
@Override
public Rest<UserPdu> info(Long id) {
return Rest.fail("暂时无法获取用户信息业务详细,请稍后再试!");
}
@Override
public Rest<Void> delete(Long[] ids, String authorization) {
return Rest.fail("暂时无法删除用户信息业务,请稍后再试!");
}
@Override
public Rest<RespData<UserPdu>> save(UserPdu userPdu, String authorization) {
return Rest.fail("暂时无法保存用户信息业务,请稍后再试!");
}
@Override
public String portalLogin(UserPdu userPdu) {
return JSON.toJSONString(Rest.fail("登录失败!"));
}
@Override
public String findAllUser(UserPdu userPdu) {
return null;
}
@Override
public Rest<String> getToken(String userKey) {
return Rest.fail("token获取失败");
}
};
}
}
...@@ -2,12 +2,10 @@ package com.mortals.xhx.base.login.web; ...@@ -2,12 +2,10 @@ package com.mortals.xhx.base.login.web;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.CookieService;
import com.mortals.framework.ap.GlobalSysInfo; import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.ap.SysConstains; import com.mortals.framework.ap.SysConstains;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.ICacheService; import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.ITokenService;
import com.mortals.framework.service.IUser; import com.mortals.framework.service.IUser;
import com.mortals.framework.util.AESUtil; import com.mortals.framework.util.AESUtil;
import com.mortals.framework.util.DateUtils; import com.mortals.framework.util.DateUtils;
...@@ -21,11 +19,11 @@ import com.mortals.xhx.base.system.user.model.UserEntity; ...@@ -21,11 +19,11 @@ import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.service.UserService; import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.base.system.valid.service.ValidCodeService; import com.mortals.xhx.base.system.valid.service.ValidCodeService;
import com.mortals.xhx.common.key.RedisKey; import com.mortals.xhx.common.key.RedisKey;
import lombok.extern.slf4j.Slf4j; import com.mortals.xhx.common.pdu.user.UserPdu;
import org.springframework.beans.factory.InitializingBean; import com.mortals.xhx.feign.user.IUserFeign;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -35,13 +33,9 @@ import javax.servlet.http.HttpServletResponse; ...@@ -35,13 +33,9 @@ import javax.servlet.http.HttpServletResponse;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED_CONTENT;
@RestController @RestController
@Slf4j
@RequestMapping("login") @RequestMapping("login")
public class LoginController extends BaseCRUDJsonBodyMappingController<UserService, UserEntity, Long> implements InitializingBean { public class LoginController extends BaseCRUDJsonBodyMappingController<UserService, UserEntity, Long> {
@Autowired @Autowired
private UserService userService; private UserService userService;
...@@ -52,15 +46,34 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -52,15 +46,34 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
@Autowired @Autowired
private MenuService menuService; private MenuService menuService;
@Autowired @Autowired
private ITokenService tokenService;
@Autowired
private ICacheService cacheService; private ICacheService cacheService;
@Autowired @Autowired
private IAuthTokenService authTokenService; private IUserFeign userFeign;
private static Log logger = LogFactory.getLog(LoginController.class);
@RequestMapping("login") @RequestMapping("login")
public String login(@RequestBody LoginForm loginForm) throws Exception { public String login( @RequestBody LoginForm loginForm) throws Exception {
JSONObject ret = new JSONObject();
String loginName = loginForm.getLoginName(); String loginName = loginForm.getLoginName();
String password = loginForm.getPassword(); String password = loginForm.getPassword();
UserPdu userPdu = new UserPdu();
userPdu.setLoginName(loginName);
userPdu.setPassword(password);
String resp = userFeign.portalLogin(userPdu);
return resp;
/*JSONObject ret = new JSONObject();
String loginName = loginForm.getLoginName();
String password = loginForm.getPassword();
//String securityCode = loginForm.getSecurityCode();
String ip = super.getRequestIP(request); String ip = super.getRequestIP(request);
if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) { if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE); ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
...@@ -70,8 +83,10 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -70,8 +83,10 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
UserEntity userEntity = null; UserEntity userEntity = null;
try { try {
loginForm.validate(); loginForm.validate();
userEntity = userService.doLogin(loginName, password, ip); userEntity = userService.doLogin(loginName, password, ip);
userEntity.setLastLoginAddress(ip); userEntity.setLastLoginAddress(ip);
//saveCurrUser(request, response, userEntity);
recordSysLog(request, userEntity, "用户登录系统成功!"); recordSysLog(request, userEntity, "用户登录系统成功!");
// 返回拥有的菜单数据 // 返回拥有的菜单数据
Set<String> urls = resourceService.findUrlSetByUserId(userEntity.getId()); Set<String> urls = resourceService.findUrlSetByUserId(userEntity.getId());
...@@ -80,26 +95,23 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -80,26 +95,23 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
if (currUserName == null || currUserName.trim().length() == 0) { if (currUserName == null || currUserName.trim().length() == 0) {
currUserName = "管理员"; currUserName = "管理员";
} }
userEntity.setLoginTime(System.currentTimeMillis());
userEntity.setToken(IdUtil.fastSimpleUUID());
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
String token = authTokenService.createToken(userEntity);
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("token", token);
data.put("currUserName", currUserName); data.put("currUserName", currUserName);
data.put("barList", outlookBarList); data.put("barList", outlookBarList);
data.put("id", userEntity.getId()); data.put("id", userEntity.getId());
data.put("userType", userEntity.getUserType()); data.put("userType", userEntity.getUserType());
userEntity.setLoginTime(System.currentTimeMillis());
userEntity.setToken(IdUtil.fastSimpleUUID());
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
userEntity.setMenuUrl(generateMenuUrlCode(urls));
String token = authTokenService.createToken(userEntity);
data.put("token", token);
//设置token 和过期时间
//data.put("expiresTime", DateUtils.addCurrDate(7).getTime());
generateMenuUrlCode(urls);
//this.generateBlackCookie(request, response, loginName, urls); //this.generateBlackCookie(request, response, loginName, urls);
ret.put(KEY_RESULT_DATA, data); ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS); ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "用户登录系统成功!"); ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
ret.put("resources", urls); ret.put("resources", urls);
return ret.toJSONString(); return ret.toJSONString();
} catch (Exception e) { } catch (Exception e) {
log.error("login error ", e); log.error("login error ", e);
...@@ -110,22 +122,21 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -110,22 +122,21 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE); ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, super.convertException(e)); ret.put(KEY_RESULT_MSG, super.convertException(e));
return ret.toJSONString(); return ret.toJSONString();
} }*/
} }
@RequestMapping("logout") @RequestMapping("logout")
public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception { public void logout() throws Exception {
recordSysLog(request, "退出登录"); recordSysLog(request, "退出登录");
super.removeCurrUser(request); super.removeCurrUser(request);
this.deleteBlackCookie(request, response);
} }
@RequestMapping("index") @RequestMapping("index")
public String index() throws Exception { public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONObject ret = new JSONObject(); JSONObject ret = new JSONObject();
IUser user = this.getCurUser(); IUser user = this.getCurUser();
if (user == null) { if (user == null) {
return JSONObject.toJSONString(Rest.fail(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT)); return "";
} }
Set<String> urls = resourceService.findUrlSetByUserId(user.getId()); Set<String> urls = resourceService.findUrlSetByUserId(user.getId());
List<MenuEntity> outlookBarList = menuService.findTreeMenu(user, urls); List<MenuEntity> outlookBarList = menuService.findTreeMenu(user, urls);
...@@ -134,9 +145,6 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -134,9 +145,6 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
currUserName = "管理员"; currUserName = "管理员";
} }
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
String token = authTokenService.getToken(request);
data.put("token", token);
data.put("currUserName", currUserName); data.put("currUserName", currUserName);
data.put("barList", outlookBarList); data.put("barList", outlookBarList);
data.put("id", user.getId()); data.put("id", user.getId());
...@@ -172,26 +180,6 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -172,26 +180,6 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
} }
} }
private String generateMenuUrlCode(Set<String> urls) {
try {
String securityKey = GlobalSysInfo.getPropertyValue(SysConstains.PROP_COOKIE_SECURITY_KEY);
StringBuilder sb = new StringBuilder();
if (urls != null && urls.size() > 0) {
for (String url : urls) {
int index = url.hashCode() & (Integer.MAX_VALUE - 1);
sb.append(index).append(",");
}
}
String menuUrl = sb.toString();
return AESUtil.encrypt(menuUrl, securityKey);
} catch (Throwable e) {
log.error("编码异常", e);
return null;
}
}
@RequestMapping("validcode") @RequestMapping("validcode")
public String validCode(HttpServletRequest request, HttpServletResponse response, LoginForm loginForm) { public String validCode(HttpServletRequest request, HttpServletResponse response, LoginForm loginForm) {
JSONObject ret = new JSONObject(); JSONObject ret = new JSONObject();
...@@ -235,26 +223,4 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi ...@@ -235,26 +223,4 @@ public class LoginController extends BaseCRUDJsonBodyMappingController<UserServi
return ret.toJSONString(); return ret.toJSONString();
} }
protected void deleteBlackCookie(HttpServletRequest request, HttpServletResponse response) {
try {
CookieService.deleteCookieForAuth(request, response);
} catch (Throwable e) {
}
}
@RequestMapping("parseToken")
public IUser parseToken() throws Exception {
IUser userEntity = authTokenService.getLoginUser(request);
if (!ObjectUtils.isEmpty(userEntity)) {
authTokenService.verifyToken(userEntity);
return userEntity;
}
return null;
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("初始化加载单机版登录。。。");
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment