[JAVA] Login with HttpServletRequest # login in Spring Security of Servlet 3.x environment

As an example of use, there is a case where the user is logged in as it is after the user registration is completed. In this case, the user does not explicitly enter the ID and password in a form or the like, but logs in programmatically.

To achieve this, in Servlet 3.0 and above, set Spring Security and then [HttpServletRequest # login (Java (TM) EE 7 Specification APIs)](http://docs.oracle.com/javaee/7/api/ Call javax / servlet / http / HttpServletRequest.html # login-java.lang.String-java.lang.String-).

@RequestMapping(...
public void index(HttpServletRequest request) {
    try {
        request.login("username", "password");
    } catch (ServletException e) {}
}

As described in Spring Security Reference --15.2.2 HttpServletRequest.login (String, String) , Due to the cooperation function of Spring Security and Servlet API. The mechanism is that when you call the login method, the login process is performed using the currently valid AuthenticationManager on the back side.

Recommended Posts

Login with HttpServletRequest # login in Spring Security of Servlet 3.x environment
SSO with GitHub OAuth in Spring Boot 1.5.x environment
Self-hosting with Docker of AuteMuteUs in Windows environment
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
Handle passwords hashed with BCryptPasswordEncoder in Spring Security in Perl
How to boot by environment with Spring Boot of Maven
Database environment construction with Docker in Spring boot (IntellJ)
SNS login with Spring Boot
I implemented an OAuth client with Spring Boot / Security (LINE login)
Part 4: Customize the behavior of OAuth 2.0 Login supported by Spring Security 5
Part 1: Try using OAuth 2.0 Login supported by Spring Security 5 with Spring Boot
Implemented authentication function with Spring Security ②
Implemented authentication function with Spring Security ③
Java Spring environment in vs Code
Authentication / authorization with Spring Security & Thymeleaf
How to read Body of Request multiple times with Spring Boot + Spring Security
How to execute with commands of normal development language in Docker development environment
Settings when calling API using CSRF measures of Spring Security in JMeter
Alert slack with alert manager in Docker environment
[Processing x Java] Construction of development environment
DB authentication with Spring Security & hashing with BCrypt
Create Spring Boot-gradle-mysql development environment with Docker
Handle system environment variables in Spring application.properties
How Dispatcher servlet works in Spring MVC
Creating a Servlet in the Liberty environment
Use Spring Security JSP tags with FreeMarker
How Spring Security works with Hello World
Use cache with EhCashe 2.x with Spring Boot
Login function implementation by Spring Security (securityConfig)
Achieve BASIC authentication with Spring Boot + Spring Security
Part 2: Understand (roughly) the process flow of OAuth 2.0 Login supported by Spring Security 5
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
Create login / logout function with Spring Security according to Spring official guide [For beginners]
Part 3: Understand (deeply) the process flow of OAuth 2.0 Login supported by Spring Security 5