refactor: 认证方式调整为认证身份标识重命名

This commit is contained in:
郝先瑞 2022-02-21 23:20:43 +08:00
parent 1ba35954a8
commit 774a7fa799
7 changed files with 40 additions and 27 deletions

View File

@ -1,6 +1,7 @@
package com.youlai.admin.service.impl; package com.youlai.admin.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -98,7 +99,7 @@ public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, S
.filter(item -> StrUtil.isNotBlank(item.getBtnPerm())) .filter(item -> StrUtil.isNotBlank(item.getBtnPerm()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(btnPermList)) { if (CollectionUtil.isNotEmpty(btnPermList)) {
Map<String, List<String>> btnPermRoles = CollectionUtil.newHashMap(); Map<String, List<String>> btnPermRoles = MapUtil.newHashMap();
btnPermList.stream().forEach(item -> { btnPermList.stream().forEach(item -> {
String perm = item.getBtnPerm(); String perm = item.getBtnPerm();
List<String> roles = item.getRoles(); List<String> roles = item.getRoles();

View File

@ -24,10 +24,11 @@ public class MemberUserDetails implements UserDetails {
private Boolean enabled; private Boolean enabled;
/** /**
* 认证方式 * 扩展字段认证身份标识枚举值如下
*
* @see com.youlai.common.enums.AuthenticationIdentityEnum
*/ */
private String authenticationMethod; private String authenticationIdentity;
/** /**
* 小程序会员用户体系 * 小程序会员用户体系

View File

@ -1,6 +1,6 @@
package com.youlai.auth.security.core.userdetails.member; package com.youlai.auth.security.core.userdetails.member;
import com.youlai.common.enums.AuthenticationMethodEnum; import com.youlai.common.enums.AuthenticationIdentityEnum;
import com.youlai.common.result.Result; import com.youlai.common.result.Result;
import com.youlai.common.result.ResultCode; import com.youlai.common.result.ResultCode;
import com.youlai.mall.ums.api.MemberFeignClient; import com.youlai.mall.ums.api.MemberFeignClient;
@ -44,7 +44,7 @@ public class MemberUserDetailsServiceImpl implements UserDetailsService {
MemberAuthInfoDTO member = result.getData(); MemberAuthInfoDTO member = result.getData();
if (null != member) { if (null != member) {
userDetails = new MemberUserDetails(member); userDetails = new MemberUserDetails(member);
userDetails.setAuthenticationMethod(AuthenticationMethodEnum.MOBILE.getValue()); // 认证方式OpenId userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.MOBILE.getValue()); // 认证身份标识:mobile
} }
} }
if (userDetails == null) { if (userDetails == null) {
@ -68,11 +68,12 @@ public class MemberUserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByOpenId(String openId) { public UserDetails loadUserByOpenId(String openId) {
MemberUserDetails userDetails = null; MemberUserDetails userDetails = null;
Result<MemberAuthInfoDTO> result = memberFeignClient.loadUserByOpenId(openId); Result<MemberAuthInfoDTO> result = memberFeignClient.loadUserByOpenId(openId);
if (Result.isSuccess(result)) { if (Result.isSuccess(result)) {
MemberAuthInfoDTO member = result.getData(); MemberAuthInfoDTO member = result.getData();
if (null != member) { if (null != member) {
userDetails = new MemberUserDetails(member); userDetails = new MemberUserDetails(member);
userDetails.setAuthenticationMethod(AuthenticationMethodEnum.OPENID.getValue()); // 认证方式OpenId userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.OPENID.getValue()); // 认证方式:openId
} }
} }
if (userDetails == null) { if (userDetails == null) {

View File

@ -23,10 +23,20 @@ import java.util.Collection;
public class SysUserDetails implements UserDetails { public class SysUserDetails implements UserDetails {
/** /**
* 扩展字段 * 扩展字段用户ID
*/ */
private Long userId; private Long userId;
private String authenticationMethod;
/**
* 扩展字段认证身份标识枚举值如下
*
* @see com.youlai.common.enums.AuthenticationIdentityEnum
*/
private String authenticationIdentity;
/**
* 扩展字段部门ID
*/
private Long deptId; private Long deptId;
/** /**

View File

@ -2,7 +2,7 @@ package com.youlai.auth.security.extension.refresh;
import com.youlai.auth.security.core.userdetails.member.MemberUserDetailsServiceImpl; import com.youlai.auth.security.core.userdetails.member.MemberUserDetailsServiceImpl;
import com.youlai.common.constant.SecurityConstants; import com.youlai.common.constant.SecurityConstants;
import com.youlai.common.enums.AuthenticationMethodEnum; import com.youlai.common.enums.AuthenticationIdentityEnum;
import com.youlai.common.web.util.RequestUtils; import com.youlai.common.web.util.RequestUtils;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -52,13 +52,13 @@ public class PreAuthenticatedUserDetailsService<T extends Authentication> implem
@Override @Override
public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException { public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException {
String clientId = RequestUtils.getOAuth2ClientId(); String clientId = RequestUtils.getOAuth2ClientId();
// 获取认证方式默认是用户名 username // 获取认证身份标识默认是用户名:username
AuthenticationMethodEnum authenticationMethodEnum = AuthenticationMethodEnum.getByValue(RequestUtils.getAuthenticationMethod()); AuthenticationIdentityEnum authenticationIdentityEnum = AuthenticationIdentityEnum.getByValue(RequestUtils.getAuthenticationIdentity());
UserDetailsService userDetailsService = userDetailsServiceMap.get(clientId); UserDetailsService userDetailsService = userDetailsServiceMap.get(clientId);
if (clientId.equals(SecurityConstants.APP_CLIENT_ID)) { if (clientId.equals(SecurityConstants.APP_CLIENT_ID)) {
// 移动端的用户体系是会员认证方式是通过手机号 mobile 认证 // 移动端的用户体系是会员认证方式是通过手机号 mobile 认证
MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService; MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService;
switch (authenticationMethodEnum) { switch (authenticationIdentityEnum) {
case MOBILE: case MOBILE:
return memberUserDetailsService.loadUserByMobile(authentication.getName()); return memberUserDetailsService.loadUserByMobile(authentication.getName());
default: default:
@ -67,7 +67,7 @@ public class PreAuthenticatedUserDetailsService<T extends Authentication> implem
} else if (clientId.equals(SecurityConstants.WEAPP_CLIENT_ID)) { } else if (clientId.equals(SecurityConstants.WEAPP_CLIENT_ID)) {
// 小程序的用户体系是会员认证方式是通过微信三方标识 openid 认证 // 小程序的用户体系是会员认证方式是通过微信三方标识 openid 认证
MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService; MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService;
switch (authenticationMethodEnum) { switch (authenticationIdentityEnum) {
case OPENID: case OPENID:
return memberUserDetailsService.loadUserByOpenId(authentication.getName()); return memberUserDetailsService.loadUserByOpenId(authentication.getName());
default: default:
@ -75,7 +75,7 @@ public class PreAuthenticatedUserDetailsService<T extends Authentication> implem
} }
} else if (clientId.equals(SecurityConstants.ADMIN_CLIENT_ID)) { } else if (clientId.equals(SecurityConstants.ADMIN_CLIENT_ID)) {
// 管理系统的用户体系是系统用户认证方式通过用户名 username 认证 // 管理系统的用户体系是系统用户认证方式通过用户名 username 认证
switch (authenticationMethodEnum) { switch (authenticationIdentityEnum) {
default: default:
return userDetailsService.loadUserByUsername(authentication.getName()); return userDetailsService.loadUserByUsername(authentication.getName());
} }

View File

@ -58,6 +58,11 @@ public interface SecurityConstants {
String REFRESH_TOKEN_KEY = "refresh_token"; String REFRESH_TOKEN_KEY = "refresh_token";
/**
* 认证身份标识
*/
String AUTHENTICATION_IDENTITY_KEY = "authenticationIdentity";
String APP_API_PATTERN = "/*/app-api/**"; String APP_API_PATTERN = "/*/app-api/**";
String LOGOUT_PATH = "/youlai-auth/oauth/logout"; String LOGOUT_PATH = "/youlai-auth/oauth/logout";
@ -67,11 +72,6 @@ public interface SecurityConstants {
*/ */
String SAVE_MENU_PATH = "/youlai-admin/api/v1/menus"; String SAVE_MENU_PATH = "/youlai-admin/api/v1/menus";
/**
* 认证方式
*/
String AUTHENTICATION_METHOD = "authenticationMethod";
/** /**
* 验证码key前缀 * 验证码key前缀
*/ */

View File

@ -5,7 +5,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.nimbusds.jose.JWSObject; import com.nimbusds.jose.JWSObject;
import com.youlai.common.constant.SecurityConstants; import com.youlai.common.constant.SecurityConstants;
import com.youlai.common.enums.AuthenticationMethodEnum; import com.youlai.common.enums.AuthenticationIdentityEnum;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings; import org.apache.logging.log4j.util.Strings;
@ -62,22 +62,22 @@ public class RequestUtils {
} }
/** /**
* 解析JWT获取获取认证方式 * 解析JWT获取获取认证身份标识
* *
* @return * @return
*/ */
@SneakyThrows @SneakyThrows
public static String getAuthenticationMethod() { public static String getAuthenticationIdentity() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY); String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY);
String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload()); String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload());
JSONObject jsonObject = JSONUtil.parseObj(payload); JSONObject jsonObject = JSONUtil.parseObj(payload);
String authenticationMethod = jsonObject.getStr(SecurityConstants.AUTHENTICATION_METHOD); String authenticationIdentity = jsonObject.getStr(SecurityConstants.AUTHENTICATION_IDENTITY_KEY);
if (StrUtil.isBlank(authenticationMethod)) { if (StrUtil.isBlank(authenticationIdentity)) {
authenticationMethod = AuthenticationMethodEnum.USERNAME.getValue(); authenticationIdentity = AuthenticationIdentityEnum.USERNAME.getValue();
} }
return authenticationMethod; return authenticationIdentity;
} }
} }