2016-03-01 86 views
0

我想實現的Spring Security在那裏我得到的錯誤。Spring MVC的休眠Spring Security的錯誤

UserAuthenticationProviderImpl.java

@Component(value = "authenticationProvider") 
public class UserAuthenticationProviderImpl implements 
     UserAuthenticationProvider { 

    UserFunctionsService userFunctionsService; 

    @Autowired(required=true) 
    @Qualifier(value="userFunctionsService") 
    public void setUserFunctionsService(UserFunctionsService userFunctionsService) 
    { 
     this.userFunctionsService = userFunctionsService; 
    } 

    /* (non-Javadoc) 
    * @see com.pir.authentication.UserAuthenticationProvider#authenticate(org.springframework.security.core.Authentication) 
    */ 
    @Override 
    public Authentication authenticate(Authentication authentication) { 
     // TODO Auto-generated method stub 

     Users users = (Users) this.userFunctionsService.getUserDetails(authentication.getPrincipal().toString()); 

     if(users == null) 
      throw new UsernameNotFoundException(String.format("Invalid credentials", authentication.getPrincipal())); 

     String suppliedPasswordHash = authentication.getCredentials().toString(); 

     if(!users.getPassword().equals(suppliedPasswordHash)){ 
      throw new BadCredentialsException("Invalid credentials"); 
     } 

     UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(users, users.getAuthorities()); 

     return token; 
    } 

} 

Users.java

@Entity 
@Table(name = "users") 
public class Users { 

    @NotNull 
    @Id 
    @Column(name = "userID") 
    private int userID; 

    @NotNull 
    @Size(min=3, max=50) 
    @Pattern(regexp = "[email protected]+\\.[a-z]+") 
    @Column(name = "emailID") 
    private String emailID; 

    @NotNull 
    @Size(min=3, max=50) 
    @Column(name = "firstName") 
    private String firstName; 

    @NotNull 
    @Size(min=3, max=50) 
    @Column(name = "lastName") 
    private String lastName; 

    @NotNull 
    @Size(min=3, max=50) 
    @Column(name = "password") 
    private String password; 

    @NotNull 
    @Column(name = "mobileNo") 
    private String mobileNo; 


    @Column(name = "imageURL") 
    private String imageURL; 

    @NotNull 
    @DateTimeFormat(pattern="MM/dd/yyyy") 
    @Column(name = "dateOfBirth") 
    private String dateOfBirth; 

    @NotNull 
    @Column(name = "gender") 
    private String gender; 

    @NotNull 
    @Column(name = "userType") 
    private String userType; 

    public Collection<? extends GrantedAuthority> getAuthorities() 
    { 
     Set<UserAuthority> authority = new HashSet<UserAuthority>(); 

     return authority; 
    } 
} 

UserAuthority

public class UserAuthority implements GrantedAuthority { 

    private String authority; 

    public UserAuthority(String authority) 
    { 
     this.authority = authority; 
    } 
    /* (non-Javadoc) 
    * @see org.springframework.security.core.GrantedAuthority#getAuthority() 
    */ 

    @Override 
    public String getAuthority() { 
     // TODO Auto-generated method stub 
     return authority; 
    } 

} 

錯誤

java.lang.IllegalStateException: Cannot convert value of type [com.pir.authentication.UserAuthenticationProviderImpl] to required type [org.springframework.security.core.userdetails.UserDetailsService] for property 'userDetailsService': no matching editors or conversion strategy found 
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287) 
    org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:461) 
    org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:512) 
    org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:506) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1523) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1482) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157) 
    org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) 
    org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.security.config.authentication.AuthenticationManagerFactoryBean.getObject(AuthenticationManagerFactoryBean.java:34) 
    org.springframework.security.config.authentication.AuthenticationManagerFactoryBean.getObject(AuthenticationManagerFactoryBean.java:25) 
    org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) 
    org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1523) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:314) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) 
    org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157) 
    org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) 
    org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382) 
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736) 
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:630) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:678) 
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:549) 
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490) 
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 
    javax.servlet.GenericServlet.init(GenericServlet.java:160) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    java.lang.Thread.run(Thread.java:744) 

WEL-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security.xsd" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"> 

    <context:annotation-config /> 

    <context:component-scan base-package="com.pir" /> 
    <mvc:annotation-driven /> 
    <tx:annotation-driven transaction-manager="myTransactionManager" /> 

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

<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="com.mysql.jdbc.Driver" 
    p:url="jdbc:mysql://localhost:3306/pir" 
    p:username="root" 
    p:password="user" /> 


    <!-- enable use-expressions --> 
    <security:http auto-config="true" use-expressions="true"> 
     <security:intercept-url pattern="/admin**" access="hasRole('ROLE_admin')" /> 

     <!-- access denied page --> 
     <security:access-denied-handler error-page="/403" /> 
     <security:form-login 
      login-page="/login" 
      login-processing-url="/j_spring_security_check" 
      default-target-url="/index" 
      authentication-failure-url="/login?error" 
      username-parameter="username" 
      password-parameter="password" /> 
     <security:logout logout-success-url="/login?logout" /> 
     <!-- enable csrf protection --> 
     <security:csrf /> 
    </security:http> 

    <bean id="userAuthenticationProviderImpl" class="com.pir.authentication.UserAuthenticationProviderImpl" /> 

    <security:authentication-manager> 
     <security:authentication-provider user-service-ref="userAuthenticationProviderImpl" > 
      <security:password-encoder hash="plaintext" />  
     </security:authentication-provider> 
    </security:authentication-manager> 
</beans> 

如何解決這個問題?

回答

1

authentication-provider user-service-ref引用bean必須實現org.springframework.security.core.userdetails.UserDetailsService。該接口只包含一個方法

UserDetails loadUserByUsername(java.lang.String username) 

UserAuthenticationProviderImpl應該只是實現該接口,它應該工作。

1

如果您想編寫自己的自定義AuthenticationProvider,則需要實現AuthenticationProvider接口或擴展AbstractUserDetailsAuthenticationProvider類。你能發佈UserAuthenticationProvider的代碼嗎?

+0

'UserAuthenticationProvider'只是與沒有身體authenticate方法的接口。 –

+0

而不是UserAuthenticationProvider爲customauthencation提供者實現spring AuthenticationProvider – Arker

+0

它包含'supports'方法。你能告訴我一個教程,因爲我是春季安全新手,所以如何做到這一點。 –