๐โ๏ธ QNA ์๋น์ค ๊ธฐ๋ฅ ๊ตฌํ (๋ณต์ต) _ 147๊ฐ ~ 154๊ฐ
1๏ธโฃ ๋ก๊ทธ์ธ
โ ๋ก๊ทธ์ธ url ๋ฑ๋ก
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll()
// (์๋ต)
.and()
.formLogin()
.loginPage("/user/login") // ๋ก๊ทธ์ธ ํ์ด์ง์ url
.defaultSuccessUrl("/") // ๋ก๊ทธ์ธ ์ฑ๊ณต ์ ์ด๋ ํ์ด์ง
;
return http.build();
}
}
โก๏ธ ์ด์ ๊ฐ์ด ์ค์ ํด์ฃผ๋ฉด ๋ก๊ทธ์ธ ํ๋ฉด์์์ POST ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์คํ๋ง ์ํ๋ฆฌํฐ์์ ์ํํ๋ค. ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด ๋ํดํธ url ๋ก ์ด๋ํ๊ณ , ์คํจํ๋ฉด ์๋ฌ๋ค๊ณผ ํจ๊ป ๋ก๊ทธ์ธ ํ๋ฉด์ผ๋ก ๋ฆฌ๋ค์ด๋ ์ ๋๋ค. ์ด๋ฌํ ์ฒ๋ฆฌ๋ฅผ ์ํด์ ์ฌ๋ฌ ๋ถ๋ถ๋ค์ ์คํ๋ง ์ํ๋ฆฌํฐ ๊ท์น์ ๋ง๊ฒ ์ค์ ํด์ผ ๋๋ค.
โก ์คํ๋ง ์ํ๋ฆฌํฐ์์ DB ์์ ํ์ ์ ๋ณด๋ฅผ ํ์ธํ๋๋ก ์ค์
- UserDetailsService ๋ฅผ ์์๋ฐ๋ UserSecurityService ๋ฅผ ์์ฑ & loadUserByUsername ๋งค์๋ ์ค๋ฒ๋ผ์ด๋
@Service
@RequiredArgsConstructor
public class UserSecurityService implements UserDetailsService {
private final UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// (1) DB ํ์ธ
Optional<SiteUser> _siteUser = userRepository.findByUsername(username);
if (_siteUser.isEmpty()) {
throw new UsernameNotFoundException("์ฌ์ฉ์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.");
}
SiteUser siteUSer = _siteUser.get();
// (2) ๊ถํ ํ์ธ ์ถ๊ฐ
List<GrantedAuthority> authorities = new ArrayList<>();
if ("admin".equals(username)) {
authorities.add(new SimpleGrantedAuthority(UserRole.ADMIN.getValue()));
}
else {
authorities.add(new SimpleGrantedAuthority(UserRole.USER.getValue()));
}
// (3) ์คํ๋ง ์ํ๋ฆฌํฐ User ๊ท๊ฒฉ์ ๋ง๊ฒ return
return new User(siteUSer.getUsername(), siteUSer.getPassword(), authorities);
}
}
- SecurityConfig ์ UserSecurityService ๋ฑ๋ก
@Configuration // ์คํ๋ง์ ํ๊ฒฝ์ค์ ํ์ผ์์ ์๋ฏธ
@EnableWebSecurity // ๋ชจ๋ ์์ฒญ URL์ด ์คํ๋ง ์ํ์ดํฐ์ ์ ์ด๋ฅผ ๋ฐ๋๋ก
public class SecurityConfig {
// (์๋ต)
@Bean
public AuthenticationManager authenticationManager (AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
โก๏ธ AuthenticationManager ๋ ์คํ๋ง ์ํ๋ฆฌํฐ์ ์ธ์ฆ์ ๋ด๋น. AuthenticationManager Bean ์์ฑ์ ์คํ๋ง์ ๋ด๋ถ ๋์์ผ๋ก ์ธํด ์์ฑํ UserSecurityService์ PasswordEncoder๊ฐ ์๋์ผ๋ก ์ค์ ๋จ.
โข ๋ก๊ทธ์ธ ํ์ธ

2๏ธโฃ ๋ก๊ทธ์์
โ SecurityConfig ์ค์
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/**").permitAll()
// (์๋ต)
// ๋ก๊ทธ์์ ์ค์
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/user/logout"))
.logoutSuccessUrl("/")
.invalidateHttpSession(true)
;
return http.build();
}
// (์๋ต)
}
โก ๋ก๊ทธ์์ ํ์ธ

๐ฌ ํ๋ก๊ทธ๋๋จธ์ค MySQL ๋ฌธ์ ํ์ด
๋ฐ๋ก ์ ๋ฆฌ : https://like099.tistory.com/53
(Programmers) SQL ํ์ด (6)
โ๏ธ Programmers MySQL ์ ๋ต๋ฅ 73~74% ์ ์ฒด ํ์ด 1๏ธโฃ 3์์ ํ์ด๋ ์ฌ์ฑ ํ์ ๋ชฉ๋ก ์ถ๋ ฅํ๊ธฐ (https://school.programmers.co.kr/learn/courses/30/lessons/131120) SELECT MEMBER_ID, MEMBER_NAME, GENDER, DA..
like099.tistory.com
'TIL ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| TIL 14์ผ์ฐจ (2022.10.26) (0) | 2022.10.26 |
|---|---|
| TIL 13์ผ์ฐจ (2022.10.25) (0) | 2022.10.25 |
| TIL 11์ผ์ฐจ (2022.10.21) (0) | 2022.10.21 |
| TIL 10์ผ์ฐจ (2022.10.20) (0) | 2022.10.20 |
| TIL 9์ผ์ฐจ (2022.10.19) (0) | 2022.10.19 |