JWT
JWT ํ ํฐ์ผ๋ก ๋ก๊ทธ์ธ
๋ก๊ทธ์ธ์ ์๋๋ localhost:8080/login ์ ํธ์ถํ๋ฉด ์คํ๋ง ์ํ๋ฆฌํฐ๊ฐ ์์์ UserDetailsService ๋น์ ์ฐพ์ loadUserByUsername์ ํธ์ถํ๋๋ฐ filterChain ์์ formLogin์ disableํ๊ณ ์ปค์คํ ํํฐ๋ฅผ ๋ฑ๋กํด์ ๋ก๊ทธ์ธ์ ํ ๊ฒ์ด๋ค.
SecurityConfig.java
security ๊ธฐ๋ณธ์ ์ ์ด๋์ config์ ๊ฐ๋ค
@Configuration
@EnableWebSecurity // ์ํ๋ฆฌํฐ ํ์ฑํ -> ๊ธฐ๋ณธ ์คํ๋ง ํํฐ์ฒด์ธ์ ๋ฑ๋ก
public class SecurityConfig {
@Autowired
private UserRepository userRepository;
@Autowired
private CorsConfig corsConfig;
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.formLogin().disable()
.httpBasic().disable()
.apply(new MyCustomDsl()) // ์ปค์คํ
ํํฐ ๋ฑ๋ก
.and()
.authorizeRequests(authroize -> authroize.antMatchers("/api/v1/user/**")
.access("hasRole('ROLE_USER') or hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")
.antMatchers("/api/v1/manager/**")
.access("hasRole('ROLE_MANAGER') or hasRole('ROLE_ADMIN')")
.antMatchers("/api/v1/admin/**")
.access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll())
.build();
}
public class MyCustomDsl extends AbstractHttpConfigurer<MyCustomDsl, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
http
.addFilter(corsConfig.corsFilter())
.addFilter(new JwtAuthenticationFilter(authenticationManager))
.addFilter(new JwtAuthorizationFilter(authenticationManager, userRepository));
}
}
}JwtAutenticationFilter.java
๋ก๊ทธ์ธ์ ์คํ๋๋ ํํฐ๋ฅผ ์์๋ฐ์ ํํฐ๋ฅผ ๋ง๋ค์ด์ค์ SecurityConfig ์ ๋ฑ๋กํด์ค์ผํจ
UsernamePasswordAuthenticationFilter๋ AuthenticationManager๊ฐ ๋งค๊ฐ๋ณ์๋ก ํ์ํจ. ์์ SecurityConfig.java์ ํํฐ๋ฅผ ๋ฑ๋กํ๋ ๋ถ๋ถ์ ๋ณด๋ฉด AuthenticationManager ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋๊ฒจ์ฃผ๋๊ฑธ ๋ณผ ์ ์์
PrincipalDetailsService ์ PrincipalDetails๋ security ๊ธฐ๋ณธ์ ์์ฑ๋์ด ์์, ์์ฑํด์ค์ผํจ
JwtProperties.java
Last updated