-
Notifications
You must be signed in to change notification settings - Fork 0
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 #29 from jwpark1211/develop
Develop
- Loading branch information
Showing
15 changed files
with
399 additions
and
29 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
56 changes: 56 additions & 0 deletions
56
src/main/java/capstone/bookitty/common/CustomUserDetails.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,56 @@ | ||
package capstone.bookitty.common; | ||
|
||
import capstone.bookitty.domain.entity.Member; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
public class CustomUserDetails implements UserDetails { | ||
private final Member member; | ||
|
||
public CustomUserDetails(Member member) { | ||
this.member = member; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
Collection<GrantedAuthority> authorities = new ArrayList<>(); | ||
String roles = member.getAuthority().toString(); | ||
for (String role : roles.split(",")) { | ||
authorities.add(() -> role); | ||
} | ||
return authorities; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return member.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return member.getEmail(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/capstone/bookitty/common/CustomUserDetailsService.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,24 @@ | ||
package capstone.bookitty.common; | ||
|
||
import capstone.bookitty.domain.entity.Member; | ||
import capstone.bookitty.domain.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { | ||
Member member = memberRepository.findByEmail(email) | ||
.orElseThrow(() -> new UsernameNotFoundException("User not found with email: " + email)); | ||
return new CustomUserDetails(member); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/capstone/bookitty/common/SecurityConfig.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,66 @@ | ||
package capstone.bookitty.common; | ||
|
||
import capstone.bookitty.jwt.JwtFilter; | ||
import capstone.bookitty.jwt.JwtProperties; | ||
import capstone.bookitty.jwt.JwtTokenProvider; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
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.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
|
||
import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
private final JwtTokenProvider jwtTokenProvider; | ||
private final JwtProperties jwtProperties; | ||
|
||
public SecurityConfig(JwtTokenProvider jwtTokenProvider, JwtProperties jwtProperties) { | ||
this.jwtTokenProvider = jwtTokenProvider; | ||
this.jwtProperties = jwtProperties; | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.httpBasic(AbstractHttpConfigurer::disable) | ||
.headers((headersConfig) -> | ||
headersConfig.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) | ||
.authorizeHttpRequests((authorizeRequests) -> | ||
authorizeRequests | ||
.requestMatchers(antMatcher("/"), | ||
antMatcher("/members/test"), | ||
antMatcher("/members/login"), | ||
antMatcher("/members/new"), | ||
antMatcher("/members/email/**")).permitAll() | ||
//.requestMatchers(antMatcher("")).authenticated() | ||
.anyRequest().permitAll()) | ||
.formLogin(AbstractHttpConfigurer::disable) | ||
.logout((logout) -> | ||
logout | ||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) | ||
.invalidateHttpSession(true) | ||
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)) | ||
.deleteCookies("JSESSIONID") | ||
) | ||
.addFilterBefore(new JwtFilter(jwtTokenProvider, jwtProperties), UsernamePasswordAuthenticationFilter.class); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
} | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/java/capstone/bookitty/domain/dto/TokenResponseDTO.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,19 @@ | ||
package capstone.bookitty.domain.dto; | ||
|
||
import capstone.bookitty.jwt.JwtToken; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
|
||
@Data | ||
@Getter | ||
@Builder | ||
public class TokenResponseDTO { | ||
private Long idx; | ||
private JwtToken jwtToken; | ||
|
||
public TokenResponseDTO(Long idx, JwtToken jwtToken) { | ||
this.idx = idx; | ||
this.jwtToken = jwtToken; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import jakarta.persistence.EntityManager; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDate; | ||
|
@@ -28,24 +29,25 @@ public void init(){ | |
static class InitService{ | ||
|
||
private final EntityManager em; | ||
private final PasswordEncoder pwEncoder; | ||
|
||
public void dbInit(){ | ||
List<Member> members = Arrays.asList( | ||
new Member("김민준", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(1992, 7, 21)), | ||
new Member("이서현", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(2010, 12, 8)), | ||
new Member("서진호", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(1971, 8, 28)), | ||
new Member("이선희", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(1969, 2, 5)), | ||
new Member("신준서", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(2005, 8, 20)), | ||
new Member("문다연", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(1999, 1, 14)), | ||
new Member("윤동현", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(1989, 7, 3)), | ||
new Member("송지은", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(1995, 3, 18)), | ||
new Member("김준서", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(2001, 12, 11)), | ||
new Member("임지민", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(2009, 3, 14)), | ||
new Member("안지성", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(2002, 8, 8)), | ||
new Member("황예린", "[email protected]", "Wo1902!si", null, Gender.FEMALE, LocalDate.of(1991, 11, 28)), | ||
new Member("송현우", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(1999, 9, 8)), | ||
new Member("정우진", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(2004, 1, 13)), | ||
new Member("서은우", "[email protected]", "Wo1902!si", null, Gender.MALE, LocalDate.of(2008, 2, 20)) | ||
new Member("김민준", "[email protected]", pwEncoder.encode("Wo1902!si1"), null, Gender.MALE, LocalDate.of(1992, 7, 21)), | ||
new Member("이서현", "[email protected]", pwEncoder.encode("Wo1902!si2"), null, Gender.FEMALE, LocalDate.of(2010, 12, 8)), | ||
new Member("서진호", "[email protected]", pwEncoder.encode("Wo1902!si3"), null, Gender.MALE, LocalDate.of(1971, 8, 28)), | ||
new Member("이선희", "[email protected]", pwEncoder.encode("Wo1902!si4"), null, Gender.FEMALE, LocalDate.of(1969, 2, 5)), | ||
new Member("신준서", "[email protected]", pwEncoder.encode("Wo1902!si5"), null, Gender.MALE, LocalDate.of(2005, 8, 20)), | ||
new Member("문다연", "[email protected]", pwEncoder.encode("Wo1902!si6"), null, Gender.FEMALE, LocalDate.of(1999, 1, 14)), | ||
new Member("윤동현", "[email protected]", pwEncoder.encode("Wo1902!si7"), null, Gender.MALE, LocalDate.of(1989, 7, 3)), | ||
new Member("송지은", "[email protected]", pwEncoder.encode("Wo1902!si8"), null, Gender.FEMALE, LocalDate.of(1995, 3, 18)), | ||
new Member("김준서", "[email protected]", pwEncoder.encode("Wo1902!si9"), null, Gender.MALE, LocalDate.of(2001, 12, 11)), | ||
new Member("임지민", "[email protected]", pwEncoder.encode("Wo1902!si10"), null, Gender.FEMALE, LocalDate.of(2009, 3, 14)), | ||
new Member("안지성", "[email protected]", pwEncoder.encode("Wo1902!si11"), null, Gender.MALE, LocalDate.of(2002, 8, 8)), | ||
new Member("황예린", "[email protected]", pwEncoder.encode("Wo1902!si12"), null, Gender.FEMALE, LocalDate.of(1991, 11, 28)), | ||
new Member("송현우", "[email protected]", pwEncoder.encode("Wo1902!si13"), null, Gender.MALE, LocalDate.of(1999, 9, 8)), | ||
new Member("정우진", "[email protected]", pwEncoder.encode("Wo1902!si14"), null, Gender.MALE, LocalDate.of(2004, 1, 13)), | ||
new Member("서은우", "[email protected]", pwEncoder.encode("Wo1902!si15"), null, Gender.MALE, LocalDate.of(2008, 2, 20)) | ||
); | ||
|
||
for (Member member : members) { | ||
|
Oops, something went wrong.