我正在寻找一种干净的方法来在 React 中存储我的访问令牌。
经过一番研究,我发现使用全局变量是获得我想要的东西的有效“欺骗”。但是,我猜测是否有更清晰的方法来获得相同的结果。
//token.js
const access_token= "";
export const setAccessToken(token){
access_token=token;
}
export const getAccessToken(){
return access_token;
}
//NOTICE:
// -ON LOGIN/REFRESH: I set the access token
// -ON API CALLS: I get the access token and I add it to the header
Run Code Online (Sandbox Code Playgroud)
api.js
const baseURL= "http://my_base_url";
const generateApiInterface = ()=>{
let headers : any= {
};
token=getAccessToken(); //HERE I NEED TO RETRIEVE MY ACCESS TOKEN
if(token){
headers={ …Run Code Online (Sandbox Code Playgroud) 我有一个名为“ Consultant我的应用程序从用户那里收集数据”的类。
class Consultant {
final int id;
final String consultantFirstName;
final String consultantLastName;
final String consultantNickName;
final String consultantImageProfile;
final String consultantCategory;
final String consultantDescription;
final String consultantLikes;
final String consultantReviews;
final String consultantStars;
final String consultantPrice;
final String consultantExperience;
const Consultant({
this.id,
this.consultantFirstName,
this.consultantLastName,
this.consultantNickName,
this.consultantImageProfile,
this.consultantCategory,
this.consultantDescription,
this.consultantLikes,
this.consultantReviews,
this.consultantStars,
this.consultantPrice,
this.consultantExperience,
});
}
Run Code Online (Sandbox Code Playgroud)
下面是我的一位用户的示例:
final Consultant marco = Consultant(
id: 1,
consultantFirstName: 'Marco',
consultantLastName: 'Marcello',
consultantNickName: 'Tarot and Dreams',
consultantCategory: 'Tarocchi',
consultantDescription: 'Ciao a tutti …Run Code Online (Sandbox Code Playgroud) 我使用 JWKS 格式从身份验证服务提供公钥,该公钥可用于验证来自该身份验证服务的令牌。但是,要执行验证,我需要从 JWK 重建公钥。我该如何转换它?
type JWKeys struct {
Keys []JWKey `json:"keys"`
}
type JWKey struct {
Kty string `json:"kty"`
Use string `json:"use,omitempty"`
Kid string `json:"kid,omitempty"`
Alg string `json:"alg,omitempty"`
Crv string `json:"crv,omitempty"`
X string `json:"x,omitempty"`
Y string `json:"y,omitempty"`
D string `json:"d,omitempty"`
N string `json:"n,omitempty"`
E string `json:"e,omitempty"`
K string `json:"k,omitempty"`
}
var PublicKey *rsa.PublicKey
func SetUpExternalAuth() {
res, err := http.Get("my_url")
if err != nil {
log.Fatal("Can't retrieve the key for authentication")
}
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != …Run Code Online (Sandbox Code Playgroud) 最近我需要构建一个简单的 REST API,我阅读了关于最佳实践的不同文章,以尽可能减少我的 Web 应用程序的漏洞。在网上搜索,我找到了关于如何实现 JWT 令牌的不同教程,每个教程在某些方面都不同,我找不到一种节流良好的“通用方法”。最后,我实现了对我来说似乎最合理的解决方案,但我想确认这是处理此类身份验证的最有效方法。
在开始之前:
#STEP 1:认证后生成令牌:
#STEP 2:授权请求
为了授权请求,我只使用在请求标头中的字段中发送的访问令牌。仅当令牌有效时才允许请求。
#STEP 3:刷新访问令牌
为了刷新令牌,我向服务器发送刷新请求。服务器:
有关刷新过程的更多信息
刷新被称为:
进一步的预防措施:关键操作
刷新令牌有两个过期时间。第一个,对于非关键操作,每次发布新令牌时都会刷新。这样,如果用户继续使用该应用程序,他/她可能会永远保持登录状态。对于关键操作,第二个(持续 3 小时)是“绝对的”。这意味着,在每次刷新时,关键操作的“计时器”不会刷新。
//To make it simpler:
nextToken.criticalExpiration=previousToken.criticalExpiration
Run Code Online (Sandbox Code Playgroud)
在“关键”计时器到期后调用刷新请求时,生成的访问令牌有一个字段为 false,表示执行关键操作的选项。如果为 false,则不允许进行关键操作(因此用户必须重新进行身份验证才能执行这些请求)
概括:
我想了解此过程是否正确执行或是否会产生漏洞。我知道在应用程序的其他部分和/或外部库中总是可能存在漏洞,但是,我想尽量减少留下可以被利用的东西的可能性。
data我正在构建 API 以将我的 React 应用程序与后端服务连接起来,并且我想使用 TypeScript 来指定Axios 请求内部的类型。如何在不修改其他字段的情况下更新 Axios 响应中的数据类型(请参阅下面代码中的 getAllProjects)?
class MyApi {
constructor(token: string | null) {
let headers: any = {
'Content-Type': 'application/json',
};
if (token) {
headers = {
...headers, //append other basic proprieties
'Authorization': 'Token ' + token
}
}
this.baseEndpoint = axios.create({
baseURL: `${baseURL}`,
headers: headers
});
}
//DATA
const getAllProjects = async () : Promise<AxiosResponse<?????>> => this.baseEndpoint.get('/projects/');
}
Run Code Online (Sandbox Code Playgroud)
简单地分配所需的类型(假设data: string[]本例)会引发以下错误:
Argument of type 'string[]' is not assignable to parameter of …Run Code Online (Sandbox Code Playgroud) 当我尝试使用 SQLMock 和 Gorm.io 模拟 Postgres 插入时,我收到一条错误,指出该查询不是预期的。我尝试使用regexp.QuoteMeta()换行和转义字符串,但它不起作用。我添加并删除了 args 和 result,但错误仍然出现
如何通过 SQLMock 设置预期的查询?
我给你原始的 PostgresQuery 和 UserModel
//RAW QUERY
INSERT INTO "users" ("id","name","surname","birthdate","company","custom_claims","deleted") VALUES ($1,$2,$3,$4,$5,$6,$7)' with args [{Name: Ordinal:1 Value:my_user_id} {Name: Ordinal:2 Value:<nil>} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:<nil>} {Name: Ordinal:5 Value:<nil>} {Name: Ordinal:6 Value:<nil>} {Name: Ordinal:7 Value:<nil>}]
Run Code Online (Sandbox Code Playgroud)
//Gorm model
type User struct {
ID string `gorm:"primaryKey"`
Name *string
Surname *string
Birthdate *time.Time
Company *string
CustomClaims *json.RawMessage
Deleted gorm.DeletedAt
}
func (repository Repository) CreateUser(user users.User) (*users.User, error) { …Run Code Online (Sandbox Code Playgroud) 我已经使用ApexCharts库构建了一个图表,并且我想禁用拖动图表的可能性。我应该如何更新我的配置(见下文)?我在文档中找不到任何详细信息。这是我的图表:
//My component
<Chart
options={performanceLineChart.options}
series={performanceLineChart.series}
/>
//My configurations
export const performanceLineChart = {
options: {
chart: {
id: 'my-chart',
type: 'line' as 'line',
toolbar: {
show: false //Disable toolbar
}
},
stroke: {
width: 5,
curve: 'smooth' as 'smooth'
},
markers: {
size: 4,
hover: {
size: undefined,
sizeOffset: 2
}
},
xasis: {
type: 'numeric',
}
},
series: [{
name: 'Line 1',
data: [
[3066, 323],
[6317, 312],
[12498, 313],
[24091, 326]
]
},{
name: 'Line …Run Code Online (Sandbox Code Playgroud) reactjs ×4
typescript ×3
go ×2
javascript ×2
apexcharts ×1
axios ×1
cryptography ×1
firebase ×1
flutter ×1
go-gorm ×1
go-sqlmock ×1
jwk ×1
jwt ×1
pem ×1
public-key ×1
redux ×1
security ×1