반응형
56. 57. 로그인 API 분리 - 1,2
- 프로젝트 내에 다른 프로젝트로 분기 -> 모듈을 생성(build gradle 도 만들어야함 )-> 재시작 필요
- refactor -> move classes 하면 파일들이 잘 넘어 갑니다. (copy classes도 가능합니다.)
- build -> sync 누르는것이 재시작과 비슷한 역할을 해준다.
- test all 탐난다. 어떻게 하는지 나중에 알아내자.
package kr.co.fastcampus.eatgo.interfaces;
import kr.co.fastcampus.eatgo.application.UserService;
import kr.co.fastcampus.eatgo.domain.User;
import kr.co.fastcampus.eatgo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.net.URI;
import java.net.URISyntaxException;
@CrossOrigin
@RestController
public class SessionController {
@Autowired
private JwtUtil jwtUtil;
@Autowired
private UserService userService;
@PostMapping("/session")
public ResponseEntity<SessionResponseDto> create(
@RequestBody SessionRequestDto resource
) throws URISyntaxException {
String email = resource.getEmail();
String password = resource.getPassword();
User user = userService.authenticate(email, password);
String accessToken = jwtUtil.createToken(
user.getId(),
user.getName(),
user.isRestaurantOwner() ? user.getRestaurantId() : null);
String url = "/session";
return ResponseEntity.created(new URI(url)).body(
SessionResponseDto.builder()
.accessToken(accessToken)
.build());
}
}
package kr.co.fastcampus.eatgo.application;
import kr.co.fastcampus.eatgo.domain.User;
import kr.co.fastcampus.eatgo.domain.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Optional;
@Service
@Transactional
public class UserService {
UserRepository userRepostory;
PasswordEncoder passwordEncoder;
@Autowired
public UserService(UserRepository userRepostory,
PasswordEncoder passwordEncoder) {
this.userRepostory = userRepostory;
this.passwordEncoder = passwordEncoder;
}
public User authenticate(String email, String password) {
User user = userRepostory.findByEmail(email)
.orElseThrow(() -> new EmailNotExistedException(email));
if (!passwordEncoder.matches(password, user.getPassword())) {
throw new PasswordWrongException();
}
return user;
}
}
package kr.co.fastcampus.eatgo;
import kr.co.fastcampus.eatgo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter {
@Value("${jwt.secret}")
private String secret;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().disable()
.csrf().disable()
.formLogin().disable()
.headers().frameOptions().disable();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public JwtUtil jwtUtil() {
return new JwtUtil(secret);
}
}
package kr.co.fastcampus.eatgo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EatgoLoginApiApplication {
public static void main(String[] args) {
SpringApplication.run(EatgoLoginApiApplication.class, args);
}
}
자바 인강이 듣고 싶다면 =>https://bit.ly/3ilMbIO
반응형
'스프링, 자바' 카테고리의 다른 글
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 44회차 미션 (0) | 2020.09.22 |
---|---|
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 43회차 미션 (0) | 2020.09.21 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 41회차 미션 (0) | 2020.09.19 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 40회차 미션 (0) | 2020.09.18 |
[패스트캠퍼스 수강 후기] 자바 인강 100% 환급 챌린지 39회차 미션 (0) | 2020.09.17 |