Springboot-Security๋ก jwt ์ธ์ฆ ์ธ๊ฐ ์ ์ฉ ํด๋ณด๊ธฐ
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
// junit
testImplementation 'org.testcontainers:junit-jupiter'
// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
}
# JWT
jwt.secret-key=l4(&:+*kc&uf$<0q5(9(n]cNricB'Yx(1l;YEsKE83l]lCOJrsg,YT\LL4yH7G1K
jwt.prefix=Bearer
jwt.header=Authorization
package com.szs.task.domain.configration;
import com.szs.task.domain.exception.UnAuthorizationException;
import com.szs.task.domain.service.UserService;
import io.netty.handler.codec.http.HttpMethod;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private final AuthUserDetailService authUserDetailService;
private AuthenticationManager authenticationManager;
@Value("${jwt.secretKey}")
private final String secretKey;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
// ํผ ๋ก๊ทธ์ธ ๋นํ์ฑํ
httpSecurity.formLogin(AbstractHttpConfigurer::disable);
// HTTP ๊ธฐ๋ณธ ๋นํ์ฑํ
httpSecurity.httpBasic(AbstractHttpConfigurer::disable);
// csrf ๊ณต๊ฒฉ ๋ฐฉ์ด ๊ธฐ๋ฅ ๋นํ์ฑํ
httpSecurity.csrf(AbstractHttpConfigurer::disable);
// ์ธ์
๋นํ์ฑํ => JWT๋ก ์ธ์ฆ
httpSecurity.sessionManagement(management ->
management.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
// ์ธ๊ฐ ์ค์ : ์ธ์ฆ์ด ํ์ํ ์์ฒญ ๋ฑ๋ก authenticated, ์ธ์ฆ์ด ํ์์๋ ์์ฒญ ๋ฑ๋ก permitAll
httpSecurity.authorizeHttpRequests(req -> req
.requestMatchers("/**").permitAll()
.anyRequest().authenticated()
);
/*
* ์ธ์ฆ ๋ฐฉ์ ์ค์
* - ์ธ๋ฉ๋ชจ๋ฆฌ ๋ฐฉ์
* - JDBC ๋ฐฉ์
* - ์ปค์คํ
๋ฐฉ์
* ์ ์ฉ: ์ปค์คํ
๋ฐฉ์
* */
httpSecurity.userDetailsService(authUserDetailService);
/*
* ํํฐ ์ค์
* 1. JWT Request Filter
* - JWT ํด์
* 2. JWT Filter(Login)
* - @param userId, password
* - ํ ํฐ ์์ฑ
* */
httpSecurity.addFilterAt(new JwtAuthenticationFilter(authenticationManager), null)
.addFilterBefore(null, null);
return httpSecurity.build();
}
}