This commit is contained in:
이진석
2020-01-31 11:13:12 +09:00
parent a3d04a39f6
commit a1034204ab
13 changed files with 159 additions and 6 deletions

View File

@@ -59,6 +59,10 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>

View File

@@ -1,6 +1,6 @@
package com.example.vue.config; package com.example.vue.config;
import com.example.vue.security.UserDetailsAuthenticationProvider; import com.example.vue.config.security.UserDetailsAuthenticationProvider;
import com.example.vue.domain.user.UserDetailsServiceImpl; import com.example.vue.domain.user.UserDetailsServiceImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -31,6 +31,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.cors().disable() http.cors().disable()
.csrf().disable()
.authorizeRequests() .authorizeRequests()
.antMatchers("/auth/login").permitAll() .antMatchers("/auth/login").permitAll()
.antMatchers("/auth/register").permitAll() .antMatchers("/auth/register").permitAll()

View File

@@ -1,4 +1,4 @@
package com.example.vue.security; package com.example.vue.config.security;
import com.example.vue.domain.user.UserDetailsServiceImpl; import com.example.vue.domain.user.UserDetailsServiceImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;

View File

@@ -0,0 +1,28 @@
package com.example.vue.domain.auth;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/auth")
@RequiredArgsConstructor
public class AuthController {
private final AuthService authService;
@PostMapping(value = "/login")
public LoginResponseDto login(@RequestBody @Valid LoginRequestDto loginRequestDto) {
authService.login(loginRequestDto);
return null;
}
@PostMapping(value = "/register")
public void register() {
}
}

View File

@@ -0,0 +1,14 @@
package com.example.vue.domain.auth;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
public class AuthException {
@ResponseStatus(HttpStatus.BAD_REQUEST)
public static class NoExistEmail extends RuntimeException {
public NoExistEmail(String email) {
super("존재하지 않는 이메일입니다. [email=" + email + "]");
}
}
}

View File

@@ -0,0 +1,19 @@
package com.example.vue.domain.auth;
import com.example.vue.domain.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class AuthService {
private final UserRepository userRepository;
public void login(LoginRequestDto loginRequestDto) {
String email = loginRequestDto.getEmail();
if (userRepository.findByEmail(email).size() < 1) {
throw new AuthException.NoExistEmail(email);
}
}
}

View File

@@ -0,0 +1,15 @@
package com.example.vue.domain.auth;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class LoginRequestDto {
@NotNull
private String email;
@NotNull
private String password;
}

View File

@@ -0,0 +1,8 @@
package com.example.vue.domain.auth;
import lombok.Data;
@Data
public class LoginResponseDto {
private String token;
}

View File

@@ -1,13 +1,28 @@
package com.example.vue.domain.user; package com.example.vue.domain.user;
import javax.persistence.Entity; import org.springframework.data.annotation.CreatedDate;
import javax.persistence.GeneratedValue; import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.Id;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity @Entity
@NamedQuery(name = "findByEmail", query = "select u from User u where u.email = :email")
public class User { public class User {
@Id @GeneratedValue @Id @GeneratedValue
Long id; private Long id;
@Column(name = "email")
private String email;
@Column(name = "name")
private String name;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
} }

View File

@@ -3,7 +3,9 @@ package com.example.vue.domain.user;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class UserDetailsServiceImpl implements UserDetailsService { public class UserDetailsServiceImpl implements UserDetailsService {
@Override @Override

View File

@@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.util.List;
@Repository @Repository
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -16,4 +17,8 @@ public class UserRepository {
return user; return user;
} }
public List<User> findByEmail(String email) {
return em.createNamedQuery("findByEmail", User.class).setParameter("email", email).getResultList();
}
} }

View File

@@ -1,9 +1,12 @@
spring.datasource.password=1111 spring.datasource.password=1111
spring.datasource.username=root spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3307/blog?serverTimezone=Asia/Seoul spring.datasource.url=jdbc:mysql://localhost:3307/blog?serverTimezone=Asia/Seoul
spring.jpa.show-sql=true spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=validate spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.use-new-id-generator-mappings=false spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
server.error.include-stacktrace=never
server.port=7070 server.port=7070

View File

@@ -0,0 +1,39 @@
CREATE TABLE user (
id bigint unsigned NOT NULL AUTO_INCREMENT,
created_at datetime(6) DEFAULT NULL,
email varchar(255) DEFAULT NULL,
password varchar(255) DEFAULT NULL,
updated_at datetime(6) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE article (
id bigint unsigned NOT NULL AUTO_INCREMENT,
content varchar(255) DEFAULT NULL,
created_at datetime(6) DEFAULT NULL,
title varchar(255) DEFAULT NULL,
updated_at datetime(6) DEFAULT NULL,
user_id bigint unsigned DEFAULT NULL,
PRIMARY KEY (id),
KEY fk_article_user_id (user_id),
CONSTRAINT fk_article_user_id FOREIGN KEY (user_id) REFERENCES user (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE authority (
id bigint unsigned NOT NULL AUTO_INCREMENT,
user_id bigint unsigned NOT NULL,
authority varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY fk_authority_user_id (user_id),
CONSTRAINT fk_authority_user_id FOREIGN KEY (user_id) REFERENCES user (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE hibernate_sequence (
next_val bigint unsigned DEFAULT NULL,
sequence_name VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;