'Error 401 Unauthorized' for Neo4j REST url on Heroku

让人想犯罪 __ 提交于 2019-12-24 08:24:59

问题


I am using correct NEO4J_URL environment variable as shown in heroku config. If I use the same neo4j url via a browser, it works fine (no auth failure). However my application fails to start with below error on heroku. (I am using spring-data-neo4j)

The URL looks like http://username:password@123456ac6.hosted.neo4j.org:1234/db/data/. Tried without '/db/data' at the end and also with and without trailing slash. Didn't help.

Please help if anyone has already faced/resolved this issue. The stack trace on server startup:

←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m Caused by:
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.index.RestIndexManager.existsForNodes(RestIndexManager.java:45)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m java.lang.RuntimeException: Error reading as JSON '<html>|<head>|<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>|<tit
le>Error 401 UNAUTHORIZED</title>|</head>|<body><h2>HTTP ERROR 401</h2>|<p>Problem accessing /db/data/index/node. Reason:|<pre>    UNAUTHORIZED</pre></p><hr /><i><small>Powered by Jetty://</
small></i><br/>                                                |<br/>                                                |<br/>                                                |<br/>
                                   |<br/>                                                |<br/>                                                |<br/>
       |<br/>                                                |<br/>                                                |<br/>                                                |<br/>
                                 |<br/>                                                |<br/>
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.util.JsonHelper.jsonToSingleValue(JsonHelper.java:62)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.RequestResult.toEntity(RequestResult.java:114)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m                      |<br/>                                                |<br/>                                                |<br/>
                                        |<br/>                                                |<br/>                                                |<br/>
            |<br/>                                                ||</body>|</html>|'
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.RequestResult.toMap(RequestResult.java:120)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:57)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.index.RestIndexManager.indexInfo(RestIndexManager.java:50)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.chooseStrategy(TypeRepresentationStrategyFactor
y.java:56)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.neo4j.rest.graphdb.ExecutingRestAPI.indexInfo(ExecutingRestAPI.java:327)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$36bc2a37.CGLIB$typeRepresentationStrategyFactory$7(<generated>)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m  at org.springframework.data.neo4j.rest.SpringRestGraphDatabase.getIndex(SpringRestGraphDatabase.java:100

回答1:


As suggested by @MichaelHunger I used other SpringRestGraphDatabase Constructor which takes username and password as separate parameters, now the neo database is accessible.

My earlier spring context looked like this

<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
    <constructor-arg index="0" value="${NEO4J_URL}" />
</bean>

Now I changed it to as below and everything works fine! Here I provide the username and password via two additional environment variables. Yes, there is a little manual work in terms of setting username and password environment variables on heroku(or less preferably, you can hard code the same in your application). This is in addition to the NEO4J_URL environment variable automatically available after adding neo4j heroku add-on.

<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
    <constructor-arg index="0" value="${NEO4J_URL}" />
    <constructor-arg index="1" value="${NEO4J_USERNAME}" />
    <constructor-arg index="2" value="${NEO4J_PASSWORD}" />
</bean>

Thanks @MichaelHunger!



来源:https://stackoverflow.com/questions/15260994/error-401-unauthorized-for-neo4j-rest-url-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!