-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[톰캣 구현하기 - 3, 4단계] 달리 미션 제출합니다 #442
Changes from 10 commits
d007034
7125da6
f03ea85
eb2e34d
43c6c51
79da7b0
b6cfe11
753539f
9c26df1
0ff608d
8b12c21
ec7ad61
3bd9ea4
def103c
5019ea3
0fca1b5
0ad5111
eb87ed1
0ff993d
5f963d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package nextstep.jwp.config; | ||
|
||
import nextstep.jwp.db.InMemorySession; | ||
import org.apache.coyote.http11.filter.Filter; | ||
import org.apache.coyote.http11.filter.FilterChain; | ||
import org.apache.coyote.http11.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.util.Resource; | ||
|
||
public class LoginFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(Request request, Response response, FilterChain filterChain) { | ||
final String uri = request.getPath(); | ||
final var cookie = request.getCookie(); | ||
|
||
if (uri.equals("/login") && cookie.containsKey("JSESSIONID")) { | ||
validKey(cookie.get("JSESSIONID"), response); | ||
} | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void validKey(String jSessionId, Response response) { | ||
if (InMemorySession.isLogin(jSessionId)) { | ||
response | ||
.setStatus(HttpStatus.FOUND) | ||
.setContentType("html") | ||
.setLocation("/index.html") | ||
.setResponseBody(Resource.getFile("index.html")) | ||
.setFiltered(true); | ||
return; | ||
} | ||
response | ||
.setStatus(HttpStatus.UNAUTHORIZED) | ||
.setContentType("html") | ||
.setResponseBody(Resource.getFile("401.html")) | ||
.setLocation("/401.html") | ||
.setFiltered(true); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import org.apache.coyote.http11.controller.AbstractController; | ||
import org.apache.coyote.http11.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
|
||
public class HelloWorldController extends AbstractController { | ||
@Override | ||
protected void doGet(Request request, Response response) { | ||
response.setStatus(HttpStatus.OK) | ||
.setContentType("html") | ||
.setResponseBody("Hello world!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,43 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import nextstep.jwp.db.InMemorySession; | ||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.exception.UnauthorizedException; | ||
import nextstep.jwp.model.AuthUser; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.controller.AbstractController; | ||
import org.apache.coyote.http11.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.servlet.Servlet; | ||
import org.apache.coyote.http11.util.Resource; | ||
|
||
public class LoginController { | ||
public class LoginController extends AbstractController { | ||
|
||
public static Response login(Request request){ | ||
@Override | ||
protected void doPost(Request request, Response response) { | ||
Map<String, String> body = request.getBody(); | ||
AuthUser authUser = AuthUser.from(body); | ||
User user = InMemoryUserRepository.findByAccount(authUser.getAccount()).orElseThrow(()->new UnauthorizedException("해당 유저가 없습니다.")); | ||
if(!user.checkPassword(authUser.getPassword())){ | ||
User user = InMemoryUserRepository.findByAccount(body.get("account")) | ||
.orElseThrow(() -> new UnauthorizedException("해당 유저가 없습니다.")); | ||
if (!user.checkPassword(body.get("password"))) { | ||
throw new UnauthorizedException("아이디 및 패스워드가 틀렸습니다."); | ||
} | ||
String jSessionId = InMemorySession.login(user); | ||
Map<String,String> cookie = new HashMap<>(); | ||
if(!request.getCookie().containsKey("JSESSIONID")){ | ||
cookie.put("JSESSIONID",jSessionId); | ||
Map<String, String> cookie = new HashMap<>(); | ||
if (!request.getCookie().containsKey("JSESSIONID")) { | ||
cookie.put("JSESSIONID", jSessionId); | ||
} | ||
return Response.builder() | ||
.status(HttpStatus.FOUND) | ||
.contentType("html") | ||
.cookie(cookie) | ||
.location("index.html") | ||
.responseBody(getFile("index.html")) | ||
.build(); | ||
response.setStatus(HttpStatus.FOUND) | ||
.setContentType("html") | ||
.setCookie(cookie) | ||
.setLocation("index.html") | ||
.setResponseBody(Resource.getFile("index.html")); | ||
} | ||
|
||
public static Response signUp(Request request){ | ||
Map<String, String> requestBody = request.getBody(); | ||
final String account = requestBody.get("account"); | ||
final String password = requestBody.get("password"); | ||
final String email = requestBody.get("email"); | ||
User user = new User(account,password,email); | ||
InMemoryUserRepository.save(user); | ||
return Response.builder() | ||
.status(HttpStatus.FOUND) | ||
.contentType("html") | ||
.location("index.html") | ||
.responseBody(getFile("index.html")) | ||
.build(); | ||
} | ||
|
||
|
||
private static String getFile(String fileName){ | ||
try { | ||
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName); | ||
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath()); | ||
return new String(fileBytes); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} catch (NullPointerException e){ | ||
return ""; | ||
} | ||
@Override | ||
protected void doGet(Request request, Response response) { | ||
response.setStatus(HttpStatus.OK) | ||
.setContentType("html") | ||
.setResponseBody(Resource.getFile("login.html")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import java.util.Map; | ||
import nextstep.jwp.db.InMemoryUserRepository; | ||
import nextstep.jwp.model.User; | ||
import org.apache.coyote.http11.controller.AbstractController; | ||
import org.apache.coyote.http11.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.util.Resource; | ||
|
||
public class RegisterController extends AbstractController { | ||
@Override | ||
public void doPost(Request request, Response response) { | ||
Map<String, String> requestBody = request.getBody(); | ||
final String account = requestBody.get("account"); | ||
final String password = requestBody.get("password"); | ||
final String email = requestBody.get("email"); | ||
User user = new User(account, password, email); | ||
InMemoryUserRepository.save(user); | ||
response.setStatus(HttpStatus.FOUND) | ||
.setContentType("html") | ||
.setLocation("index.html") | ||
.setResponseBody(Resource.getFile("index.html")); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 개행이 두줄 입니다!😱 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@Override | ||
protected void doGet(Request request, Response response) { | ||
response.setStatus(HttpStatus.OK) | ||
.setContentType("html") | ||
.setResponseBody(Resource.getFile("register.html")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,20 @@ | ||
package nextstep.jwp.controller; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import org.apache.coyote.http11.controller.AbstractController; | ||
import org.apache.coyote.http11.request.Request; | ||
import org.apache.coyote.http11.response.HttpStatus; | ||
import org.apache.coyote.http11.response.Response; | ||
import org.apache.coyote.http11.servlet.Servlet; | ||
import org.apache.coyote.http11.util.Resource; | ||
|
||
public class ViewController { | ||
public static Response getLogin(Request request){ | ||
return Response.builder() | ||
.status(HttpStatus.OK) | ||
.contentType("html") | ||
.responseBody(getFile("login.html")) | ||
.build(); | ||
} | ||
|
||
public static Response getRegister(Request request){ | ||
return Response.builder() | ||
.status(HttpStatus.OK) | ||
.contentType("html") | ||
.responseBody(getFile("register.html")) | ||
.build(); | ||
} | ||
public class ViewController extends AbstractController { | ||
|
||
public static Response getVoid(Request request){ | ||
return Response.builder() | ||
.status(HttpStatus.OK) | ||
.responseBody("Hello world!") | ||
.contentType("html") | ||
.build(); | ||
} | ||
@Override | ||
protected void doGet(Request request, Response response) { | ||
|
||
String fileName = request.getPath(); | ||
|
||
private static String getFile(String fileName){ | ||
try { | ||
final var fileUrl = Servlet.class.getClassLoader().getResource("static/" + fileName); | ||
final var fileBytes = Files.readAllBytes(new File(fileUrl.getFile()).toPath()); | ||
return new String(fileBytes); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} catch (NullPointerException e){ | ||
return ""; | ||
} | ||
response.setStatus(HttpStatus.OK) | ||
.setContentType(fileName.split("\\.")[1]) | ||
.setResponseBody(Resource.getFile(fileName)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,20 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import nextstep.jwp.exception.UnauthorizedException; | ||
import nextstep.jwp.model.User; | ||
|
||
public class InMemorySession { | ||
private static final Map<User, UUID> session = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 한줄 개행 추가부탁합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 아코 말이 맞는 것 같네요! 단순히 user가 key를 가지는 것이라 생각했는데 생각해보니 uuid를 통해 user를 찾는 것이니 이런형식이 맞겠군요. |
||
|
||
public static String login(User user){ | ||
public static String login(User user) { | ||
UUID uuid = UUID.randomUUID(); | ||
session.put(user,uuid); | ||
session.put(user, uuid); | ||
return uuid.toString(); | ||
} | ||
public static boolean isLogin(String id){ | ||
for(UUID uuid: session.values()){ | ||
if(uuid.toString().equals(id)){ | ||
|
||
public static boolean isLogin(String id) { | ||
for (UUID uuid : session.values()) { | ||
if (uuid.toString().equals(id)) { | ||
return true; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
package nextstep.jwp.db; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
import nextstep.jwp.model.User; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import nextstep.jwp.model.User; | ||
|
||
public class InMemoryUserRepository { | ||
|
||
private static final Map<String, User> database = new ConcurrentHashMap<>(); | ||
private static final AtomicLong autoIncrementId = new AtomicLong(1L); | ||
|
||
static { | ||
final User user = new User(autoIncrementId.get(), "gugu", "password", "hkkang@woowahan.com"); | ||
database.put(user.getAccount(), user); | ||
} | ||
|
||
private InMemoryUserRepository() { | ||
} | ||
|
||
public static void save(User user) { | ||
database.put(user.getAccount(), user.putId(autoIncrementId.addAndGet(1L))); | ||
} | ||
|
||
public static Optional<User> findByAccount(String account) { | ||
return Optional.ofNullable(database.get(account)); | ||
} | ||
|
||
private InMemoryUserRepository() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package nextstep.jwp.exception; | ||
|
||
public class UnauthorizedException extends RuntimeException{ | ||
public class UnauthorizedException extends RuntimeException { | ||
public UnauthorizedException(String message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기도 개행 추가 부탁드립니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
super(message); | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 자료형을 나타낼 때 var를 사용하는 경우와 그렇지 않은 경우는 어떤 기준이신가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 원래는
var
를 안쓸려고 했는데 여기에 추가한 것을 깜빡했네요;;;;var
를 안써봐서 확실한 기준이 아직은 없네요;;만약 사용한다면 메서드나 기능적인면 혹은 역할이 바뀌지 않는 객체이지만 후에 type이 변경될 수 있을때 사용할 것 같습니다.
0ad5111