为了安全起见,我使用了 JDBC 身份验证服务。 身份验证提供者的代码是,
<authentication-provider>
<jdbc-user-service id="userDetailsService" data-source-ref="dataSource" />
</authentication-provider>
对于数据源,
<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>
我还使用了 daoAuthenticationProvider ,代码是,
<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>
而且它工作正常。 现在我想使用 JPA 连接而不是 JDBC。 因此,我创建了新类 CustomUserDetailsService,它实现了 UserDetailsService 接口(interface)。现在我的身份验证提供者看起来像,
<authentication-provider user-service-ref="CustomUserDetailsService">
</authentication-provider>
<beans:bean id="CustomUserDetailsService" class="com.service.CustomUserDetailsService" />
认证管理器代码:
<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>
问题是,现在如何在 daoAuthenticationProvider 的属性 userDetailsService 中给出它的引用? 先感谢您。 (如果需要,我可以提供更多信息)
最佳答案
???只需通过 id 引用新的 UserDetailsService:
<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>
还是我遗漏了什么?
关于java - 使用 JPA 的 Spring 安全性。如何配置applicationContext-security.XML文件?(使用DaoAuthenticationProvider),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4933843/