使用spring security 3.1.6登录并且hibernate无效

时间:2014-05-07 04:26:51

标签: spring hibernate spring-mvc spring-security

我是Spring的新手,我正在使用hibernate和spring security 3.1.6开发一个hello world应用程序,但问题是身份验证提供程序没有调用我的用户服务的实现。我知道这是因为把方法放在它的主体的开头写一个文件,但没有任何反应。

这是我的web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
         /WEB-INF/applicationContext.xml
            /WEB-INF/dispatcher-servlet.xml
            /WEB-INF/conexion-servlet.xml
            /WEB-INF/security-servlet.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>

    </servlet-mapping>
   <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我的调度员

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

   <context:component-scan base-package="scul">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


          <bean id="usuarioDao" class="scul.aplication.DAOS.Usuario.UsuarioOperacionesImpl"/>



</beans>

我的applicationcontext

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

 <mvc:annotation-driven />
<context:component-scan base-package="scul">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

     <!-- Activates annotation based transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

我的安全servlet

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http use-expressions="true" auto-config="true" >
<intercept-url pattern="/login.htm" access="permitAll"/>
<intercept-url pattern="/loginerror.htm" access="permitAll"/>
<!--<intercept-url pattern="/index.htm" access="permitAll"/>-->
<intercept-url pattern="/index.htm" access="hasRole('ROLE_USER')" />

<form-login login-page="/login.htm" authentication-failure-url="/loginerror.htm" default-target-url="/loginerror.htm"
            always-use-default-target='true'/>

<!--<remember-me user-service-ref="CustomUserDetailsService" key="test"/>

<logout invalidate-session="true" logout-success-url="/login.htm" logout-url="/j_spring_security_logout"/>
-->


</http>
<authentication-manager alias="authenticationManager" >
    <authentication-provider user-service-ref="CustomUserDetailsService">


 </authentication-provider>
</authentication-manager>

<beans:bean id="CustomUserDetailsService" class="scul.aplication.Service.Usuario.CustomUserDetailsService">

</beans:bean>

我的conexion servlet

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <bean id="pool" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/control"/>
        <property name="username" value="postgres"/>
        <property name="password" value="postgres"/>

    </bean>

    <bean name="myfactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >

        <property name="dataSource" ref="pool" />

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
            </props>
        </property>
        <property name="packagesToScan" value="scul.aplication.Entities.Usuario"/>
    </bean>



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




    <!-- Captura las excepciones del ORM y las traduce a excepciones Spring  -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

</beans>

我的CustomUserDetailsS​​ervice

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
public class CustomUserDetailsService implements UserDetailsService{
    @Autowired
    @Qualifier(value = "usuarioDao")
    UsuarioOperaciones user;


    @Override   
    public UserDetails loadUserByUsername(String string) throws     UsernameNotFoundException {
       Usuario usuario=user.findByUserName(string);

        if(usuario==null)
            throw new UsernameNotFoundException("user not found");

       GrantedAuthority[] authorities=new GrantedAuthority[1];
          authorities[0]=new UserGrantedAuthority("ROLE_USER");




       return new CustomUser(usuario.getUsername(), usuario.getPassword(),authorities);

    }



}

我的UsarioDAo(UsuarioOperaciones)

@Repository
 @Transactional   
public class UsuarioOperacionesImpl implements UsuarioOperaciones{

    @Autowired

    SessionFactory session;




    @Override
    public Usuario findByUserName(String Username) {
       Usuario user=  (Usuario) session.getCurrentSession().createQuery("SELECT u FROM Usuario u WHERE u.username=:user").setParameter("username", Username).uniqueResult();
    return user;
    }

    @Override
    public boolean SaveOrUpdateUser(Usuario user) {
        session.getCurrentSession().save(user);

        return true;
    }

    @Override
    public boolean DeleteUser(Usuario user) {
        session.getCurrentSession().delete(user);
        return true;
    }

}

0 个答案:

没有答案