可參考:
https://blog.csdn.net/She_lock/article/details/86241685
https://blog.csdn.net/weixin_42429220/article/details/107891095
https://www.jianshu.com/p/5cf82f092201?tdsourcetag=s_pctim_aiomsg
黎大神解決的
改一下BladeConfiguration
這個類就可以了
后端代碼如下
/**
* 跨域配置
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(false)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedOrigins("*");
}
前端代碼
就隨便在vs里面寫的一個頁面,然后用ajax請求一下就可以了
// 注意
// URL地址地址是localhost:10000/blade-auth, 不是localhost:1888/api/blade-auth
// 注意哦,昨天就采坑了 這里
// 請求授權
$.ajax({
type: "POST",
url: "http://localhost:10000/blade-auth/token?account=admin&grantType=password&password=admin&tenantId=000000",
dataType: 'json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'
},
success: function (response) {
console.log(response);
// 請求業務接口
var accessToken = response.data.accessToken;
$.ajax({
type: "GET",
url: "http://localhost:10000/blade-pms/brand/list?current=1&size=10",
dataType: 'json',
headers: {
'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
'Blade-Auth': 'bearer ' + accessToken,
},
success: function (response2) {
console.log(response2);
}
});
}
});
問題1:header中為什么要傳這個'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'
,以及這個是怎么來的?
原文:https://www.cnblogs.com/longsanshi/p/12740787.html
把c2FiZXI6c2FiZXJfc2VjcmV0
這個,用base64編碼的方式,解碼后是這樣的:saber:saber_secret
是不是有點恍然大悟的感覺了,標識了當前登錄的客戶端是哪個,以及對應的客戶端密鑰
數據庫對應的地方