Spring自定义验证器

时间:2014-05-28 18:33:34

标签: spring spring-security

我使用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() {
...
  }
}

任何想法?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

删除@Service注释,如果你有一个组件扫描(我假设),这个bean将被选中。随着JdbcDaoImpl的扩展,JdbcDaoSupport扩展DataSource,则需要JdbcTemplate<bean class="com.test.UserDetailsServiceImpl" id="authService"> <property name="passwordEncoder" ref="passwordEncoder"/> </bean> 。但是,默认情况下无法自动装配。

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

此外,您的bean定义缺少对数据源的引用。

authService

您的<sec:jdbc-user-service data-source-ref="dataSource"/>并没有做太多,因为您还有<sec:authentication-provider ref="customAuthenticationProvider" />构建Spring Security的默认实例。删除该元素并对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为您完成的{{1}}。

相关问题