feat(auth): 添加MK认证端点和第三方依赖
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package org.springblade.auth.endpoint;
|
||||
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.oauth2.endpoint.AbstractOAuth2MKEndpoint;
|
||||
import org.springblade.core.oauth2.granter.TokenGranterFactory;
|
||||
import org.springblade.core.oauth2.handler.TokenHandler;
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
import org.springblade.core.tool.utils.UrlUtil;
|
||||
import org.springblade.thirdparty.mk.config.MKProperties;
|
||||
import org.springblade.thirdparty.mk.service.IMKService;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author bfhuange
|
||||
* @date 2024/9/17
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@Tag(name = "跳转mk认证", description = "跳转mk认证端点")
|
||||
public class OAuth2MKEndpoint extends AbstractOAuth2MKEndpoint {
|
||||
/**
|
||||
* 登录链接
|
||||
*/
|
||||
private static final String LOGIN_URL_FORMAT = "%s%s?appId=%s&redirection_url=%s";
|
||||
|
||||
private final MKProperties mkProperties;
|
||||
private final IMKService mkService;
|
||||
|
||||
public OAuth2MKEndpoint(TokenGranterFactory granterFactory, TokenHandler tokenHandler, MKProperties mkProperties, IMKService mkService) {
|
||||
super(granterFactory, tokenHandler);
|
||||
this.mkProperties = mkProperties;
|
||||
this.mkService = mkService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String generateLoginUrl(String refererUrl) {
|
||||
// 重定向到来源
|
||||
return String.format(LOGIN_URL_FORMAT, mkProperties.getLoginUrl(), mkProperties.getMkSsoLoginUrl(), mkProperties.getOauthAppId(), UrlUtil.encode(refererUrl));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAccountByMkCode(String mkCode) {
|
||||
return mkService.getMKAccount(mkCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean checkRefererUrl(String refererUrl) {
|
||||
if (!mkProperties.isCheckReferer()) {
|
||||
// 未开启校验来源,校验通过
|
||||
return true;
|
||||
}
|
||||
if (StringUtil.isBlank(mkProperties.getErpBaseUrls())) {
|
||||
log.error("未配置erp地址,无法校验来源");
|
||||
return false;
|
||||
}
|
||||
if (StringUtil.isBlank(refererUrl)) {
|
||||
// 来源地址为空,通过,以便用于调试
|
||||
return true;
|
||||
}
|
||||
// 匹配到一个erp基础地址就行了
|
||||
return Stream.of(mkProperties.getErpBaseUrls().split(","))
|
||||
.map(String::trim)
|
||||
.anyMatch(refererUrl::startsWith);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.springblade.auth.exception;
|
||||
|
||||
import org.springblade.auth.endpoint.OAuth2MKEndpoint;
|
||||
import org.springblade.core.oauth2.exception.OAuth2ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author bfhuange
|
||||
* @date 2024/10/16
|
||||
*/
|
||||
@ControllerAdvice(basePackageClasses = OAuth2MKEndpoint.class)
|
||||
public class OAuth2MKExceptionHandler extends OAuth2ExceptionHandler {
|
||||
}
|
||||
@@ -8,6 +8,7 @@ spring:
|
||||
import:
|
||||
- nacos:blade.yaml?group=DEFAULT_GROUP&refreshEnabled=true
|
||||
- nacos:blade-${spring.profiles.active}.yaml?group=DEFAULT_GROUP&refreshEnabled=true
|
||||
- nacos:third-party-api.yaml?group=DEFAULT_GROUP&refreshEnabled=true
|
||||
- optional:nacos:${spring.application.name}-${spring.profiles.active}.yaml?group=DEFAULT_GROUP&refreshEnabled=true
|
||||
cloud:
|
||||
nacos:
|
||||
|
||||
Reference in New Issue
Block a user