I read the "Spring Security 3 database authentication with Hibernate"! But I don't know how I should implementate it into my project!
In which document I have to set the password/username/drivers/url for the database?
I have different column titles like OPERATOR_ID/USR_ID/PASSWORD
OPERATOR_ID should be the login name, USR_ID the role and the password for the login
Please, maybe you could post an example which implements my questions? Maybe for a checkout or a *.war file?
I dont think there is any configuration as such for doing this. You have to implement the UserDetailsService which has only one method loadUserByUsername to load the user and you have to implement the same to load your user information from your database using hibernate.
See here
You would need to configure a JDBCDaoImpl bean which takes a Datasource as a parameter. How you retrieve the Datasource is up to you, you may grab it from the app server or use something like Spring's DriverManagerDatasource Here is some (pseudo) config
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>your.driver.classname</value></property>
<property name="url"><value>yourDatabaseUrl</value></property>
<property name="username"><value>yourUsername</value></property>
<property name="password"><value>yourPassword</value></property>
</bean>
<bean id="dao" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<property name="DataSource" ref="datasource" />
...
</bean>
Related
I'm setting up an application which uses mybatis to map objects to/from the database.
In my mybatis file, I use a typehandler to map one of the objects being sent to the database.
In the typeHandler, I am injecting an attribute using spring #resource.
However, when the typehandler is called, the injected property is always null.
From my research, I have discovered that mybatis sets its configuration before spring loads. That means the bean is cannot be injected into the handler as it is created after.
Does anyone know a solution to this?
Should let spring manage customized type handler, like this:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeHandlers">
<array>
<bean class="com.example.YourCustomTypeHandler">
<!-- inject -->
<property name="property" ref="bean"/>
</bean>
</array>
</property>
</bean>
I am trying to get a Single-signon solution working, which has previously worked with Novell eDirectory as an LDAP-provider. Now, I am trying to get it to work with OpenLDAP. In the signon-process, one of the steps is retrieving the actual user from the LDAP. This is done by Spring-security, but it seems to have a bug.
For getting the user, LdapUserDetailsService#loadUserByUsername is called. This uses a predefined LdapUserSearch to find a user with the given username. In my case, the configuration looks something like this:
base:ou=something,ou=somethingElse,dc=oh,dc=my,dc=god
filter:cn={0}
where {0} is replaced with the actual username. So far this works, and the user is retrieved, in the form of userdata returned by FilterBasedLdapUserSearch#searchForUser. But, the userdata does not separate between dn and base, so it has:
base: (empty)
dn:cn=someUsername,ou=something,ou=somethingElse,dc=oh,dc=my,dc=god
This userdata-object is in turn used to retrieve the authorities for the user, so it the tries to do another search with the base above, and the dn as filter. But, this query fails, since OpenLDAP does not allow any queries with an empty string as base.
I have tried with 3.1.3.RELEASE, 3.1.7.RELEASE and 3.2.7.RELEASE, all have the same issue.
Is this a bug in spring-security? Or am I doing something wrong here?
Managed to fix it, by moving away from the "template" way of defining the ldap user service:
Before:
<sec:ldap-user-service id="ldapUserDetailsService"
server-ref="contextSource"
user-search-filter="${my.ldap.filter}"
user-search-base="${my.ldap.base}"
user-context-mapper-ref="myUserDetailsContextMapper"/>
After:
<bean id="userSearch"
class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
<constructor-arg index="0" value="${my.ldap.base}"/>
<constructor-arg index="1" value="${my.ldap.filter}"/>
<constructor-arg index="2" ref="contextSource"/>
</bean>
<bean id="ldapAuthoritiesPopulator" class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
<constructor-arg ref="contextSource"/>
<constructor-arg value="${my.ldap.usersearch.base}"/>
<property name="groupSearchFilter" value="${my.group.filter}" />
<property name="groupRoleAttribute" value="${my.group.attribute}" />
<property name="searchSubtree" value="true" />
</bean>
<bean id="ldapUserDetailsService" class="org.springframework.security.ldap.userdetails.LdapUserDetailsService">
<constructor-arg ref="userSearch"/>
<constructor-arg ref="ldapAuthoritiesPopulator"/>
</bean>
Don't ask me how or why this works..
I have different classes that extends CrudRepository<T, T> of Spring Data JPA Framework. It implicit uses the global EntityManager bean.
Now I want to create a service that should connect to a different database. Which means I'd have to somehow inject a different em/datasource. But how can I bind a database explicit to spring beans / services?
There are multiple ways of doing it. What we did in one of our project is to define two different packages each for different datasource and defined different beans for them like this:
<beans>
<jpa:repositories base-package="com.abc.repository" transaction-manager-ref="abcTransactionManager" entity-manager-factory-ref="abcEntityManagerFactory"/>
<jpa:repositories base-package="com.def.repository" transaction-manager-ref="defTransactionManager" entity-manager-factory-ref="defEntityManagerFactory"/>
<bean id="abcTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="abcEntityManagerFactory"/>
<qualifier value="abcTransactionManager"></qualifier>
</bean>
<bean id="defTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="defEntityManagerFactory"/>
<qualifier value="defTransactionManager"/>
</bean>
</beans>
Similarly defining corresponding entity manager factory. In this way, whatever repositories are defined in package abc will use abcEntityManager and similarly for def.
The other way I have seen it working is applying transaction with appropriate entity manager like this:
#Transactional("abcTransactionManager")
Hello guys I'm quite new at Web Development and I'm stuck on a problem here. I want my java program to connect to a database using Jdbs connector but instead of hard coding the database variables I want my program to read an xml file where the values are stored in beans. The lines I want to be read are stored in applicationContext.xml file as follows:
<bean id="ObjectMapperFactory" scope="singleton" class="someClass"/>
<bean id="UgcDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="username"><someUser</value></property>
<property name="password"><somePassword</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/dbName?autoReconnect=true</value></property>
<property name="defaultAutoCommit"><value>false</value></property>
<property name="maxActive"><value>20</value></property>
<property name="maxIdle"><value>3</value></property>
</bean>
And after reading it I want these variables to be stored in strings as such:
String url = "jdbc:mysql://localhost:3306/dbName"
String username = "someUser"
String password = "somePassword"
So what I'm looking for is a way to extract those 3 values, any suggestions? Much appreciated!
With Spring you can use the #Value Annotation like so
#Value("#{UgcDataSource.url}")
private String url;
I need a spring datasource like:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="<driver>"/>
<property name="url" value="<url>" />
<property name="username" value="<user>" />
<property name="password" value="<pass>" />
</bean>
I need to obtain driver, url, user, pass from persistence.xml.
Tanks a lot!
Here is my snippet for doign the same, you will obviously have to use your BasicDataSource instead of the ComboPooledDataSource I use. But they are pretty much the same, replace getDriverClass() with driverClassName, apparently.
#Autowired
private ComboPooledDataSource dataSource;
public String myMethod() {
return dataSource.getDriverClass());
}
Do you want to print it, or use it in your application for connecting to the dB?
If later one is the case, then, create a bean for sessionFactory, set hibernateProperties for the same where you can inject datasource as well.
In java code, autowire sessionFactory object (or set it using a setter method) and call getCurrentSession method for the same.
For getting various attributes, use chained getter methods to return datasource and extract all the details.
Let me know if you face any issue or need more details for the same.