处理配置时出现Spring MVC安全性错误:'dataSource'或'jdbcTemplate'是必需的尽管在Application上下文中定义

时间:2017-01-09 21:15:30

标签: java xml spring security jdbctemplate

安全配置

<!-- enable Method level [vs URL level] authorization This work for ROOT 
    WEB APPLICATION Level scanned components Does NOT recognize WEB Application 
    level components - for THAT need to place enable line in Dispatcher-servlet -->
<security:global-method-security
    pre-post-annotations="enabled" />




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


    <!-- if non-standard ports for HTTP and/or HTTPS, you can specify them 
        here -->
    <!-- <security:port-mappings> <security:port-mapping http="9080" https="9443"/> 
        </security:port-mappings> -->
    <!-- We will just use web.xml error for access-denied -->
    <!-- Other Options to redirect exception for web authorization -->
    <!-- <security:access-denied-handler error-page="/error-forbidden" /> <security:access-denied-handler 
        ref="controllerExceptionHandler" /> -->

    <!-- Implemented with Web based URL - Controller "METHOD" level authorization -->
    <!-- the more specific rules need to come first, followed by the more general 
        ones. -->
    <!-- COMMENT out the following to see @Service METHOD level authorization -->
    <!-- <security:intercept-url pattern="/adminHome" access="hasRole('ROLE_ADMIN')" 
        /> <security:intercept-url pattern="/home" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" 
        /> <security:intercept-url pattern="/" access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" 
        /> -->



    <security:form-login login-page="/login"
        login-processing-url="/postLogin" username-parameter="username"
        password-parameter="password" default-target-url="/welcome"
        always-use-default-target="true" authentication-failure-url="/loginfailed" />

    <security:logout logout-success-url="/logout"
        delete-cookies="JSESSIONID" logout-url="/doLogout" />

    <!-- remember me -->
    <!-- <security:remember-me data-source-ref="dataSource" token-validity-seconds="86400" 
        remember-me-parameter="keepMe"/> -->
</security:http>
<security:authentication-manager>
    <security:authentication-provider
        user-service-ref="customUserDetailsService">
        <security:password-encoder hash="bcrypt" />
        <!-- <security:jdbc-user-service data-source-ref="dataSource"/> -->
    </security:authentication-provider>
</security:authentication-manager>


<!-- <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" 
    id="passwordEncoder"/> -->
<bean id="customUserDetailsService"
    class="com.pioneer.infotracker.security.DomainToSecurityUserMapping">
            <!-- <property name="dataSource" ref="dataSource"></property> -->
</bean>


<!-- <bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> 
    <property name="dataSource" ref="dataSource"></property> -->

root-Context配置:

<context:component-scan base-package="com.pioneer.infotracker.*" />
<tx:annotation-driven transaction-manager="transactionManager" />


<!-- <context:component-scan base-package="com.david.*" />
<context:component-scan base-package="com.david.serviceImpl" />
<context:component-scan base-package="com.david.repoImpl" />
<context:component-scan base-package="com.david.domain" /> -->


<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/PioneerSolution?createDatabaseIfNotExist=true" />
    <property name="username" value="root" />
    <property name="password" value="password" />

</bean>



<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.pioneer.infotracker.domain</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

错误是此配置需要在根上下文中定义的dataSource,但com.pioneer.infotracker.security.DomainToSecurityUserMapping类抱怨它要么需要dataSource或JDBC模板。

1 个答案:

答案 0 :(得分:0)

我找到了我添加的解决方案@Autowired public void setDs(DataSource dataSource){setDataSource(dataSource);在Custom USer详细信息中添加了数据源的属性