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?

David Mališ

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Boot PostMapping Always Receives Null

Spring Boot PostMapping deserialize Long array

Postmapping not getting called when api called from angular to spring boot

PostMapping passing Object with oneToMany relationship | Java spring-boot

Spring Boot RestController - PostMapping - Process Request Body as InputStream

Spring Boot @PostMapping is there a way to detect null fields in a incoming request body?

Spring Boot - perform object and image upload in one @POSTMapping operation

Spring boot login failure

PostMapping in Spring Security

Spring Boot - Hibernate-Validator creates Errors when @PostMapping method is called

API not receiving information with @PostMapping, but with @RequestMapping yes. Spring Boot, Kotlin, REST API

Cannot login with Spring Boot Security

Introduce token login in Spring Boot

Spring Boot Security with Vaadin Login

Custom Login Page for Spring Boot + Spring Security

Spring boot & Spring security always redirect to Login

Spring-Boot Login without Spring Security

Spring Boot project shows the Login page

How to override default Spring Boot Login page?

How to disable login in Spring Boot Netflix' eureka?

Default Login not successful in spring boot 2.1.7

Spring Boot Redirect to requested URL after login

Spring Boot auto login Bad Credentials

Java Spring Boot Login and Role Issues

Decode a password before login Spring Boot

spring boot webflux login with username or email address

Spring boot tries to render login processing url

Spring Boot + REST Api + Social Login

spring-boot security login failed