会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132482个问题
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask数据库 14956楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 14958楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 14959楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 14960楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 14963楼
Python 全系列/第一阶段:Python入门/编程基本概念 14964楼
Python 全系列/下架-第十二阶段:Python_大型电商项目(5天后下架)/Django项目阶段-电商项目(旧) 14966楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 14967楼

老师您好。我这个mongodb一开启auth=true, test中的代码执行完就会提示如下的unauthorized错误。我已经尝试了多种方式,包括多种yml中配置的格式,以及在@SpringBootApplication后面添加(exclude=...........)的这种写法,全都不管用。


后来删掉auth=true,就可以运行成功。而且很奇怪的是,开启用户认证后不光test中的代码会提示错误,就算在mongdb控制台中已经可以验证通过后再操作几分钟之后,mongdb控制台也会提示unauthorize的错误,类似db.shutdownServer()也无法执行了。好奇怪,请问这个有什么解决方式吗?


org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13 (Unauthorized): 'not authorized on livegoods to execute command { insert: "banner", ordered: true, $db: "livegoods" }' on server 192.168.86.147:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on livegoods to execute command { insert: \"banner\", ordered: true, $db: \"livegoods\" }", "code": 13, "codeName": "Unauthorized"}; nested exception is com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'not authorized on livegoods to execute command { insert: "banner", ordered: true, $db: "livegoods" }' on server 192.168.86.147:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on livegoods to execute command { insert: \"banner\", ordered: true, $db: \"livegoods\" }", "code": 13, "codeName": "Unauthorized"}


at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138)

at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2902)

at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:587)

at org.springframework.data.mongodb.core.MongoTemplate.insertDocumentList(MongoTemplate.java:1519)

at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:1384)

at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1308)

at com.livegoods.mongodb.TestDatas.insertBanners(TestDatas.java:51)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)

at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'not authorized on livegoods to execute command { insert: "banner", ordered: true, $db: "livegoods" }' on server 192.168.86.147:27017. The full response is {"ok": 0.0, "errmsg": "not authorized on livegoods to execute command { insert: \"banner\", ordered: true, $db: \"livegoods\" }", "code": 13, "codeName": "Unauthorized"}

at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175)

at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:303)

at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)

at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)

at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:450)

at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)

at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:218)

at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)

at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)

at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:435)

at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:261)

at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:72)

at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:205)

at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:196)

at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:501)

at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:196)

at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:71)

at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:206)

at com.mongodb.client.internal.MongoCollectionImpl.executeInsertMany(MongoCollectionImpl.java:524)

at com.mongodb.client.internal.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:508)

at com.mongodb.client.internal.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:503)

at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocumentList$17(MongoTemplate.java:1526)

at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:585)

... 34 more


JAVA 全系列/第二十阶段:租房网(Spring Cloud最新架构)/Livegoods第一天 14968楼

# encoding = utf-8
"""
    The name of the game: Aircraft war game.
    This is a small game developed by myself.
"""
import pygame
import time

SCREEN_HEIGHT = 768
SCREEN_WIDTH = 512
class MainGame():
    """主类"""
    window = None
    my_plane = None

    def __init__(self):
        pass
        
    def startGame(self):
        # 加载主窗口\初始化窗口
        pygame.display.init()
        # 设置窗口的大小及显示宽高
        MainGame.window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
        # 设置窗口的标题
        pygame.display.set_caption("飞机大战")
        # 加载背景图片
        background = pygame.image.load(r"images/bg/bg.jpg")
        # 初始化我方飞机
        MainGame.my_plane = MyPlane(200, 550)
        while True:
            # 给窗口设置背景图片
            MainGame.window.blit(background, (0, 0))
            self.getEvent()
            # 调用显示我方飞机的方法
            MainGame.my_plane.displayMyPlane()
            # 调用我方飞机移动方法
            if MainGame.my_plane.stop:
                MainGame.my_plane.move()
            pygame.display.update()
            time.sleep(0.02)

    def endGame(self):
        print("客观下次再来玩呀")
        exit()

    def getEvent(self):
        # 获取所有事件
        eventList = pygame.event.get()
        for event in eventList:
            # 判断按下的键是关闭还是按下
            if event.type == pygame.QUIT:
                self.endGame()
            if event.type == pygame.KEYDOWN:
                # 判断按下的是上下左右键
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    MyPlane.direction_left = True
                    MyPlane.stop = True
                    print('向左')
                    #MainGame.my_plane.move()
                elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    MyPlane.direction_right = True
                    MyPlane.stop = True
                    print('向右')
                    #MainGame.my_plane.move()
                elif event.key == pygame.K_UP or event.key == pygame.K_w:
                    MyPlane.direction_up = True
                    MyPlane.stop = True
                    print('向上')
                    #MainGame.my_plane.move()
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    MyPlane.direction_down = True
                    MyPlane.stop = True
                    print('向下')
                    #MainGame.my_plane.move()
            
                

class Plane():
    """飞机类"""
    def __init__(self):
        pass

    def move(self):
        """飞机可以移动"""
        pass

    def shot(self):
        """飞机可以射击"""
        pass
    
    def displayAircraft(self):
        """展示飞机的方法"""

class MyPlane(Plane):
    """我方飞机"""
    def __init__(self, left, top):
        self.image = pygame.image.load(r'images\hero\hero_b_04.png')
        # 根据图片获取区域
        self.rect = self.image.get_rect()
        # 设置区域的 left和 top
        self.rect.left = left
        self.rect.top = top
        # 飞机的速度
        self.speed = 5
        # 飞机移动的开关
        self.stop = False
        # 飞机的移动方向
        self.direction_left =False
        self.direction_right =False
        self.direction_up =False
        self.direction_down = False
        
    
    def displayMyPlane(self):
        MainGame.window.blit(self.image, self.rect)

    def move(self):
        if self.direction_left:
            if self.rect.left > 0:
                self.rect.left -= self.speed
        elif self.direction_up:
            if self.rect.top > 0:
                self.rect.top -= self.speed
        elif self.direction_right:
            if self.rect.left + self.rect.width < SCREEN_WIDTH:
                self.rect.left += self.speed
        elif self.direction_down:
            if self.rect.top + self.rect.height < SCREEN_HEIGHT:
                self.rect.top += self.speed
        



class EnemyPlane(Plane):
    """敌方飞机"""
    pass

class Bullet():
    """子弹类"""
    def __init__(self):
        pass

    def move(self):
        """子弹移动"""

    def displayBullet(self):
        """展示子弹的方法"""
        pass

class Explode():

    """爆炸效果"""
    def __init__(self):
        pass

    def displayExplode(self):
        """展示爆炸效果的方法"""
        pass


if __name__ == "__main__":
    MainGame().startGame()

老师,我得小飞机,把移动方法放到while True里面后,它就失灵了。

我就加了个加了个飞机四个移动方向得开关,一开始是False,当按下对应得方向按键,对应得开关也编程True,同时,小飞机得移动开关也会变成True。从而可以调用move方法,这是我得想法,但是他不会动

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 14969楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 14970楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637