java - 使用 JPA 的 Spring 安全性。如何配置applicationContext-security.XML文件?(使用DaoAuthenticationProvider)

标签 java spring jpa spring-mvc spring-security

为了安全起见,我使用了 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。 因此,我创建了新类 CustomUserDetailsS​​ervice,它实现了 UserDetailsS​​ervice 接口(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 的属性 userDetailsS​​ervice 中给出它的引用? 先感谢您。 (如果需要,我可以提供更多信息)

最佳答案

???只需通过 id 引用新的 UserDetailsS​​ervice:

<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/

相关文章:

java - 使用数组调用方法时出现问题

java - 具有相同名称的函数接收不同派生类的实例

java - Java 中的可变范围效率

java - Spring 数据 jpa 分离实体

java - 在 JPA Criteria API 查询中使用 countDistinct 的示例

java - 使用 Spring 和 Hibernate 时是否需要 persistence.xml?

java - 我如何对我的数组列表进行排序?

java - 需要验证使用 mvc :annotation-driven? 定义处理程序映射的旧方法

java - 如何在 getJdbcTemplate().query 中将 postgresql 数组转换为 java 数组

java - @Inject 在 @aspect 上为空