栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

加解密+远程请求

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

加解密+远程请求

1,发送远程请求

public class A {
    @Value("${bizopen.url.aaoms.saveStudent}")
    private String saveStudentUrl;
    @Autowired
    private AuthValidatorProperties propAuth;
   
    public JSonObject save() {
        JSonObject json = new JSonObject();
        json.put("no","1111");
        //远程请求
        JSonObject result = this.fetchDataFromOpenServiceSuccess(saveStudentUrl, json, HttpMethod.POST);
    }

    public JSonObject fetchDataFromOpenServiceSuccess(String url, JSonObject body, HttpMethod method) {
    JSonObject result = this.exchangeWithRestTemplate(url, body, getRequestHeader(), method);
    return result;
    }
    
    punlic MultiValueMap getRequestHeader() {
    
     MultiValueMap header = new linkedMultiValueMap<>();
        List values = new ArrayList<>();
        String random = UUID.randomUUID().toString().replace("-", "");

        String str = PortalConstant.AUTHORIZATION_PARAM_KEY_APPID + "=" + propAuth.getAppId() + "&"
                + PortalConstant.AUTHORIZATION_PARAM_KEY_TIMESTAMP + "=" + System.currentTimeMillis() + "&"
                + PortalConstant.AUTHORIZATION_PARAM_KEY_RANDOM + "=" + random;

        String appSecret = EncryptUtil.getInstance().desDecode(propAuth.getAppSecret(), propAuth.getAppId());
        String encryStr = str + "&" + PortalConstant.AUTHORIZATION_PARAM_KEY_SIGN + "="
                + EncryptUtil.getInstance().desEncode(str, appSecret);
        String auth = PortalConstant.AUTHORIZATION_BEARER + EncryptUtil.getInstance().base64Encode(encryStr) + random;
        values.add(auth);
        header.put(PortalConstant.AUTHORIZATION, values);
        return header; 
    }
    
    public JSonObject exchangeWithRestTemplate(String url, JSonObject body, MultiValueMap headers, HttpMethod method) {
        RestTemplate rest = new RestTemplate();
        try {
	        ResponseEntity res = rest.exchange(url, method, new HttpEntity<>(body, headers), JSONObject.class);
			if (res != null) {
				return res.getBody();
			}
        } catch (RestClientException e) {
        	return "Failed to connect remote server!!!";
        }
        return null;
    }
}

 实体类AuthValidatorProperties

@Data
@ConfigurationProperties(prefix = "auth.validator")
public class AuthValidatorProperties {

    private String appId;

    private String appSecret;
}

常量类PortalConstant

public class PortalConstant {
    
    public final static String AUTHORIZATION_PARAM_KEY_APPID = "appId";
    public final static String AUTHORIZATION_PARAM_KEY_TIMESTAMP = "timestamp";
    public final static String AUTHORIZATION_PARAM_KEY_RANDOM = "randomStr";
    public final static String AUTHORIZATION_PARAM_KEY_SIGN = "sign";

    
    public final static String AUTHORIZATION = "Authorization";
    
    public final static String AUTHORIZATION_BEARER = "Bearer ";
}

配置文件 application.yml https://blog.csdn.net/weixin_42193908/article/details/118015425

appid 与 appSecret 有无加密都可,根据自己需要

bizopen:
  url:
    aaoms:
      base: http://localhost:8080/bizopen/aaoms
#自行配置值 
auth:
  validator:
    appId: xxxxxxxxxx
    appSecret: xxxxxxxxxxxxxxxxx

2,接收 解密 url 需要自行增加或选择不增加,代码修改即可(这里url 为远程接口地址)

定义配置类

@Configuration
public class EnableAccessConfig {
	@Value("${app.url.uiap.appInfo}")
	private String url;
	
	@Bean
	public AccessAuthorizationValidator accessAuthorizationValidator() {
		return new AccessAuthorizationValidator(url);
	}
	
	@Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean<>();
        bean.setFilter(new AccessAuthorizationFilter(this.accessAuthorizationValidator()));
        bean.addUrlPatterns("
    public final static String AUTHORIZATION_PARAM_KEY_APPID = "appId";
    public final static String AUTHORIZATION_PARAM_KEY_TIMESTAMP = "timestamp";
    public final static String AUTHORIZATION_PARAM_KEY_RANDOM = "randomStr";
    public final static String AUTHORIZATION_PARAM_KEY_SIGN = "sign";
    
    public final static String UIAP_DATA_KEY_APP_ID = "appId";
    public final static String UIAP_DATA_KEY_APP_SECRET = "appSecret";

    
    public final static String AUTHORIZATION = "Authorization";
    
    public final static String AUTHORIZATION_BEARER = "Bearer ";
    
    public final static String CONTENT_TYPE = "Content-Type";
    
    public final static String CONTENT_TYPE_VALUE = "application/json;charset=UTF-8";



}

3,衍生 springboot 注解模式,在使用的地方添加注解@EnableAccessFilter(一般在启动类加)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@import({ EnableAccessConfig.class })
public @interface EnableAccessFilter {

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号