♻️ Refactoring code. #1054 【重构】 登录流程简化,减少DB 访问

This commit is contained in:
冷冷 committed 2020-04-13 21:20:47 +08:00
1 parent 8a2315374a
commit 37356e2107
6 files changed
+48 -34

No files matched your search

@@ -117,7 +117,9 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
// 构造security用户
return new PigxUser(user.getUserId(), user.getDeptId(), user.getTenantId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
true, true, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
return new PigxUser(user.getUserId(), user.getDeptId(), user.getPhone(), user.getAvatar(), user.getTenantId()
, user.getUsername(), SecurityConstants.BCRYPT + user.getPassword()
, enabled, true, true
, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
}
}
@@ -152,22 +152,36 @@ public interface SecurityConstants {
/**
* 用户ID字段
*/
String DETAILS_USER_ID = "user_id";
String DETAILS_USER_ID = "id";
/**
* 用户名字段
* 用户名
*/
String DETAILS_USERNAME = "username";
/**
* 用户基本信息
*/
String DETAILS_USER = "user_info";
/**
* 用户名phone
*/
String DETAILS_PHONE = "phone";
/**
* 头像
*/
String DETAILS_AVATAR = "avatar";
/**
* 用户部门字段
*/
String DETAILS_DEPT_ID = "dept_id";
String DETAILS_DEPT_ID = "deptId";
/**
* 租户ID 字段
*/
String DETAILS_TENANT_ID = "tenant_id";
String DETAILS_TENANT_ID = "tenantId";
/**
* 协议字段
@@ -51,10 +51,7 @@ public class PigxTokenEnhancer implements TokenEnhancer {
final Map<String, Object> additionalInfo = new HashMap<>(8);
PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
additionalInfo.put(SecurityConstants.DETAILS_USER_ID, pigxUser.getId());
additionalInfo.put(SecurityConstants.DETAILS_USERNAME, pigxUser.getUsername());
additionalInfo.put(SecurityConstants.DETAILS_DEPT_ID, pigxUser.getDeptId());
additionalInfo.put(SecurityConstants.DETAILS_TENANT_ID, pigxUser.getTenantId());
additionalInfo.put(SecurityConstants.DETAILS_USER, pigxUser);
additionalInfo.put(SecurityConstants.DETAILS_LICENSE, SecurityConstants.PIGX_LICENSE);
additionalInfo.put(SecurityConstants.ACTIVE, Boolean.TRUE);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
@@ -69,19 +69,22 @@ public class PigxUserAuthenticationConverter implements UserAuthenticationConver
/**
* Inverse of {@link #convertUserAuthentication(Authentication)}. Extracts an Authentication from a map.
*
* @param map a map of user information
* @param responseMap a map of user information
* @return an Authentication representing the user or null if there is none
*/
@Override
public Authentication extractAuthentication(Map<String, ?> map) {
if (map.containsKey(USERNAME)) {
public Authentication extractAuthentication(Map<String, ?> responseMap) {
if (responseMap.containsKey(USERNAME)) {
Collection<? extends GrantedAuthority> authorities = getAuthorities(responseMap);
Map<String, ?> map = MapUtil.get(responseMap, SecurityConstants.DETAILS_USER, Map.class);
validateTenantId(map);
Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
String username = MapUtil.getStr(map, USERNAME);
String username = MapUtil.getStr(map, SecurityConstants.DETAILS_USERNAME);
Integer id = MapUtil.getInt(map, SecurityConstants.DETAILS_USER_ID);
Integer deptId = MapUtil.getInt(map, SecurityConstants.DETAILS_DEPT_ID);
Integer tenantId = MapUtil.getInt(map, SecurityConstants.DETAILS_TENANT_ID);
PigxUser user = new PigxUser(id, deptId, tenantId, username, N_A, true
String phone = MapUtil.getStr(map, SecurityConstants.DETAILS_PHONE);
String avatar = MapUtil.getStr(map, SecurityConstants.DETAILS_AVATAR);
PigxUser user = new PigxUser(id, deptId, phone, avatar, tenantId, username, N_A, true
, true, true, true, authorities);
return new UsernamePasswordAuthenticationToken(user, N_A, authorities);
}
@@ -41,12 +41,25 @@ public class PigxUser extends User {
@Getter
private Integer deptId;
/**
* 手机号
*/
@Getter
private String phone;
/**
* 头像
*/
@Getter
private String avatar;
/**
* 租户ID
*/
@Getter
private Integer tenantId;
/**
* Construct the <code>User</code> with the details required by
* {@link DaoAuthenticationProvider}.
@@ -68,10 +81,12 @@ public class PigxUser extends User {
* @throws IllegalArgumentException if a <code>null</code> value was passed either as
* a parameter or as an element in the <code>GrantedAuthority</code> collection
*/
public PigxUser(Integer id, Integer deptId, Integer tenantId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
public PigxUser(Integer id, Integer deptId, String phone, String avatar, Integer tenantId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.id = id;
this.deptId = deptId;
this.phone = phone;
this.avatar = avatar;
this.tenantId = tenantId;
}
}
@@ -28,7 +28,6 @@ import com.pig4cloud.pigx.admin.service.SysUserService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@@ -49,22 +48,6 @@ import javax.validation.Valid;
public class SysUserController {
private final SysUserService userService;
/**
* 获取当前用户全部信息
*
* @return 用户信息
*/
@GetMapping(value = {"/info"})
public R info() {
String username = SecurityUtils.getUser().getUsername();
SysUser user = userService.getOne(Wrappers.<SysUser>query()
.lambda().eq(SysUser::getUsername, username));
if (user == null) {
return R.failed(null, "获取当前用户信息失败");
}
return R.ok(userService.findUserInfo(user));
}
/**
* 获取指定用户全部信息
*