Skip to content

Latest commit

ย 

History

History
115 lines (92 loc) ยท 3.78 KB

building.md

File metadata and controls

115 lines (92 loc) ยท 3.78 KB

springboot-security-jwt

Springboot-Security๋กœ jwt ์ธ์ฆ ์ธ๊ฐ€ ์ ์šฉ ํ•ด๋ณด๊ธฐ

gradle setting

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'
}

application.properties

# JWT
jwt.secret-key=l4(&:+*kc&uf$<0q5(9(n]cNricB'Yx(1l;YEsKE83l]lCOJrsg,YT\LL4yH7G1K
jwt.prefix=Bearer
jwt.header=Authorization

SecurityConfig

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();
    }
}