简体   繁体   中英

Spring boot @PostMapping for login

I need to send an email every time a user logs into an account, but since SpringBoot is used I don't have a @PostMapping method for /login. What should I do?

From what you wrote, I assume that you want to send email only when user performs a successful login. You could write your implementation of AuthenticationSuccessHandler to send an email when user logs in succesfully.

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
        
@Component
public class MyNotifyingAuthSuccessHandler implements AuthenticationSuccessHandler {
        
  public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws IOException {
        
       //send email (do it asynchronously if you can)
        
       //redirect to your homepage (page where user should land after login)
    }
}

Sending an email sometimes takes a lot of time, so consider doing that async not to block the login operation for too long, if your business requirements allow you to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM