小编lou*_*ing的帖子

FastAPI - 支持多种身份验证依赖项

问题

我目前有名为 jwt 的 JWT 依赖项,它确保它在到达端点之前通过 JWT 身份验证阶段,如下所示:

sample_endpoint.py:

from fastapi import APIRouter, Depends, Request
from JWTBearer import JWTBearer
from jwt import jwks

router = APIRouter()

jwt = JWTBearer(jwks)

@router.get("/test_jwt", dependencies=[Depends(jwt)])
async def test_endpoint(request: Request):
    return True
Run Code Online (Sandbox Code Playgroud)

以下是使用 JWT 对用户进行身份验证的 JWT 依赖项(来源:https ://medium.com/datadriveninvestor/jwt-authentication-with-fastapi-and-aws-cognito-1333f7f2729e ):

JWTBearer.py

from typing import Dict, Optional, List

from fastapi import HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from jose import jwt, jwk, JWTError
from jose.utils import base64url_decode
from pydantic import BaseModel
from starlette.requests import Request …
Run Code Online (Sandbox Code Playgroud)

python oauth jwt openapi fastapi

14
推荐指数
2
解决办法
8821
查看次数

为多线程 FizzBu​​zz 编写测试用例

我正在通过此链接解决 LeetCode 问题:https ://leetcode.com/problems/fizz-buzz-multithreaded/ 。

基本上,我正在编写一个执行标准 fizzbuzz 的 FizzBu​​zz 类,但同时运行 4 个线程,每个线程调用不同的方法。(一个线程正在呼叫 fizzbuzz,一个线程在呼叫 fizz,一个呼叫 buzz 和一个呼叫号码)。这是我接受的解决方案:

class FizzBuzz {
     private int n;
     int num = 1; // FizzBuzz starts at 1.
     public FizzBuzz(int n) {
         this.n = n;
     }

     // printFizz.run() outputs "fizz".
     public synchronized void fizz(Runnable printFizz) throws InterruptedException {
         while(num <= n) {
             if(num % 3 == 0 && num % 5 != 0) {
                 printFizz.run();
                 num++;
                 notifyAll();
             } else {
                 wait();
             }
         }
     }

     // printBuzz.run() …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-safety

5
推荐指数
1
解决办法
651
查看次数

SpringBoot NoSuchMethodError

我建立连接到MongoDB的Atlas公司,具有JPA库简单的URL缩短器,当我尝试保存URL数据时,请求命中发布请求,我得到以下错误:java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertOne(Ljava/lang/Object;)Lcom/mongodb/client/result/InsertOneResult;。根据研究,我认为这是一个依赖性问题,但无法解决。

网址.java:

package com.sideproject.urlshortner.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document(collection = "url")
public class URL {
    @Id
    private String id;
    String longURL;
    String shortenedURL;

    public URL(String longURL, String shortenedURL) {
        this.longURL = longURL;
        this.shortenedURL = shortenedURL;
    }

}
 
Run Code Online (Sandbox Code Playgroud)

URLController.java:

package com.sideproject.urlshortner.controller;

import com.sideproject.urlshortner.repository.URLRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.sideproject.urlshortner.model.URL;


@RestController
@RequestMapping("/index")
public class URLController {

    @Autowired
    private URLRepository urlRepository;


    @RequestMapping(value="/urls/", method=RequestMethod.POST)
    public URL postURL(@RequestBody URL url) {
        return urlRepository.save(url); // giving the error. …
Run Code Online (Sandbox Code Playgroud)

java spring jpa mongodb spring-boot

5
推荐指数
2
解决办法
1153
查看次数

jackson/json 模式 - 用外部 json 模式生成器模块替换 ISO8601Utils

ISO8601Utils由于 SonarQube 抛出以下错误,我正在替换下面评论的内容:Remove this use of "ISO8601Utils"; it is deprecated.要替换它,我将使用外部 json 模式生成器模块,https://github.com/FasterXML/jackson-module-jsonSchema或其他内容。我通读了链接,但不明白如何使用对象映射器来替换这一行:String value = ISO8601Utils.format(date, true);

    public static class ISO8601DateFormat extends DateFormat {

    public ISO8601DateFormat() {}

    public StringBuffer format(Date date, StringBuffer toAppendTo,
            FieldPosition fieldPosition) {
        String value = ISO8601Utils.format(date, true);
        //Im not sure how I can replace this line with a new
        //replacement

        toAppendTo.append(value);
        return toAppendTo;
    }

    public Date parse(String source, ParsePosition pos) {
        pos.setIndex(source.length());
        return ISODateTimeFormat.dateTimeParser().parseDateTime(source).toDate();
    }

    public Object clone() {
        return this; …
Run Code Online (Sandbox Code Playgroud)

java json deprecated jackson sonarqube

3
推荐指数
1
解决办法
3656
查看次数