-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from KMGeon/main
Rest API - Security
- Loading branch information
Showing
33 changed files
with
1,002 additions
and
491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 28 additions & 18 deletions
46
app/src/main/java/com/codesoom/assignment/application/AuthenticationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,46 @@ | ||
package com.codesoom.assignment.application; | ||
|
||
import com.codesoom.assignment.domain.Role; | ||
import com.codesoom.assignment.domain.RoleRepository; | ||
import com.codesoom.assignment.domain.User; | ||
import com.codesoom.assignment.domain.UserRepository; | ||
import com.codesoom.assignment.errors.LoginFailException; | ||
import com.codesoom.assignment.utils.JwtUtil; | ||
import io.jsonwebtoken.Claims; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthenticationService { | ||
private final UserRepository userRepository; | ||
private final JwtUtil jwtUtil; | ||
|
||
public AuthenticationService(UserRepository userRepository, | ||
JwtUtil jwtUtil) { | ||
this.userRepository = userRepository; | ||
this.jwtUtil = jwtUtil; | ||
private final UserRepository userRepository; | ||
private final JwtUtil jwtUtil; | ||
private final PasswordEncoder passwordEncoder; | ||
private final RoleRepository roleRepository; | ||
|
||
public String login(String email, String password) { | ||
User user = userRepository.findByEmail(email) | ||
.orElseThrow(() -> new LoginFailException(email)); | ||
|
||
if (!user.authenticate(password, passwordEncoder)) { | ||
throw new LoginFailException(email); | ||
} | ||
|
||
public String login(String email, String password) { | ||
User user = userRepository.findByEmail(email) | ||
.orElseThrow(() -> new LoginFailException(email)); | ||
return jwtUtil.encode(user.getId()); | ||
} | ||
|
||
if (!user.authenticate(password)) { | ||
throw new LoginFailException(email); | ||
} | ||
public Long parseToken(String accessToken) { | ||
Claims claims = jwtUtil.decode(accessToken); | ||
return claims.get("userId", Long.class); | ||
} | ||
|
||
return jwtUtil.encode(1L); | ||
} | ||
public List<Role> roles(Long userId) { | ||
return roleRepository.findAllByUserId(userId); | ||
} | ||
|
||
public Long parseToken(String accessToken) { | ||
Claims claims = jwtUtil.decode(accessToken); | ||
return claims.get("userId", Long.class); | ||
} | ||
} |
69 changes: 42 additions & 27 deletions
69
app/src/main/java/com/codesoom/assignment/application/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,69 @@ | ||
package com.codesoom.assignment.application; | ||
|
||
import com.codesoom.assignment.domain.Role; | ||
import com.codesoom.assignment.domain.RoleRepository; | ||
import com.codesoom.assignment.domain.User; | ||
import com.codesoom.assignment.domain.UserRepository; | ||
import com.codesoom.assignment.dto.UserModificationData; | ||
import com.codesoom.assignment.dto.UserRegistrationData; | ||
import com.codesoom.assignment.errors.UserEmailDuplicationException; | ||
import com.codesoom.assignment.errors.UserNotFoundException; | ||
import com.github.dozermapper.core.Mapper; | ||
import java.nio.file.AccessDeniedException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
private final Mapper mapper; | ||
private final UserRepository userRepository; | ||
|
||
public UserService(Mapper dozerMapper, UserRepository userRepository) { | ||
this.mapper = dozerMapper; | ||
this.userRepository = userRepository; | ||
} | ||
private final Mapper mapper; | ||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final RoleRepository roleRepository; | ||
|
||
public User registerUser(UserRegistrationData registrationData) { | ||
String email = registrationData.getEmail(); | ||
if (userRepository.existsByEmail(email)) { | ||
throw new UserEmailDuplicationException(email); | ||
} | ||
|
||
User user = mapper.map(registrationData, User.class); | ||
return userRepository.save(user); | ||
public User registerUser(UserRegistrationData registrationData) { | ||
String email = registrationData.getEmail(); | ||
if (userRepository.existsByEmail(email)) { | ||
throw new UserEmailDuplicationException(email); | ||
} | ||
// User user = mapper.map(registrationData, User.class); | ||
User user = userRepository.save( | ||
mapper.map(registrationData, User.class)); | ||
Role role = roleRepository.save(new Role(user.getId(), "USER")); | ||
user.changePassword(registrationData.getPassword(), passwordEncoder); | ||
|
||
public User updateUser(Long id, UserModificationData modificationData) { | ||
User user = findUser(id); | ||
return user; | ||
} | ||
|
||
User source = mapper.map(modificationData, User.class); | ||
user.changeWith(source); | ||
|
||
return user; | ||
public User updateUser(Long id, UserModificationData modificationData, Long userId) | ||
throws AccessDeniedException { | ||
if (id != userId) { | ||
throw new AccessDeniedException("권한이 없습니다."); | ||
} | ||
|
||
public User deleteUser(Long id) { | ||
User user = findUser(id); | ||
user.destroy(); | ||
return user; | ||
} | ||
User user = findUser(id); | ||
|
||
private User findUser(Long id) { | ||
return userRepository.findByIdAndDeletedIsFalse(id) | ||
.orElseThrow(() -> new UserNotFoundException(id)); | ||
} | ||
User source = mapper.map(modificationData, User.class); | ||
user.changeWith(source); | ||
|
||
return user; | ||
} | ||
|
||
public User deleteUser(Long id) { | ||
User user = findUser(id); | ||
user.destroy(); | ||
return user; | ||
} | ||
|
||
private User findUser(Long id) { | ||
return userRepository.findByIdAndDeletedIsFalse(id) | ||
.orElseThrow(() -> new UserNotFoundException(id)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/codesoom/assignment/config/SecurityJavaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.codesoom.assignment.config; | ||
|
||
import com.codesoom.assignment.application.AuthenticationService; | ||
import com.codesoom.assignment.filter.AuthenticationErrorFilter; | ||
import com.codesoom.assignment.filter.JwtAuthenticationFilter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.authentication.HttpStatusEntryPoint; | ||
|
||
import javax.servlet.Filter; | ||
|
||
@Configuration | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private AuthenticationService authenticationService; | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
Filter authenticationFilter = new JwtAuthenticationFilter(authenticationManager(), | ||
authenticationService); | ||
Filter authenticationErrorFilter = new AuthenticationErrorFilter(); | ||
http | ||
.csrf().disable() | ||
.headers() | ||
.frameOptions().disable() | ||
.and() | ||
.addFilter(authenticationFilter) | ||
.addFilterBefore(authenticationErrorFilter, | ||
JwtAuthenticationFilter.class) | ||
.sessionManagement() | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
.and() | ||
.exceptionHandling() | ||
.authenticationEntryPoint( | ||
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)); | ||
} | ||
|
||
} |
18 changes: 0 additions & 18 deletions
18
app/src/main/java/com/codesoom/assignment/config/WebJavaConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.