如何检查邮件和密码的Spring Boot API

huangapple go评论55阅读模式
英文:

How to Check Email and Password Spring Boot Api

问题

I am a beginner in the Spring Boot framework. I have successfully completed the Spring Registration process. After completing that, I created a login form API. However, when I tested it using Postman, I encountered an error. I have included the details of what I have tried below. If the email and password are correct, I receive the message "Login Success"; otherwise, I get "Login Fail."

I have also attached the stacktrace below for reference.

LoginController:

@PostMapping(path = "/login")
public String loginEmployee(@RequestBody LoginDTO loginDTO)
{
    String email = employeeService.loginEmployee(loginDTO);
    return email;
}

Entity:

public class Employee {

    @Id
    @Column(name="employee_id", length = 45)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int employeeid;

    @Column(name="employee_name", length = 255)
    private String employeename;

    @Column(name="email", length = 255)
    private String email;

    @Column(name="password", length = 255)
    private String password;

    // Constructors, getters, setters, and toString method
}

LoginDTO:

```java
public class LoginDTO {
    private String email;
    private String password;

    // Constructors, getters, setters, and toString method
}

EmployeeRepo:

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
    public boolean findOneByEmailAndPassword(String email, String password);
}

EmployeeService:

public interface EmployeeService {
    String loginEmployee(LoginDTO loginDTO);
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
    if(employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword())) {
        return "Login Success";
    } else {
        return "Login Fail";
    }
}

Please note that the code you provided contains HTML entities (e.g., &quot; and &amp;). You may need to replace them with their actual characters in your code.

英文:

i am beginner of spring boot framework.i have done the Spring Registation successfully.after done the stuff i made the login form api but i check through the request on postman i got the error was.what i tried so far i attached below. if the email and password correct get the message login success other wise fail

i attached the stacktrace below

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract boolean com.example.Registation.Repo.EmployeeRepo.findOneByEmailAndPassword(java.lang.String,java.lang.String)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:229) ~[spring-aop-5.3.23.jar:5.3.23]
	at com.sun.proxy.$Proxy100.findOneByEmailAndPassword(Unknown Source) ~[na:na]
	at com.example.Registation.Service.impl.EmployeeIMPL.loginEmployee(EmployeeIMPL.java:44) ~[classes/:na]
	at com.example.Registation.EmployeeController.EmployeeController.loginEmployee(EmployeeController.java:30) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:696) ~[tomcat-embed-core-9.0.68.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.23.jar:5.3.23]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:779) ~[tomcat-embed-core-9.0.68.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.23.jar:5.3.23]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.68.jar:9.0.68]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_202]

LoginController

@PostMapping(path = &quot;/login&quot;)
public String loginEmployee(@RequestBody LoginDTO loginDTO)
{
    String email = employeeService.loginEmployee(loginDTO);
    return email;
}

Entity

public class Employee {

    @Id
    @Column(name=&quot;employee_id&quot;, length = 45)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int employeeid;

    @Column(name=&quot;employee_name&quot;, length = 255)
    private String employeename;

    @Column(name=&quot;email&quot;, length = 255)
    private String email;

    @Column(name=&quot;password&quot;, length = 255)
    private String password;


    public Employee() {
    }

    public Employee(int employeeid, String employeename, String email, String password) {
        this.employeeid = employeeid;
        this.employeename = employeename;
        this.email = email;
        this.password = password;
    }


    public int getEmployeeid() {
        return employeeid;
    }

    public void setEmployeeid(int employeeid) {
        this.employeeid = employeeid;
    }

    public String getEmployeename() {
        return employeename;
    }

    public void setEmployeename(String employeename) {
        this.employeename = employeename;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return &quot;Employee{&quot; +
                &quot;employeeid=&quot; + employeeid +
                &quot;, employeename=&#39;&quot; + employeename + &#39;\&#39;&#39; +
                &quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
                &quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }

LoginDTO

public class LoginDTO {
    private String email;
    private String password;

    public LoginDTO() {
    }

    public LoginDTO(String email, String password) {
        this.email = email;
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return &quot;LoginDTO{&quot; +
                &quot;email=&#39;&quot; + email + &#39;\&#39;&#39; +
                &quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
                &#39;}&#39;;
    }
}

EmployeeRepo

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
      public boolean findOneByEmailAndPassword(String email, String password);
      
}

EmployeeService

public interface EmployeeService {
  
    String loginEmployee(LoginDTO loginDTO);
}

EmployeeIMPL

@Override
public String loginEmployee(LoginDTO loginDTO) {
 if(employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(),loginDTO.getPassword()))
 {
     return &quot;Login Success&quot;;
 }
 else
 {
     return &quot;Login Fail&quot;;
 }

}

答案1

得分: 1

如果您想检查行是否存在,您可以使用"existsBy"。这将返回一个布尔值,您可以在if语句中进行检查。

如果您使用"findOne",您会从数据库中获取行(或null)。如果您不需要行的数据,最好使用"exists"。如果需要数据,请将其存储在变量中,并检查它是否为null。

使用"existsBy"的示例:

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
  boolean existsByEmailAndPassword(String email, String password); 
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  if (employeeRepo.existsByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword())) {
    return "Login Success";
  } else {
    return "Login Fail";
  }
}

使用"findOne"的示例:

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
  Employee findOneByEmailAndPassword(String email, String password);
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  Employee employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword());
  if (employee != null) {
    return "Login Success for " + employee;
  } else {
    return "Login Fail";
  }
}

使用"findOne"还可以使用Optionals。

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
  Optional<Employee> findOneByEmailAndPassword(String email, password);
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  Optional<Employee> employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword());
  if (employee.isPresent()) {
    return "Login Success for " + employee.get();
  } else {
    return "Login Fail";
  }
}
英文:

If you want to check if the row exists, you can use "existsBy". This returns a boolean value which you could check in your if.

If you use "findOne" you get the row from the database (or null). If you do not need the data from the row, you better use exists. If you need it - store it in a variable and check if it's null.

Example with existsBy:

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
  boolean existsByEmailAndPassword(String email, String password); 
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  if (employeeRepo.existsByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword())) {
    return &quot;Login Success&quot;;
  } else {
    return &quot;Login Fail&quot;;
  }
}

Example with findBy:

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
  Employee findOneByEmailAndPassword(String email, String password);
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  Employee employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword());
  if (employee != null) {
    return &quot;Login Success for &quot; + employee;
  } else {
    return &quot;Login Fail&quot;;
  }
}

With findOne you could also use Optionals.

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
  Optional&lt;Employee&gt; findOneByEmailAndPassword(String email, String password);
}

EmployeeIMPL:

@Override
public String loginEmployee(LoginDTO loginDTO) {
  Optional&lt;Employee&gt; employee = employeeRepo.findOneByEmailAndPassword(loginDTO.getEmail(), loginDTO.getPassword());
  if (employee.isPresent()) {
    return &quot;Login Success for &quot; + employee.get();
  } else {
    return &quot;Login Fail&quot;;
  }
}

huangapple
  • 本文由 发表于 2023年2月10日 15:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定