Spring/Spring Boot

name, password 구현

hoonssss 2023. 9. 21. 03:12
반응형
SMALL
package com.in28minutes.springboot.myfirstwebapp.login;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import ch.qos.logback.core.model.Model;

@Controller
public class LoginController {

	private AuthenticationService authenticationService;
	
	public LoginController(AuthenticationService authenticationService) {
		super();
		this.authenticationService = authenticationService;
	}

	@RequestMapping(value="login", method=RequestMethod.GET)
	public String gotoLoginPage() {
		return "login";
	}
	
	@RequestMapping(value="login", method=RequestMethod.POST)
	public String gotoWelcomePage(@RequestParam String name, @RequestParam String password, ModelMap model) {
		
		if(authenticationService.authenticate(name, password)) {
		
		model.put("name", name);
		model.put("password", password);
	
		return "welcome";
		}
		
		model.put("error", "Invalid PLZ try again.");
		return "login";
	}
}
package com.in28minutes.springboot.myfirstwebapp.login;

import org.springframework.stereotype.Service;

@Service
public class AuthenticationService {
	
	public boolean authenticate(String username, String password) {
		
		boolean isValidUserName = username.equalsIgnoreCase("JH");
		boolean isValidPassWord = password.equalsIgnoreCase("JH");
		
		return isValidUserName && isValidPassWord;
	}

}
#login.jsp
<html>
	<head>
		<title> Login Page</title>
	</head>
	<body>
		
		Welcome to the login page!!
		
		<pre>${error}</pre>
		
		<form method="post">
			name: <input type="text" name = "name">
			password: <input type="text" name = "password">
			<input type = "submit">
		</form>
		
	</body>
</html>
#welcome.jsp
<html>
	<head>
		<title> Welcome Page</title>
	</head>
	<body>
		<div>Welcome to in 28minutes</div>
		<div>Your name : ${name}</div>
		<div>Your password : ${password}</div>
	</body>
</html>

첫화면
로그인실패
로그인 성공시 welcome

 

반응형
LIST

'Spring > Spring Boot' 카테고리의 다른 글

Rest API 동적 필터링  (0) 2023.10.06
REST API 버전 관리 (params, headers, produces)  (0) 2023.10.05
logging  (0) 2023.09.21
@RequestParam  (0) 2023.09.21
JSP Login view  (0) 2023.09.20