Commit 9ee46006 authored by fangxinjiang's avatar fangxinjiang

ekp-common

parent bf2fb84c
......@@ -9,19 +9,11 @@
<version>1.0.0</version>
</parent>
<artifactId>yifu-common-test</artifactId>
<artifactId>yifu-common-ekp</artifactId>
<packaging>jar</packaging>
<description>yifu oauth 2.0 单元测试工具类</description>
<description>yifu EKP 工具类</description>
<dependencies>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>
</dependencies>
</project>
package com.yifu.cloud.plus.v1.yifu.ekp.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Author fxj
* @Description 薪资明细对接
* @Date 11:57 2022/8/22
* @Param
* @return
**/
@Data
@Component
@PropertySource("classpath:ekpSalaryConfig.properties")
@ConfigurationProperties(value = "ekp", ignoreInvalidFields = false)
public class EkpSalaryProperties {
String url;
String fdModelId;
String fdFlowId;
String docStatus;
String LoginName;
String docSubject;
}
package com.yifu.cloud.plus.v1.yifu.ekp.util;
import com.yifu.cloud.plus.v1.yifu.common.ekp.config.EkpSalaryProperties;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* @Author fxj
* @Date 2022/7/11
* @Description
* @Version 1.0
*/
@Log4j2
@EnableConfigurationProperties(EkpSalaryProperties.class)
public class EkpSalaryUtil {
@Autowired
private EkpSalaryProperties ekpProperties;
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yifu.cloud.plus.v1.yifu.common.dapr.util.EkpSalaryUtil
\ No newline at end of file
com.yifu.cloud.plus.v1.yifu.ekp.util.EkpSalaryUtil
\ No newline at end of file
ekp.url=http://119.96.227.251:8080/api/sys-modeling/appModelRestService/addModel
ekp.fdModelId='181d7633ff8bc797276d0d3a54e80ad6'
ekp.fdFlowId='182b40249c1bc940d7226b941c7a4183'
ekp.docStatus='20'
ekp.LoginName='admin'
ekp.docSubject='\u85AA\u8D44\u8BA2\u5355\u660E\u7EC6\u6570\u636E\u63A5\u53E3'
/*
* Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yifu.cloud.plus.v1.yifu.test;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.SpringContextHolder;
import com.yifu.cloud.plus.v1.yifu.test.annotation.WithMockOAuth2User;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.test.context.support.WithSecurityContextFactory;
/**
* @author lengleng
* @date 2020/9/22
* <p>
* oauth2 上下文生成处理器
*/
public class WithMockSecurityContextFactory implements WithSecurityContextFactory<WithMockOAuth2User> {
@Override
public SecurityContext createSecurityContext(WithMockOAuth2User oAuth2User) {
// 1. 请求认证中心获取token
String token = getToken(oAuth2User);
// 2. 解析认证中心返回用户
OAuth2Authentication authentication = getUser(token);
// 3. 构建 oauth2 上下文
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
// 4. 上下文保存 token
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(token);
OAuth2ClientContext clientContext = SpringContextHolder.getBean(OAuth2ClientContext.class);
clientContext.setAccessToken(accessToken);
return context;
}
/**
* 请求认证中心获取token
* @param oAuth2User 账号、密码
* @return String token
*/
private String getToken(WithMockOAuth2User oAuth2User) {
OAuth2ProtectedResourceDetails clientProperties = SpringContextHolder
.getBean(OAuth2ProtectedResourceDetails.class);
String result = HttpRequest.post(clientProperties.getAccessTokenUri())
.basicAuth(clientProperties.getClientId(), clientProperties.getClientSecret())
.form("username", oAuth2User.username()).form("password", oAuth2User.password())
.form("grant_type", "password").form("scope", clientProperties.getScope()).execute().body();
return JSONUtil.parseObj(result).getStr("access_token");
}
/**
* 使用token 获取用户详情
* @param token token
* @return user详细
*/
private OAuth2Authentication getUser(String token) {
RemoteTokenServices tokenServices = SpringContextHolder.getBean(RemoteTokenServices.class);
return tokenServices.loadAuthentication(token);
}
}
package com.yifu.cloud.plus.v1.yifu.test.annotation;
import com.yifu.cloud.plus.v1.yifu.test.WithMockSecurityContextFactory;
import org.springframework.security.test.context.support.WithSecurityContext;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author lengleng
* @date 2020/9/22
* <p>
* WithMockOAuth2User 注解
*/
@Retention(RetentionPolicy.RUNTIME)
@WithSecurityContext(factory = WithMockSecurityContextFactory.class)
public @interface WithMockOAuth2User {
/**
* 用户名
*/
String username() default "admin";
/**
* 密码
*/
String password() default "123456";
}
\ No newline at end of file
package com.yifu.cloud.plus.v1.yifu.test.kit;
import com.yifu.cloud.plus.v1.yifu.common.core.util.SpringContextHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
/**
* @author lengleng
* @date 2020/9/22
* <p>
* Mock 工具类
*/
public class OAuthMockKit {
/**
* mock 请求增加统一请求头
* @return RequestPostProcessor 类似于拦截器
*/
public static RequestPostProcessor token() {
return mockRequest -> {
OAuth2ClientContext clientContext = SpringContextHolder.getBean(OAuth2ClientContext.class);
String token = clientContext.getAccessToken().getValue();
mockRequest.addHeader(HttpHeaders.AUTHORIZATION, String.format("Bearer: %s", token));
return mockRequest;
};
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment