Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Spring Security 2 database authentication with Hibernate 3 Annotated Classes?

I'm building an app using Hibernate 3 (with JPA Annotations), Spring 2.5 and Spring Security 2.0.5.

I want to know what I need to put in my <authentication-provider> tag in my spring security config file (applicationContext-security.xml) so that I can get Spring Security to use my existing Service layer class (AuthenticationService) which deals with my custom User and Role domain objects.

I understand that Spring Security requires two tables to be present with the following schema:

     create table users(
      username varchar_ignorecase(50) not null primary key,
      password varchar_ignorecase(50) not null,
      enabled boolean not null);

  create table authorities (
      username varchar_ignorecase(50) not null,
      authority varchar_ignorecase(50) not null,
      constraint fk_authorities_users foreign key(username) references users(username));

  create unique index ix_auth_username on authorities (username,authority);;

but I want to use my own domain objects which are different to the above table definitions.

Could someone please point me in the right direction here? I can't find any useful documentation and I'm not sure whether what I want to do is actually possible.

Thanks!

like image 762
JMM Avatar asked Sep 16 '25 02:09

JMM


1 Answers

You can implement a custom UserDetailsService as a bridge between your domain and Spring Security. Then you supply Spring Security with it as follows (for Spring Security 2.x):

<security:authentication-provider user-service-ref='myUserDetailsService'/>

<bean id="myUserDetailsService" class="... your implementation ...">
    ...
</bean>
like image 106
axtavt Avatar answered Sep 17 '25 17:09

axtavt