[JAVA] Implemented authentication function with Spring Security ②

** This is a continuation of Last time. ** **

4. Implementation of login screen.

-This time, we will implement it as a login screen. However, since the authentication function is not implemented, ** If you press a button from the login screen, the screen will change regardless of the contents of the form **.

-The file configuration image is as follows. I wrote the comment as Zura, so please delete it if you don't need it.

SpringBoot_Di_Security_DB_11.png

-The code is as follows.

java:SpringLogin.app.controller/LoginController.java



package SpringLogin.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

//import SpringLogin.app.service.UserService;

@Controller
public class LoginController {

//    @Autowired
//    UserService userService;

    @GetMapping("/login")
    public String getSignUp(Model model) {
        return "login";
    }
    
    //Press Submit on the Form to request the Post method. This method is activated when the request of the URL written in () is received.
    @PostMapping("/login")
    public String postSignUp(Model model) {
    	/*
This time we will use redirects. Use redirect when crossing files such as screen transitions.
You can think of it as calling the Get method.
Normal forward without redirecting to try(return "xxx";)Then the screen itself will be displayed, but the URL will not change.
If you do this, you may not be able to receive the data you want to receive at the transition destination, so it is better to use redirect.
    	*/
        return "redirect:/userList";
    }

}

:templates/login.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"></meta>
</head>
<body>
    <!--Content part-->
    <div>
    	<!-- method="post"You can issue a post request by setting. th:Specify the method to process Post in action by URL.-->
        <form method="post" th:action="@{/login}">
	        <div>
	        	<label>Username:</label>
	        	<!--Although it is not used in this implementation, the name attribute is specified. It will be used later for authentication.-->
	        	<input type="text" name="username" />
	        </div>
	        <div>
	        	<label>Password:</label>
	        	<input type="password" name="password" />
	        </div>

    		<button type="submit">login</button>
        </form>
    </div>
</body>
</html>

・ Once you have written the code, check the app on your browser. ** URL changes ** → http: // localhost: 8080 / login When the page is displayed, press the Login button and confirm that you are transitioning to the userList. Make sure that the URL has changed at this time.

SpringBoot_Di_Security_DB_12.gif

・ It's OK if you can make a transition like a video!

From now on, it will be the main part of this article. The volume is likely to increase here as well, so turn it off once. Continued here.

Recommended Posts

Implemented authentication function with Spring Security ②
Implemented authentication function with Spring Security ③
Implemented authentication function with Spring Security ①
Login function with Spring Security
[Introduction to Spring Boot] Authentication function with Spring Security
Authentication / authorization with Spring Security & Thymeleaf
DB authentication with Spring Security & hashing with BCrypt
Achieve BASIC authentication with Spring Boot + Spring Security
About Spring Security authentication
Try LDAP authentication with Spring Security (Spring Boot) + OpenLDAP
Add your own authentication items with Spring Security
Spring Security usage memo Authentication / authorization
Use Basic Authentication with Spring Boot
Spring Boot Tutorial Using Spring Security Authentication
Implemented mail sending function with rails
Learn Spring Security authentication processing architecture
Oauth2 authentication with Spring Cloud Gateway
A new employee tried to create an authentication / authorization function from scratch with Spring Security
I get a 404 error when testing forms authentication with Spring Security
I implemented an OAuth client with Spring Boot / Security (LINE login)
Set Spring Security authentication result to JSON
Authentication function with Play Framework [Registration and authentication]
Use Spring Security JSP tags with FreeMarker
Implement paging function with Spring Boot + Thymeleaf
How Spring Security works with Hello World
Login function implementation by Spring Security (securityConfig)
Implemented comment function
Hash passwords with Spring Boot + Spring Security (with salt, with stretching)
Try to implement login function with Spring Boot
Create Spring Cloud Config Server with security with Spring Boot 2.0
Create login / logout function with Spring Security according to Spring official guide [For beginners]
Execute arbitrary processing after Basic authentication with Spring boot.
Spring with Kotorin --3. Omitting curly braces from the function
Spring Security usage memo: Cooperation with Spring MVC and Boot
Spring with Kotorin --- 5. Actuator
[Rails] Implemented hashtag function
Spring Boot with Spring Security Filter settings and addictive points
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Spring Security causes 403 forbidden
Handle passwords hashed with BCryptPasswordEncoder in Spring Security in Perl
Create a simple demo site with Spring Security with Spring Boot 2.1
Self-made Validation with Spring
Try to work with Keycloak using Spring Security SAML (Spring 5)
With Kotorin ―― 7. Scoping Function
Call your own method with PreAuthorize in Spring Security
Spring with Kotorin ―― 1. SPRING INITIALIZR
Serverless Function with Micronaut
Download with Spring Boot
Create API key authentication for Web API in Spring Security
With Spring boot, password is hashed and member registration & Spring security is used to implement login function.
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Login with HttpServletRequest # login in Spring Security of Servlet 3.x environment
Generate barcode with Spring Boot
Hello World with Spring Boot
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Introduced graph function with rails
Implement GraphQL with Spring Boot
Java to play with Function
Spring Security usage memo CSRF
Spring with Kotorin --8 Repository layer