-
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.
시큐리티 기능 적용 디펜던시에는 타임리프를 사용하므로 `thymeleaf-extras-springsecurity5` 추가 잊으면 안 됨 시큐리티 설정은 DB를 사용하는 방식으로, 테스트 계정의 패스워드는 `noop` 을 써서 암호화하지 않음 추후 암호화 가능 로그인 인증 예외 범위는 루트 및 일반 조회 페이지로 지정 기존 테스트는 시큐리티와 별개로 동작시키고 싶으므로 시큐리티 자동 설정과 필터를 제외하는 규칙을 추가 스프링 시큐리티의 인증 테스트는 필요에 따라 별도로 진행할 계획 **PS Spring Security 5.7.0-M2 부터 ` WebSecurityConfigurerAdapter ` 지원을 하지 않는다. ` SecurityFilterChain` 을 직접 Bean으로 등혹하는 방식을 권장.
- Loading branch information
Showing
11 changed files
with
145 additions
and
10 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
47 changes: 47 additions & 0 deletions
47
src/main/java/com/bm/getin/config/SecurityCustomConfig.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,47 @@ | ||
package com.bm.getin.config; | ||
|
||
import com.bm.getin.service.AdminService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
public class SecurityCustomConfig { | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
} | ||
|
||
@Autowired | ||
public void configureGlobal( | ||
AuthenticationManagerBuilder auth, | ||
PasswordEncoder passwordEncoder, | ||
AdminService adminService | ||
) throws Exception { | ||
auth.userDetailsService(adminService).passwordEncoder(passwordEncoder); | ||
} | ||
|
||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.antMatchers("/", "/event/**", "/places/**") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
.and() | ||
.formLogin() | ||
.permitAll() | ||
.and() | ||
.logout() | ||
.permitAll() | ||
.logoutSuccessUrl("/") | ||
; | ||
|
||
return http.build(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,31 @@ | ||
package com.bm.getin.service; | ||
|
||
public class AdminService { | ||
import com.bm.getin.domain.Admin; | ||
import com.bm.getin.repository.AdminRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.User; | ||
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 java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AdminService implements UserDetailsService { | ||
private final AdminRepository adminRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
Admin admin = adminRepository.findByEmail(username) | ||
.orElseThrow(() -> new UsernameNotFoundException("Admin Not Found.")); | ||
|
||
return User.builder() | ||
.username(admin.getEmail()) | ||
.password(admin.getPassword()) | ||
.authorities(List.of()) | ||
.build() | ||
; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -39,7 +39,7 @@ values | |
|
||
insert into `admin` (`email`, `nickname`, `password`, `phone_number`, `memo`) | ||
values | ||
('[email protected]', '테스트', '1234', '010-0101-0101', '안녕하세요') | ||
('[email protected]', '테스트', '{noop}1234', '010-0101-0101', '안녕하세요') | ||
; | ||
|
||
insert into `admin_place_map` (`admin_id`, `place_id`) | ||
|
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
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
12 changes: 11 additions & 1 deletion
12
src/test/java/com/bm/getin/controller/error/BaseErrorControllerTest.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