【问题标题】:Spring custom authenticatorSpring 自定义身份验证器
【发布时间】:2014-05-28 18:33:33
【问题描述】:

我使用 spring 3.2、spring security 3.1 和自定义身份验证器。

我的 security-context.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:sec="http://www.springframework.org/schema/security"
         xsi:schemaLocation="
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.1.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<sec:http use-expressions="true">

    <sec:form-login
        login-page="/login.html"
        default-target-url="/index.html"
        always-use-default-target='true'
        authentication-failure-url="/login-error.html"
    />
    <sec:logout
        logout-url="/j_spring_security_logout"
        invalidate-session="true"
        logout-success-url="/login.html"
    />

    <sec:intercept-url pattern="/**" access="hasRole('ROLE_USER')" requires-channel="any"/>

    <sec:http-basic />

</sec:http>

<sec:authentication-manager>
    <sec:jdbc-user-service data-source-ref="dataSource"/>
    <sec:authentication-provider ref="customAuthenticationProvider" />
</sec:authentication-manager>

<bean class="org.springframework.security.authentication.dao.DaoAuthenticationProvider" id="daoAuthenticationProvider">
    <property name="userDetailsService" ref="authService"/>
    <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>

<bean class="com.test.UserDetailsServiceImpl" id="authService"> 
    <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>

<bean class="com.test.services.util.Encoder" id="passwordEncoder"/>

我得到这个错误:

 Error creating bean with name 'userDetailsService' defined in class path resource [config/security-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required

我的实现类

@Service("userDetailsService")
public class UserDetailsServiceImpl extends JdbcDaoImpl implements UserDetailsService {

    public UserDetails loadUserByUsername(String username) throws  UsernameNotFoundException, DataAccessException {
...
    }

}

我也有,但是如果我们检查我之前的错误,似乎看不到这段代码。

@Configuration
@EnableTransactionManagement
public class DBConfiguration {

  @Bean(name = "dataSource")
  public BasicDataSource dataSource() {
...
  }
}

有什么想法吗?

【问题讨论】:

  • UserDetailsServiceImpl扩展了什么类?
  • 她扩展了 JdbcDaoImpl
  • 为什么你在那个类上有@Service("userDetailsService"),而且还要在你的xml配置中用id="authService"创建它?
  • 我相信您需要稍微清理一下您的代码,或者更详细地解释您正在尝试做什么。你有一个UserDetailsServiceImpl,但你也在定义jdbc-user-service。这些在我看来是多余的。
  • 我试过没有 得到同样的错误

标签: spring spring-security


【解决方案1】:

JdbcDaoImpl 用于使用 jdbc 查询从数据库中检索用户详细信息的功能。假设一个默认的数据库模式,有两个表“users”和“authorities”,因此它需要一个数据源或 jdbc 模板(这个先决条件来自 JdbcDaoImpl 从 JdbcDaoSupport 扩展的事实,这显然需要这些)。 要实现您自己的身份验证提供程序,请查看herehere

【讨论】:

【解决方案2】:
@Service("userDetailsService")
public class UserDetailsServiceImpl extends JdbcDaoImpl implements UserDetailsService { ... }

删除@Service 注释,如果你有一个组件扫描(我假设)这个bean 将被拾取。因为它扩展了JdbcDaoImpl,而后者又扩展了JdbcDaoSupport,所以需要DataSourceJdbcTemplate。但是默认情况下这不能自动装配。

<bean class="com.test.UserDetailsServiceImpl" id="authService"> 
    <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>

您的 bean 定义也缺少对数据源的引用。

<bean class="com.test.UserDetailsServiceImpl" id="authService"> 
    <property name="passwordEncoder" ref="passwordEncoder"/>
    <property name="dataSource" ref="dataSource" />
</bean>

您的 authService 并没有做太多事情,因为您还有一个 &lt;sec:jdbc-user-service data-source-ref="dataSource"/&gt;,它构造了 Spring Security 的默认实例。删除该元素并更改 &lt;sec:authentication-provider ref="customAuthenticationProvider" /&gt; 以引用您的 authService

<sec:authentication-provider user-service-ref="authService" />

基本上以下就足够了

<sec:authentication-manager>   
    <sec:authentication-provider user-service-ref="authService">
        <sec:password-encoder ref="passwordEncoder"/>
    </sec:authentication-provider>
</sec:authentication-manager>

<bean class="com.test.UserDetailsServiceImpl" id="authService"> 
    <property name="passwordEncoder" ref="passwordEncoder"/>
    <property name="dataSource" ref="dataSource" />
</bean>

<bean class="com.test.services.util.Encoder" id="passwordEncoder"/>

您不需要 DaoAuthenticationProvider,因为这是 Spring Security 为您完成的。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2014-04-20
    • 2014-12-13
    • 2020-02-14
    • 2016-08-05
    • 2020-12-05
    • 2011-03-13
    • 2015-04-27
    • 2020-04-01
    相关资源
    最近更新 更多