Spring“user”未映射

时间:2015-08-26 02:39:06

标签: java spring hibernate spring-security

完成培训应用程序的第一部分后,我遇到了这个错误:

org.springframework.security.authentication.AuthenticationServiceException: user is not mapped [from user u where u.Login = :login]

点击登录按钮后会抛出。我不明白缺少什么,这是我的LoginDAO和DAOImpl:

UserLoginDAOImpl:

package school.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import school.model.User;

import java.util.ArrayList;
import java.util.List;

@Repository

public class UserLoginDAOImpl implements UserLoginDAO{
     @Autowired
        private SessionFactory sessionFactory;

        private Session openSession() {
            return sessionFactory.getCurrentSession();
        }

        public User getLogin(String Login) {
            List<User> userList = new ArrayList<User>();
            Query query = openSession().createQuery("from user u where u.Login = :login");
            query.setParameter("Login", Login);
            userList = query.list();
            if (userList.size() > 0)
                return userList.get(0);
            else
                return null;
        }
}

UserLoginDAO:

package school.dao;

import school.model.User;

public interface UserLoginDAO {
     public User getLogin(String Login);
}

登录页面:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>

</h:head>
<h:body>
    <div align="right" style="">
        <h:form  id="loginFormId" prependId="false">
            <div id="loginFieldsPnlId">
                <div id="loginFieldUsrContId">
                    <h:outputText id="outTxtUserNameId" value="Username: " name="outTxtUserNameNm"></h:outputText>
                    <h:inputText id="userName" required="true" value="#{UserLoginMB.userName}" requiredMessage="Please enter username"></h:inputText>
                    <h:outputLabel id="outLblUserNameId" for="userName" name="outLblUserNameNm"></h:outputLabel>
                </div>
                <div id="loginFieldPassContId">
                    <h:outputText id="outTxtPasswordId" value="Password: " name="outTxtPasswordNm"></h:outputText>
                    <h:inputSecret id="password"  required="true" value="#{UserLoginMB.password}" requiredMessage="Please enter password" name="inTxtPasswordNm"></h:inputSecret>
                    <h:outputLabel id="outLblPasswordId" for="password" name="outLblPasswordNm"></h:outputLabel>
                </div>
            </div>
            <div id="loginBtnPanelId">
                <h:commandButton id="btnLoginId" value="Login" action="#{UserLoginMB.login}" styleClass="loginPanelBtn"></h:commandButton>
                <h:commandButton id="btnCancelId" value="Cancel" action="#{UserLoginMB.cancel}" styleClass="loginPanelBtn" immediate="true" update="loginFormId"></h:commandButton>
            </div>

        </h:form>
    </div>
    <div>
        <h:messages></h:messages>
    </div>
</h:body>
</html>

Tomcat上的确切错误:

org.springframework.security.authentication.AuthenticationServiceException: user is not mapped [from user u where u.Login = :login]
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:109)
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
    at school.managedBeans.UserLoginBean.login(UserLoginBean.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:245)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:64)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [from user u where u.Login = :login]
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
    at school.dao.UserLoginDAOImpl.getLogin(UserLoginDAOImpl.java:26)
    at school.service.CustomUserLoginService.loadUserByUsername(CustomUserLoginService.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy18.loadUserByUsername(Unknown Source)
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)
    ... 74 more

编辑:添加用户POJO

@Entity
@Table (name="USER")
public class User {
    private int idUser;
    private String NomUser;
    private String PrenUser;
    private String MailUser;
    private String AdressUser;
    private int PhoneUser;
    private Date DateNaissanceUser;
    private int AccountStatus;
    private String Password;
    private String Login;
    private char SexeUser;
    private String ImagePath;
     private int idRole;


     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name="IDUSER", unique = true, nullable = false)
     public int getidUser() {     return idUser;     }
     public void setidUser(int iduser) {      this.idUser = iduser;  }

     @Column(name="NOMUSER", nullable = false)
     public String getNomUser() {     return NomUser;    }
     public void setNomUser(String nomUser) {     this.NomUser = nomUser;    }

     @Column(name="PRENUSER", nullable = false)
     public String getPrenUser() {    return PrenUser;   }
     public void setPrenUser(String prenUser) {   this.PrenUser = prenUser;  }

     @Column(name="MAILUSER", nullable = false)
     public String getMailUser() {    return MailUser;   }
     public void setMailUser(String mailUser) {   this.MailUser = mailUser;  }

     @Column(name="ADRESSUSER", nullable = false)
     public String getAdressUser() {      return AdressUser;     }
     public void setAdressUser(String adressUser) {   this.AdressUser = adressUser;  }

     @Column(name="PHONEUSER", nullable = false)
     public int getPhoneUser() {      return PhoneUser;      }
     public void setPhoneUser(int phoneUser) {    this.PhoneUser = phoneUser;    }

     @Column(name="DATENAISSANCEUSER", nullable = false)
     public Date getDateNaissanceUser() {     return DateNaissanceUser;      }
     public void setDateNaissanceUser(Date dateNaissanceUser) {   this.DateNaissanceUser = dateNaissanceUser;    }

     @Column(name="ACCOUNTSTATUS", nullable = false)
     public int getAccountStatus() {      return AccountStatus;      }
     public void setAccountStatus(int accountStatus) {    this.AccountStatus = accountStatus;    }

     @Column(name="PASSWORD", nullable = false)
     public String getPassword() {    return Password;   }
     public void setPassword(String password) {   this.Password = password;  }

     @Column(name="LOGIN", nullable = false)
     public String getLogin() {   return Login;      }
     public void setLogin(String login) {     this.Login = login;    }

     @Column(name="SEXEUSER", nullable = false)
     public char getSexeUser() {      return SexeUser;   }
     public void setSexeUser(char sexeUser) {     this.SexeUser = sexeUser;  }

     @Column(name="IMAGEPATH", nullable = true)
     public String getImagePath() {   return ImagePath;      }
     public void setImagePath(String imagePath) {     this.ImagePath = imagePath;    }

     @ManyToOne(cascade = CascadeType.ALL)
     @JoinColumn(name="IDROLE", nullable=false)
    public int getidRole() {      return idRole;     }
    public void setidRole(int idRole) {   this.idRole = idRole;  }
     private Roles roles;
    public Roles getRoles() {
        return roles;
    }
    public void setRoles(Roles roles) {
        this.roles = roles;
    }

1 个答案:

答案 0 :(得分:1)

大多数答案和评论都表明了以下内容:

Query query = openSession().createQuery("from User u where u.Login = :login");

我认为您对此的误解还在于您的bean属性未正确映射。

getLogin和setLogin引用属性“login”而不是“Login” - 请考虑将您的属性重新命名为“camelCased”并相应地调整您的setter / getter方法。

我在本地使用hsqldb + hibernate5 + Java8 / JPA进行测试 - 在您的情况下 - 异常最有可能被翻译,但其中的要点就是这个。

  • 查询 - 用户不是用户
  • Where子句 - 属性(根据JavaBeans Specification)应该是从登录方法setLogin和getLogin中“登录”。
  • 将您的属性名称与JavaBeans Specification定义的“camelCase”语义对齐。