Neo4jTemplate error when using standalone mode - neo4j

In my project I'm using Spring 3 with Neo4j 2.0.1, I was using the embedded mode and everything was working fine.
In my code I use the Neo4jTemplate (#Autowired in my services) as well as the GraphRepository and RelationshipOperationsRepository.
I wanted to migrate my coode to use the standalone mode. Here's what I did :
In spring-context.xml, I made it like this :
<!-- Neo4J -->
<!-- neo4j:config storeDirectory="${neo.storeDir}" base-package="net.nextep.nextenergy.domain.graph" /-->
<bean id="graphDatabase" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg value="http://localhost:7474/db/data/" index="0"/>
</bean>
<neo4j:repositories base-package="net.nextep.nextenergy.repository.graph" />
I tried to run it, I got this error :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'neo4jTemplate' is defined
I tried then to remove the #Autowired Neo4jTemplate from my services, use #Autowired private SpringRestGraphDatabase graphDB and create a new Template this way private Neo4jTemplate template = new Neo4jTemplate((GraphDatabase) graphDB)
And I still get the same error :
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userNodeRepository': Cannot resolve reference to bean 'neo4jTemplate' while setting bean property 'neo4jTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'neo4jTemplate' is defined
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'neo4jTemplate' is defined
...
The UserNodeRepository :
#Repository
public interface UserNodeRepository extends GraphRepository<UserNode>,
RelationshipOperationsRepository<UserNode>{
public UserNode findById(String id);
public Node findNodeById(String id);
#Query("match (u:UserNode)-[:HABILITATED]->(f:Folder) where ID(u) = {0} return f")
public Set<Folder> getAllowedFoldersFirstLevel(UserNode user);
}
Am I doing wrong?

Name your bean "graphDatabaseService" and pass it to the config which you didn't create:
<neo4j:config graphDatabaseService="graphDatabaseService" base-package="net.nextep.nextenergy.domain.graph" />
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg value="http://localhost:7474/db/data/" index="0"/>
</bean>
<neo4j:repositories base-package="net.nextep.nextenergy.repository.graph" />

Related

Spring Social : Unable to create SocialAuthenticationFilter using xml configuration

I want to add spring social facebook (spring social 1.1.4 and spring social facebook 1.1.1) with spring security 4.0.3 using XML configuration,
I'm unable to add SocialAuthenticationFilter :
<b:bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter">
<b:constructor-arg index="0" ref="authenticationManager" />
<b:constructor-arg index="1" ref="userIdSource" />
<b:constructor-arg index="2" ref="usersConnectionRepository" />
<b:constructor-arg index="3" ref="socialAuthenticationServiceLocator" />
</b:bean>
the execution fails due to this error message :
GRAVE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#1' while setting bean property 'sourceList' with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#1': Cannot resolve reference to bean 'socialAuthenticationFilter' while setting constructor argument with key [4]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'socialAuthenticationFilter' defined in ServletContext resource [/WEB-INF/spring/security-context.xml]: Cannot resolve reference to bean 'socialAuthenticationServiceLocator' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'socialAuthenticationServiceLocator' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382)
You need a proper socialAuthenticationServiceLocator defined in your context.
<bean id="socialAuthenticationServiceLocator" class="org.springframework.social.security.SocialAuthenticationServiceRegistry">
<property name="authenticationServices">
<list>
<bean class="org.springframework.social.twitter.security.TwitterAuthenticationService">
<constructor-arg value="${twitter.apiKey}" />
<constructor-arg value="${twitter.appSecret}" />
</bean>
</list>
</property>
</bean>

custom exception mapping in spring security xml

I am giving below snippet in spring security.xml
<!-- Exception Mapping configuration -->
<bean id="securityExceptionTranslationHandler"
class="com.abc.xyz.ExceptionMappingFailureHandler" >
<property name="exceptionMappings">
<props>
<prop key="org.springframework.security.authentication.CredentialsExpiredException">/408</prop>
</props>
</property>
</bean>
I am getting error while starting tomcat itself saying :
Invalid property exceptionMappings of com.abc.xyz.ExceptionMappingFailureHandler class :Bean Property exceptionMappings is not writtable or has an invalid setter method .
What should be content of com.abc.xyz.ExceptionMappingFailureHandler class ?
Any Help would be appreciated !
Your class should look something like this to work:
package com.abc.xyz;
public class ExceptionMappingFailureHandler {
public void setExceptionMappings(Map mappings) {
...
}
}
The important thing is that there is a setter with that name, it returns void, and accepts a single argument of type Map.

org.springframework.data.mapping.model.MappingException: Unknown persistent entity java.lang.String

I've upgraded from Spring-data-Neo4j 3.0.0 to 3.0.1 to use the new Label feature.
The derived finders (working with previous versions) are giving this exception :
org.springframework.data.mapping.model.MappingException: Unknown persistent entity java.lang.String
I can't find why does this happen :(
I've tried to add #Query("MATCH (e:Event) WHERE e.title = {0} RETURN e") over my repository finder but I'm getting the same exception
I think it's a Mongodb-Neo4j conflict because when I debug I see that in the invoke method of the JdkDynamicAopProxy, the proxy parameter is org.springframework.data.mongodb.repository.support.SimpleMongoRepository#3c8f3375 while My my entity is annotated with #NodeEntity and in the context configuration I have this :
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com.xxx"/>
<!-- Neo4J -->
<neo4j:config storeDirectory="D:/neo4j-enterprise-2.0.1/data/mydb" base-package="com.xxx.domain.graph" />
<neo4j:repositories base-package="com.xxx.repository.graph" />
<!-- MongoDB -->
<!-- Factory bean that creates the Mongo instance -->
<mongo:mongo host="localhost" port="27017">
<mongo:options
connections-per-host="5"
connect-timeout="30000"
max-wait-time="10000"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<mongo:db-factory dbname="test" mongo-ref="mongo"/>
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="xxxx"/>
</bean>
<mongo:repositories base-package="com.xxx.repository.document" />
<tx:annotation-driven mode="proxy"/>
In case it helps, I had to upgrade spring-data-commons to 1.7.1
It works fine now!

UserDetailsService bean missing since Spring Security migration from XML to JavaConfig

I use Spring Security 3.2.0.RELEASE.
I am currently migrating my application's XML-based Spring Security configuration to a JavaConfig-based Spring Security configuration.
In the old security.xml, I configured a JDBC-based UserDetailsService like this:
<authentication-manager alias="authenticationManager">
<authentication-provider>
<password-encoder ref="passwordEncoder" />
<!-- parsed by JdbcUserServiceBeanDefinitionParser -->
<!-- the following creates a JdbcUserDetailsManager -->
<jdbc-user-service data-source-ref="dataSource" />
</authentication-provider>
</authentication-manager>
Among other effects, above XML configuration automatically registered a Spring Bean of type JdbcUserDetailsManager (a subtype of UserDetailsService) that I could inject into other components like so:
#Service
public class MyCustomService {
#Inject
private JdbcUserDetailsManager judm;
//...
}
My JavaConfig-based adaption of above XML looks like this:
#Configuration
// #ImportResource("classpath:to/no/longer/needed/security.xml")
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.passwordEncoder( somePasswordEncoder )
.dataSource( someDataSource );
}
// ...
}
Above JavaConfig configuration does not seem to additionally register a Spring Bean of type JdbcUserDetailsManager. This means that MyCustomService does not get injected with such a bean, and therefore I get a NoSuchBeanDefinitionException:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.provisioning.JdbcUserDetailsManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#javax.inject.Inject()}
Using the JavaConfig approach, how can I get hold of a bean of type JdbcUserDetailsManager?
have you tried to add
#Bean
public JdbcUserDetailsManager jdbcUserDetailsManager(){
return new JdbcUserDetailsManager();
}
to your SecurityConfig class?

LDAP Authentication with Spring Security 3

I'm trying to secure parts of my Spring 3 MVC web application by authenticating against my organization's LDAP server. I'm new to LDAP so I'm learning as I go. I've been following the documentation here and the example here but I can't seem to get it right.
Here is my security-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:s="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Security Configuration -->
<s:http>
<s:intercept-url pattern="/page/tosecure/*" access="ROLE_USER" />
<s:http-basic />
</s:http>
<s:ldap-server root="dc=ldap,dc=sub,dc=myorg,dc=org" url="ldap.sub.myorg.org" port="636" />
<s:authentication-manager>
<s:ldap-authentication-provider user-dn-pattern="uid={0},cn=users" />
<s:authentication-provider ref="ldapAuthProvider" />
</s:authentication-manager>
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
<constructor-arg value="ldaps://ldap.sub.myorg.org:636/dc=ldap,dc=sub,dc=myorg,dc=org" />
</bean>
<bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
<constructor-arg>
<bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
<constructor-arg ref="contextSource" />
<property name="userDnPatterns">
<list>
<value>uid={0},cn=users</value>
</list>
</property>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<constructor-arg ref="contextSource" />
<constructor-arg value="cn=groups" />
<property name="groupRoleAttribute" value="cn" />
</bean>
</constructor-arg>
</bean>
</beans>
And here is the error I am getting (the last few causes listed in the stack trace)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.securityContextSource': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.ldap.DefaultSpringSecurityContextSource]: Constructor threw exception; nested exception is org.springframework.ldap.BadLdapGrammarException: Failed to parse DN; nested exception is org.springframework.ldap.core.TokenMgrError: Lexical error at line 1, column 5. Encountered: "." (46), after : ""
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
... 106 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.ldap.DefaultSpringSecurityContextSource]: Constructor threw exception; nested exception is org.springframework.ldap.BadLdapGrammarException: Failed to parse DN; nested exception is org.springframework.ldap.core.TokenMgrError: Lexical error at line 1, column 5. Encountered: "." (46), after : ""
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
... 115 more
Caused by: org.springframework.ldap.BadLdapGrammarException: Failed to parse DN; nested exception is org.springframework.ldap.core.TokenMgrError: Lexical error at line 1, column 5. Encountered: "." (46), after : ""
at org.springframework.ldap.core.DistinguishedName.parse(DistinguishedName.java:224)
at org.springframework.ldap.core.DistinguishedName.<init>(DistinguishedName.java:174)
at org.springframework.ldap.core.support.AbstractContextSource.setBase(AbstractContextSource.java:207)
at org.springframework.security.ldap.DefaultSpringSecurityContextSource.<init>(DefaultSpringSecurityContextSource.java:67)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 117 more
Caused by: org.springframework.ldap.core.TokenMgrError: Lexical error at line 1, column 5. Encountered: "." (46), after : ""
at org.springframework.ldap.core.DnParserImplTokenManager.getNextToken(DnParserImplTokenManager.java:678)
at org.springframework.ldap.core.DnParserImpl.jj_consume_token(DnParserImpl.java:231)
at org.springframework.ldap.core.DnParserImpl.SpacedEquals(DnParserImpl.java:114)
at org.springframework.ldap.core.DnParserImpl.attributeTypeAndValue(DnParserImpl.java:94)
at org.springframework.ldap.core.DnParserImpl.rdn(DnParserImpl.java:58)
at org.springframework.ldap.core.DnParserImpl.dn(DnParserImpl.java:23)
at org.springframework.ldap.core.DistinguishedName.parse(DistinguishedName.java:218)
It appears that it doesn't like the URL that is listed in the the constructor-arg for the contextSource bean although I'm not sure why.
Also, I have a suspicion that other parts of this configuration are incorrect. For instance, I have the ldap server URL defined in the ldap-server tag and in the contextSource bean. That seems like unneeded duplication but it's how it is done in the examples. Could someone take a good look at the configuration to make sure it is sane?
In addition, in case it's necessary, I'll talk a little about our LDAP server layout since it seems to be a little non-standard. A user's DN is constructed by uid={the_user_name},cn=users,dc=ldap,dc=sub,dc=myorg,dc=org. Group DNs are cn={group_name},cn=groups,dc=ldap,dc=sub,dc=myorg,dc=org and the members of a group are defined by a memberUid attribute. I say this is non-standard because, from what I've read, groups should be defined by an ou instead. But hopefully spring security can handle this setup. Does this configuration properly fetch the roles (groups) a user belongs to?
Have you tried removing the ldap-server element? You shouldn't need it and you haven't configured it with a proper URL (it should probably start with ldap:// or ldaps://).
The example you've linked to uses an embedded server and is illustrating both namespace and bean configurations for the same thing.
The group attribute defaults to cn, so that should be correct for your setup. The Javadoc for DefaultLdapAuthoritiesPopulator gives quite a good description of how it works.

Resources