Skip to content

Commit

Permalink
refactor: 认证中心升级(临时提交勿clone)
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxianrui committed Jun 9, 2023
1 parent 84aa6da commit 9a99197
Show file tree
Hide file tree
Showing 31 changed files with 566 additions and 456 deletions.
4 changes: 2 additions & 2 deletions laboratory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<description>有来实验室</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.youlai.mall.oms.model.entity.OmsOrder;
import com.youlai.mall.oms.model.entity.OmsOrderItem;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import lombok.Data;
import lombok.experimental.Accessors;

Expand All @@ -22,6 +22,6 @@ public class OrderDTO {

private List<OmsOrderItem> orderItems;

private MemberDTO member;
private MemberRegisterDto member;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.youlai.mall.pms.model.vo.ProductHistoryVO;
import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.dto.MemberAuthDTO;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import com.youlai.mall.ums.dto.MemberInfoDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
Expand All @@ -27,7 +27,7 @@ public interface MemberFeignClient {
* @return
*/
@PostMapping("/app-api/v1/members")
Result<Long> addMember(@RequestBody MemberDTO member);
Result<Long> registerMember(@RequestBody MemberRegisterDto member);

/**
* 获取会员的 openid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.youlai.mall.ums.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
Expand All @@ -10,7 +12,8 @@
* @since 2022/2/12
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class MemberAuthDTO {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @since 2022/2/12
*/
@Data
public class MemberDTO {
public class MemberRegisterDto {

private Integer gender;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.youlai.mall.pms.model.vo.ProductHistoryVO;
import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.dto.MemberAuthDTO;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import com.youlai.mall.ums.model.entity.UmsMember;
import com.youlai.mall.ums.model.vo.MemberVO;
import com.youlai.mall.ums.service.IUmsMemberService;
Expand Down Expand Up @@ -40,7 +40,7 @@ public Result<String> getMemberById(@Parameter(name = "会员ID") @PathVariable

@Operation(summary= "新增会员")
@PostMapping
public Result<Long> addMember(@RequestBody MemberDTO member) {
public Result<Long> addMember(@RequestBody MemberRegisterDto member) {
Long memberId = memberService.addMember(member);
return Result.success(memberId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.youlai.mall.ums.convert;

import com.youlai.mall.ums.dto.MemberAuthDTO;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import com.youlai.mall.ums.dto.MemberInfoDTO;
import com.youlai.mall.ums.model.entity.UmsMember;
import org.mapstruct.Mapper;
Expand Down Expand Up @@ -30,5 +30,5 @@ public interface MemberConvert {

MemberInfoDTO entity2MemberInfoDTO(UmsMember entity);

UmsMember dto2Entity(MemberDTO memberDTO);
UmsMember dto2Entity(MemberRegisterDto memberRegisterDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.youlai.mall.pms.model.vo.ProductHistoryVO;
import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.dto.MemberAuthDTO;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import com.youlai.mall.ums.dto.MemberInfoDTO;
import com.youlai.mall.ums.model.entity.UmsMember;
import com.youlai.mall.ums.model.vo.MemberVO;
Expand Down Expand Up @@ -51,7 +51,7 @@ public interface IUmsMemberService extends IService<UmsMember> {
* @param member
* @return
*/
Long addMember(MemberDTO member);
Long addMember(MemberRegisterDto member);

/**
* 获取登录会员信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.youlai.mall.ums.convert.MemberConvert;
import com.youlai.mall.ums.dto.MemberAddressDTO;
import com.youlai.mall.ums.dto.MemberAuthDTO;
import com.youlai.mall.ums.dto.MemberDTO;
import com.youlai.mall.ums.dto.MemberRegisterDto;
import com.youlai.mall.ums.dto.MemberInfoDTO;
import com.youlai.mall.ums.mapper.UmsMemberMapper;
import com.youlai.mall.ums.model.entity.UmsAddress;
Expand Down Expand Up @@ -114,12 +114,12 @@ public MemberAuthDTO getMemberByMobile(String mobile) {
/**
* 新增会员
*
* @param memberDTO
* @param memberRegisterDTO
* @return
*/
@Override
public Long addMember(MemberDTO memberDTO) {
UmsMember umsMember = memberConvert.dto2Entity(memberDTO);
public Long addMember(MemberRegisterDto memberRegisterDTO) {
UmsMember umsMember = memberConvert.dto2Entity(memberRegisterDTO);
boolean result = this.save(umsMember);
Assert.isTrue(result, "新增会员失败");
return umsMember.getId();
Expand Down
16 changes: 3 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
<spring-boot.version>3.1.0</spring-boot.version>

<!-- spring cloud & alibaba -->
<spring-cloud.version>2022.0.2</spring-cloud.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version>
<!-- -->
<!-- spring authorization server -->
<authorization-server.version>1.1.0</authorization-server.version>
<nimbus-jose-jwt.version>9.16.1</nimbus-jose-jwt.version>

<!-- db && orm -->
<mysql.version>8.0.28</mysql.version>
<druid.version>1.2.16</druid.version>
<druid.version>1.2.18</druid.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version>

<!-- api doc -->
Expand All @@ -66,7 +66,6 @@
<thumbnailator.version>0.4.17</thumbnailator.version>

<!-- distributed -->
<seata.version>1.5.2</seata.version>
<redisson.version>3.21.0</redisson.version>

<!-- 阿里云短信 -->
Expand Down Expand Up @@ -158,22 +157,13 @@
<version>${weixin-java.version}</version>
</dependency>

<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.version}</version>
</dependency>


<!-- 分布式锁 -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>${redisson.version}</version>
</dependency>



<dependency>
<groupId>com.youlai</groupId>
<artifactId>common-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.youlai.auth.authentication.mobile;

import cn.hutool.core.util.StrUtil;
import com.youlai.auth.userdetails.member.MemberUserDetailsServiceImpl;
import com.youlai.auth.userdetails.member.MmsUserDetailsService;
import com.youlai.common.constant.SecurityConstants;
import com.youlai.common.web.exception.BizException;
import com.youlai.mall.ums.api.MemberFeignClient;
Expand Down Expand Up @@ -44,7 +44,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
// 比对成功删除缓存的验证码
redisTemplate.delete(codeKey);
}
UserDetails userDetails = ((MemberUserDetailsServiceImpl) userDetailsService).loadUserByMobile(mobile);
UserDetails userDetails = ((MmsUserDetailsService) userDetailsService).loadUserByMobile(mobile);
SmsCodeAuthenticationToken result = new SmsCodeAuthenticationToken(userDetails, authentication.getCredentials(), new HashSet<>());
result.setDetails(authentication.getDetails());
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.youlai.auth.authentication.refresh;

import com.youlai.auth.config.AuthorizationServerConfig;
import com.youlai.auth.userdetails.member.MemberUserDetailsServiceImpl;
import com.youlai.auth.userdetails.member.MmsUserDetailsService;
import com.youlai.auth.util.RequestUtils;
import com.youlai.common.constant.SecurityConstants;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -54,13 +53,13 @@ public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundExce
switch (clientId) {
case SecurityConstants.APP_CLIENT_ID -> {
// 移动端的用户体系是会员,认证方式是通过手机号 mobile 认证
MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService;
return memberUserDetailsService.loadUserByUsername(authentication.getName());
MmsUserDetailsService mmsUserDetailsService = (MmsUserDetailsService) userDetailsService;
return mmsUserDetailsService.loadUserByUsername(authentication.getName());
}
case SecurityConstants.WEAPP_CLIENT_ID -> {
// 小程序的用户体系是会员,认证方式是通过微信三方标识 openid 认证
MemberUserDetailsServiceImpl memberUserDetailsService = (MemberUserDetailsServiceImpl) userDetailsService;
return memberUserDetailsService.loadUserByOpenId(authentication.getName());
MmsUserDetailsService mmsUserDetailsService = (MmsUserDetailsService) userDetailsService;
return mmsUserDetailsService.loadUserByOpenId(authentication.getName());
}
// 管理系统的用户体系是系统用户,认证方式通过用户名 username 认证
default -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.youlai.auth.authentication.wechat;

import lombok.Getter;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeAuthenticationToken;
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationGrantAuthenticationToken;

import java.util.Map;

/**
* 微信小程序
*
* @author haoxr
* @see OAuth2AuthorizationCodeAuthenticationToken
* @since 3.0.0
*/
public class WeChatMiniProgramAuthenticationToken extends OAuth2AuthorizationGrantAuthenticationToken {

/**
* 授权类型:微信小程序
*/
public static final AuthorizationGrantType WECHAT_MINI_PROGRAM = new AuthorizationGrantType("wechat_mini_program");

@Getter
private final String code;

@Getter
private final String encryptedData;

@Getter
private final String iv;


/**
* @see org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames#SCOPE
*/
private final String scope;


/**
* Sub-class constructor.
*
* @param clientPrincipal the authenticated client principal
* @param additionalParameters the additional parameters
*/
protected WeChatMiniProgramAuthenticationToken(
Authentication clientPrincipal,
Map<String, Object> additionalParameters,
String code,
String encryptedData,
String iv,
String scope
) {
super(WeChatMiniProgramAuthenticationToken.WECHAT_MINI_PROGRAM, clientPrincipal, additionalParameters);
this.code = code;
this.encryptedData = encryptedData;
this.iv = iv;
this.scope = scope;
}
}
Loading

0 comments on commit 9a99197

Please sign in to comment.