Spring using JPA. How to configure applicationContext-security.XML file? (Using DaoAuthenticationProvider)

I used the JDBC authentication service for my security. authentication provider code

<authentication-provider> <jdbc-user-service id="userDetailsService" data-source-ref="dataSource" /> </authentication-provider> 

And for the data source

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/demodata" /> <property name="username" value="root"/> <property name="password" value="root"/> </bean> 

I also used daoAuthenticationProvider , whose code is

 <beans:bean id="daoAuthenticationProvider" class="org.springframework.security.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService"/> <property name="saltSource" ref bean="saltSource"/> <property name="passwordEncoder" ref="passwordEncoder"/> </beans:bean> 

And it worked correctly. Now I want to use a JPA connection instead of JDBC . So, I created a new CustomUserDetailsService class that implements the UserDetailsService interface . Now my authentication provider looks like

 <authentication-provider user-service-ref="CustomUserDetailsService"> </authentication-provider> <beans:bean id="CustomUserDetailsService" class="com.service.CustomUserDetailsService" /> 

Authentication Manager Code:

 <beans:bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager"> <beans:property name="providers"><beans:list> <beans:ref local="daoAuthenticationProvider" /> </beans:list> </beans:property> <beans:property name="sessionController" ref="defaultConcurrentSessionController" /> </beans:bean> 

The problem is that now, how can I give a link in the daoAuthenticationProvider userDetailsService property ? Thank you in advance. (If necessary, I can provide additional information)

+4
source share
2 answers

??? Just specify the new UserDetailsService by id:

 <beans:bean id="daoAuthenticationProvider" class="org.springframework.security.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="CustomUserDetailsService"/> <property name="saltSource" ref bean="saltSource"/> <property name="passwordEncoder" ref="passwordEncoder"/> </beans:bean> 

Or am I missing something?

+4
source

Instead

 <authentication-provider user-service-ref="CustomUserDetailsService"> </authentication-provider> <beans:bean id="CustomUserDetailsService" class="com.service.CustomUserDetailsService" /> 

Can you try the following suggested here ?

 <beans:bean id="CustomUserDetailsService" class="com.service.CustomUserDetailsService"> <custom-authentication-provider/> </beans> 
+2
source

Source: https://habr.com/ru/post/1338855/


All Articles