1. 처음 페이지에 들어갔을 때 보이는 화면으로 로그인을 제외한 다른 기능 사용이 불가능함.
2. 로그인 페이지(login.html)와 로그인이 성공했을 때 보이는 메인 페이지 구현(index_ok).
3. 로그인과 로그인을 수행하는 SQL문에 대한 소스코드이다.
4. 로그인 수행 시 세션을 name에 저장해 로그인 여부를 체크하는 interceptor를 구현..
소스코드
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>로그인 화면</title>
<link href="/css/login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="login_box">
<div class="login">
<h1> 로그인 </h1>
<form action="/login_ok" method="post">
<input id="id" name="id" type="text" placeholder="아이디를 입력하세요."><br>
<input id="pw" name="pw" type="password" placeholder="비밀번호를 입력하세요."><br>
<button name="login_btn" id="login_btn" type="submit">로그인</button>
</form>
<a href="/members/register"> 회원 가입</a>
<a href="/"> 메인으로</a>
</div>
</div>
</body>
</html>
login.css
*{margin: 0 auto; padding: 30;}
body * {
padding: 0;
margin: 0;
box-sizing: border-box;
line-height: 1;
-webkit-tap-highlight-color: transparent;
font-family: 'Geogrotesque', 'YoonGothic', sans-serif;
font-weight: inherit;
font-size: inherit;
background-image: url('css/background.jpg');
background-size: 100%;
}
a {
text-decoration: none;
font-size: 1rem;
margin-left: 10px;
line-height: 1.2;
letter-spacing: -0.025rem;
color: #111;}
a:visited { text-decoration: none; }
a:hover { text-decoration: none; }
a:focus { text-decoration: none; }
a:hover, a:active { text-decoration: none; color: black;}
h1
{
font-size: 2rem !important;
font-weight: 100 !important;
line-height: 1.2;
letter-spacing: -0.075rem;
text-align: center;
margin-bottom: 20px;
}
.login_box {
width: 35.5rem;
margin: 0 auto;
padding: 50px;
}
input {
writing-mode: horizontal-tb !important;
text-rendering: auto;
color: fieldtext;
letter-spacing: normal;
word-spacing: normal;
line-height: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
appearance: auto;
-webkit-rtl-ordering: logical;
cursor: text;
background-color: field;
margin: 5px;
padding: 1px 2px;
border-width: 2px;
border-style: inset;
border-color: -internal-light-dark(rgb(118, 118, 118), rgb(133, 133, 133));
border-image: initial;
}
input[type="text"], input[type="password"]
{
height: 5rem;
width: 100%;
padding: 1rem 1.1875rem;
border: 1px solid #ddd;
background: #fff;
color: #111;
line-height: 1.2rem;
letter-spacing: -0.025rem;
font-family: 'Geogrotesque Md', 'YoonGothic', sans-serif;
}
button {
width: 100%;
height: 4rem;
appearance: auto;
writing-mode: horizontal-tb !important;
text-rendering: auto;
color: buttontext;
letter-spacing: normal;
word-spacing: normal;
line-height: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: center;
align-items: flex-start;
cursor: default;
box-sizing: border-box;
margin-top: 10px;
margin: 5px;
padding: 1px 6px;
border-width: 0.5px;
}
domain
Member.java
package hello.hellospring.domain;
public class Member {
private String id;
private String pw;
private String name;
private String email;
private String phone;
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Controller
MemberController.java
package hello.hellospring.Controller;
import hello.hellospring.domain.Member;
import hello.hellospring.mapper.MemberMapper;
import hello.hellospring.service.MemberService;
import lombok.extern.slf4j.Slf4j;
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 java.util.List;
//@RestController
@Controller
@Slf4j
public class MemberController {
private final MemberService memberService;
private final MemberMapper memberMapper;
@Autowired
public MemberController(MemberService memberService, MemberMapper memberMapper) {
this.memberService = memberService;
this.memberMapper = memberMapper;
}
@PostMapping("members/register_ok")
public String register_ok(@ModelAttribute Member member , Model model) {
memberMapper.setMember(member);
System.out.println("====================" + member.getId());
System.out.println("====================" + member.getName());
// System.out.println("====================" + member.toString());
model.addAttribute("member", member);
return "members/member";
}
@GetMapping("/members/register")
public String register(Model model){
return "/members/register";
}
@GetMapping("/members")
public String list(Model model){
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
return "members/memberlist";
}
interface
MemberMapper
package hello.hellospring.mapper;
import hello.hellospring.domain.Member;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface MemberMapper {
Member getMember(Member member);
int setMember(Member member);
void deleteMember(Member member);
}
--------Interceptor 코드 수정 사용 필요-----------
InterceptorConfig
package hello.hellospring.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Autowired
LoginInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// this interceptor will be applied to all URLs
registry.addInterceptor(interceptor);
}
}
Logininterceptor
package hello.hellospring.config;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("-----interceptor-----");
String path = request.getRequestURI().toString();
System.out.println(path);
if( path.equals("/css/**") || path.equals("/") || path.equals("/index") || path.equals("/login")|| path.equals("/login_ok")) return true;
HttpSession session = request.getSession();
String name = (String)session.getAttribute("name");
System.out.printf("session: " + name);
if(name != null) return true;
else {
response.sendRedirect("/index");
return false;
}
}
}
'개발 > Java' 카테고리의 다른 글
[Spring] 스프링 입문 5 - 게시판 제작하기 (0) | 2023.01.05 |
---|---|
[Spring] Infearn 스프링 입문3 - (응용)MySQL & Mybatis 사용 (1) | 2022.12.15 |
[Android 개발] Unresolved class 'Activity', xml에서 클래스를 못찾을 때 해결 (0) | 2022.12.07 |
[Spring] Infearn 스프링 입문 필기 2 - 실습, 회원 등록, 회원 조회 기능 만들기 (0) | 2022.12.06 |
[Spring] Infearn 스프링 입문 필기 1 - 개념 학습 (1) | 2022.12.06 |