Skip to content

Commit fffa864

Browse files
committed
refactor/修改登录逻辑
1 parent 09d08e4 commit fffa864

File tree

20 files changed

+650
-725
lines changed

20 files changed

+650
-725
lines changed

pmhub-api/pmhub-api-system/src/main/java/com/laigeoffer/pmhub/api/system/UserFeignService.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.laigeoffer.pmhub.api.system.factory.UserFeginFallbackFactory;
44
import com.laigeoffer.pmhub.base.core.constant.SecurityConstants;
55
import com.laigeoffer.pmhub.base.core.constant.ServiceNameConstants;
6-
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
6+
import com.laigeoffer.pmhub.base.core.core.domain.R;
7+
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysUser;
8+
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
79
import org.springframework.cloud.openfeign.FeignClient;
8-
import org.springframework.web.bind.annotation.GetMapping;
9-
import org.springframework.web.bind.annotation.PathVariable;
10-
import org.springframework.web.bind.annotation.RequestHeader;
10+
import org.springframework.web.bind.annotation.*;
1111

1212
/**
1313
* @author canghe
@@ -17,15 +17,20 @@
1717
@FeignClient(contextId = "userFeignService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = UserFeginFallbackFactory.class)
1818
public interface UserFeignService {
1919

20-
/**
21-
* 根据用户编号获取详细信息
22-
*/
23-
@GetMapping(value = { "/user/{userId}"})
24-
AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
2520

2621
/**
2722
* 根据用户名获取当前用户信息
2823
*/
2924
@GetMapping("/user/info/{username}")
30-
AjaxResult getInfoByUsername(@PathVariable("username") String username);
25+
R<LoginUser> info(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
26+
27+
/**
28+
* 注册用户信息
29+
*
30+
* @param sysUser 用户信息
31+
* @param source 请求来源
32+
* @return 结果
33+
*/
34+
@PostMapping("/user/register")
35+
R<Boolean> registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
3136
}

pmhub-api/pmhub-api-system/src/main/java/com/laigeoffer/pmhub/api/system/factory/UserFeginFallbackFactory.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.laigeoffer.pmhub.api.system.factory;
22

33
import com.laigeoffer.pmhub.api.system.UserFeignService;
4-
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
4+
import com.laigeoffer.pmhub.base.core.core.domain.R;
5+
import com.laigeoffer.pmhub.base.core.core.domain.entity.SysUser;
6+
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
57
import org.slf4j.Logger;
68
import org.slf4j.LoggerFactory;
79
import org.springframework.cloud.openfeign.FallbackFactory;
@@ -23,14 +25,15 @@ public UserFeignService create(Throwable throwable)
2325
log.error("用户服务调用失败:{}", throwable.getMessage());
2426
return new UserFeignService()
2527
{
28+
2629
@Override
27-
public AjaxResult getInfo(Long userId, String source) {
28-
return AjaxResult.error("获取用户失败:" + throwable.getMessage());
30+
public R<LoginUser> info(String username, String source) {
31+
return R.fail("获取用户失败:" + throwable.getMessage());
2932
}
3033

3134
@Override
32-
public AjaxResult getInfoByUsername(String username) {
33-
return AjaxResult.error("获取用户失败:" + throwable.getMessage());
35+
public R<Boolean> registerUserInfo(SysUser sysUser, String source) {
36+
return R.fail("注册用户失败:" + throwable.getMessage());
3437
}
3538

3639
};
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,83 @@
1-
//package com.laigeoffer.pmhub.auth.controller;
2-
//
3-
//import com.laigeoffer.pmhub.auth.service.SysLoginService;
4-
//import com.laigeoffer.pmhub.base.core.constant.Constants;
5-
//import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
6-
//import com.laigeoffer.pmhub.base.core.core.domain.model.LoginBody;
7-
//import org.springframework.beans.factory.annotation.Autowired;
8-
//import org.springframework.web.bind.annotation.PostMapping;
9-
//import org.springframework.web.bind.annotation.RequestBody;
10-
//import org.springframework.web.bind.annotation.RestController;
11-
//
12-
//import static com.laigeoffer.pmhub.base.core.core.domain.AjaxResult.success;
13-
//
14-
///**
15-
// * 登录验证
16-
// *
17-
// * @author canghe
18-
// */
19-
//@RestController
20-
//public class LoginController {
21-
//
22-
//
23-
// @Autowired
24-
// private SysLoginService loginService;
25-
//
26-
//// @Autowired
27-
//// private ISysMenuService menuService;
28-
////
29-
////
30-
//// @Autowired
31-
//// private SysPermissionService permissionService;
32-
//
33-
//
34-
//
35-
//
36-
//
37-
//
38-
// /**
39-
// * 登录方法
40-
// *
41-
// * @param loginBody 登录信息
42-
// * @return 结果
43-
// */
44-
// @PostMapping("/login")
45-
// public AjaxResult login(@RequestBody LoginBody loginBody) {
46-
// AjaxResult ajax = success();
47-
// // 生成令牌
48-
// String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
49-
// loginBody.getUuid());
50-
// ajax.put(Constants.TOKEN, token);
51-
// return ajax;
52-
// }
53-
//
54-
//// /**
55-
//// * 获取用户信息
56-
//// *
57-
//// * @return 用户信息
58-
//// */
59-
//// @GetMapping("getInfo")
60-
//// public AjaxResult getInfo() {
61-
//// SysUser user = SecurityUtils.getLoginUser().getUser();
62-
//// // 角色集合
63-
//// Set<String> roles = permissionService.getRolePermission(user);
64-
//// // 权限集合
65-
//// Set<String> permissions = permissionService.getMenuPermission(user);
66-
//// AjaxResult ajax = success();
67-
//// ajax.put("user", user);
68-
//// ajax.put("roles", roles);
69-
//// ajax.put("permissions", permissions);
70-
//// return ajax;
71-
//// }
72-
////
73-
//// /**
74-
//// * 获取路由信息
75-
//// *
76-
//// * @return 路由信息
77-
//// */
78-
//// @GetMapping("getRouters")
79-
//// public AjaxResult getRouters() {
80-
//// Long userId = SecurityUtils.getUserId();
81-
//// List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
82-
//// return success(menuService.buildMenus(menus));
83-
//// }
84-
//
85-
//
86-
//}
1+
package com.laigeoffer.pmhub.auth.controller;
2+
3+
import com.laigeoffer.pmhub.auth.service.SysLoginService;
4+
import com.laigeoffer.pmhub.base.core.constant.Constants;
5+
import com.laigeoffer.pmhub.base.core.core.domain.AjaxResult;
6+
import com.laigeoffer.pmhub.base.core.core.domain.R;
7+
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginBody;
8+
import com.laigeoffer.pmhub.base.core.core.domain.model.LoginUser;
9+
import com.laigeoffer.pmhub.base.core.core.domain.model.RegisterBody;
10+
import com.laigeoffer.pmhub.base.core.utils.JwtUtils;
11+
import com.laigeoffer.pmhub.base.core.utils.StringUtils;
12+
import com.laigeoffer.pmhub.base.security.auth.AuthUtil;
13+
import com.laigeoffer.pmhub.base.security.service.TokenService;
14+
import com.laigeoffer.pmhub.base.security.utils.SecurityUtils;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.web.bind.annotation.DeleteMapping;
17+
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.RequestBody;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
import static com.laigeoffer.pmhub.base.core.core.domain.AjaxResult.success;
24+
25+
/**
26+
* 登录验证
27+
*
28+
* @author canghe
29+
*/
30+
@RestController
31+
public class LoginController {
32+
33+
34+
@Autowired
35+
private TokenService tokenService;
36+
37+
@Autowired
38+
private SysLoginService sysLoginService;
39+
40+
@PostMapping("login")
41+
public AjaxResult login(@RequestBody LoginBody form) {
42+
AjaxResult ajax = success();
43+
// 用户登录
44+
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
45+
String token = tokenService.createToken(userInfo);
46+
ajax.put(Constants.TOKEN, token);
47+
// 获取登录token
48+
return ajax;
49+
}
50+
51+
@DeleteMapping("logout")
52+
public R<?> logout(HttpServletRequest request) {
53+
String token = SecurityUtils.getToken(request);
54+
if (StringUtils.isNotEmpty(token)) {
55+
String username = JwtUtils.getUserName(token);
56+
// 删除用户缓存记录
57+
AuthUtil.logoutByToken(token);
58+
// 记录用户退出日志
59+
sysLoginService.logout(username);
60+
}
61+
return R.ok();
62+
}
63+
64+
@PostMapping("refresh")
65+
public R<?> refresh(HttpServletRequest request) {
66+
LoginUser loginUser = tokenService.getLoginUser(request);
67+
if (StringUtils.isNotNull(loginUser)) {
68+
// 刷新令牌有效期
69+
tokenService.refreshToken(loginUser);
70+
return R.ok();
71+
}
72+
return R.ok();
73+
}
74+
75+
@PostMapping("register")
76+
public R<?> register(@RequestBody RegisterBody registerBody) {
77+
// 用户注册
78+
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
79+
return R.ok();
80+
}
81+
82+
83+
}

pmhub-auth/src/main/java/com/laigeoffer/pmhub/auth/controller/SysRegisterController.java

-34
This file was deleted.

pmhub-auth/src/main/java/com/laigeoffer/pmhub/auth/handle/AuthenticationEntryPointImpl.java

-33
This file was deleted.

0 commit comments

Comments
 (0)