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 {
}



