How can I define my custom UserDetailsService
bean in a way that enables my spring mvc web app to use my underlying MySQL database to check authentication for users and passwords?
Here are the specifics:
I am adding security to the spring petclinic
sample as a way of learning about spring security. I am using Java configuration, and have set up a SecurityConfig.java
file which extends WebSecurityConfigurerAdapter
. I am trying to set up JdbcAuthentication
in a way that utilizes a MySQL database managed by the ClinicService
tools that are built into the petclinic sample. I therefore created a CustomUserDetailsService
class which extends UserDetailsService
, and which is intented to link the SecurityConfig.java
with ClinicService.java
. I created a User
class and a Role
class to model the users
and roles
tables in the MySQL database, respectively.
I then added the following line to business-config.xml
to define the CustomUserDetailService
:
<bean class="org.springframework.samples.petclinic.service.CustomUserDetailsService"></bean>
But yet I am still getting the following error stating that the bean for CustomUserDetailService
has not been defined:
Caused by: java.lang.IllegalArgumentException: Can not set
org.springframework.samples.petclinic.service.CustomUserDetailsService field
org.springframework.security.samples.petclinic.config.SecurityConfig.myCustomUserDetailsService
to $Proxy61
To keep this posting concise, I have loaded the relevant backup materials to a file sharing site. You can read all the source code and the complete stack trace by clicking on the following links:
You can read SecurityConfig.java
by clicking on this link.
The code for business-config.xml
is at this link.
The code for CustomUserDetailService.java
is at this link.
The code for the User
entity is at this link.
The code for the Role
entity is at this link.
The complete stack trace can be read at this link.
All of the other code for the application can be found at the github page for the Spring petclinic sample, which you can read by clicking on this link.
Here is a link to the code for login.jsp.