Posts
- Q&A - Spring Security ์ JWT ๋๊ด์ ๋ด์ฐฉํ์ต๋๋ค... - ์๋ ํ์ธ์? ๋์์ด ๋ ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ๋ต๋ณ ๋จ๊ฒจ๋ด ๋๋ค.๋จผ์  WebSecurityConfig ํด๋์ค์์ anyRequests().authenticated()๊ฐ ์ด๋๊น์ง ์ ํ์ ๊ฑฐ๋ ์ง ์๊ณ ๊ฐ๋ฉด ์ข์ ๋ฏ ํฉ๋๋ค. ์ ๋ ์  ์ ํ์ด ์ด๋๊น์ง ์ ์ฉ๋๋์ง ๋ชฐ๋ผ์ ์ ๋ผ๋ฉด requestMatcher(new AntPathRequestMatcher("/test/**")).authenticated() ์ฒ๋ผ ์ธ์ฆ์ด ํ์ํ ๋ถ๋ถ์ ์ธ์ฆ์ด ๋๋๋ก ๊ฑธ์ด๋ ๊ฒ ๊ฐ๊ณ ๊ทธ๋ฌ๋ฉด ์ฝ๋ ์๋ฏธ๊ฐ ๋ ๋ช ๋ฃํด์ง ๊ฒ ๊ฐ์ต๋๋ค.๊ทธ๋ฆฌ๊ณ ํ ํฐ์ด ์ฝ์ ๋ถ๋ถ์ผ๋ก ์ ์ฐํ๋ค๋ฉด ๋ฐ๊ธ์ด ์ ๋ ๊ฒ์ผ๋ก ์๊ฐ๋ฉ๋๋ค. ใ ใ .. - 0
- 1
- 799
 
- Q&A - @Getter ์ ๋ ธํ ์ด์  ์ญํ - @Getter ์ ๋ ธํ ์ด์ ์ ๋ค๋ฅธ ํด๋์ค์์ ํด๋น ํด๋์ค์ ๋ณ์๋ฅผ ํ์ธํ ์ ์๊ฒ ํ๊ธฐ ์ํด ํ์ํฉ๋๋ค.์๋๋ code๋ณ์์ message ๋ณ์๋ฅผ ๊ฐ์ ธ๊ฐ์ ๋ณผ ์ ์๋๋ก getCode(), getMessage() ๋ผ๋ ๋ฉ์๋๋ก๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค. ํ์ง๋ง Lombok์์๋ ํธ์์ฑ์ ์ ๊ณตํ๋ ์ด๋ ธํ ์ด์ ์ผ๋ก @Getter๋ฅผ ์ ๊ณตํฉ๋๋ค.@RequiredArgsConstructor public class ErrorResponse { private final String code; private final String message; public String getCode() { return code; } public String getMessage() { return message; }@Getter๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ ๊ฐ์ด ๊ฐ๋ ์ฑ์ด ์ข์์ง๊ณ , ๊ฐ๊ฒฐํด์ง๋๋ค.@Getter @RequiredArgsConstructor public class ErrorResponse { private final String code; private final String message; } ์์๋ฅผ ๋ค์ด๋ณด๊ฒ ์ต๋๋ค.๋จผ์  Test ํด๋์ค์ main ํจ์์ code 401, message Unatuhorized๋ผ๋ ErrorResponse ๊ฐ์ฒด๋ฅผ ๋ง๋ค์๋ค๊ณ ํ๊ฒ ์ต๋๋ค.public class Test { public static void main(String[] args) { String code = "401"; String message = "Unauthorized"; ErrorResponse testResponse = new ErrorResponse(code, message); } } ์์ ์ฝ๋์์ 'Response์ code๊ฐ 401 ์ด๋ฉด ๊ถํ์ ์ฃผ๋ผ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ผ'๋ผ๋ ์ฝ๋๋ฅผ ๋ง๋ ๋ค๊ณ ํ ๋Getter๊ฐ ์๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด testResponse์ code๋ฅผ ํ์ธํ ์ ์๊ฒ ๋ฉ๋๋ค.(์ฌ์ง)code, message๋ฅผ ํ์ธ๊ธฐ ์ํด์๋ ์ํด์๋ Getter๊ฐ ํ์ํฉ๋๋ค.(์ฌ์ง)@Getter ์ค์ ์ ํ๊ณ ์์ ์ฝ๋๋ฅผ ๋ชจ๋ ์์ฑํ์ต๋๋ค. ์ด์  ์คํ์์ผ๋ณด๊ฒ ์ต๋๋ค.public class Test { public static void main(String[] args) { String code = "401"; String message = "Unauthorized"; ErrorResponse testResponse = new ErrorResponse(code, message); String testResponseCode = testResponse.getCode(); if (testResponseCode.equals("401")){ System.out.println("๊ถํ์ ์ฃผ์ธ์."); } } } ์ถ๋ ฅ๊ฐ : ๊ถํ์ ์ฃผ์ธ์. ๊ฒฐ๋ก : @Getter๋ฅผ ๊ผญ ์จ์ผ ํ๋ ๊ฑด ์๋์ง๋ง ๊ทธ๋ด๋ ค๋ฉด get ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ๋ณ์๋ฅผ ํ์ธํ ์ ์๊ฒ ํด์ค์ผ ํฉ๋๋ค..ํ์ต ๋จ๊ณ ๋๋, ์ด๊ธฐ ๊ฐ๋ฐ ๋จ๊ณ์์ get ๋ฉ์๋๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ์ด ๋ถํ์ํ๋ค๋ฉด @Getter๋ฅผ ์ฌ์ฉํ๋๊ฒ ํธ์์ฑ๊ณผ ๊ฐ๋ ์ฑ์ ์ข์ต๋๋ค. - 1
- 1
- 500
 
- Q&A - /api/hello์ ์ ๊ทผ์ด ์๋ฉ๋๋ค ใ ใ - ์คํ๋ง๋ถํธ์ build ์ข ์์ฑ์ spring security๊ฐ ๋ค์ด ์์ผ๋ฉด ์๋์ ์ผ๋ก ์ํ๋ฆฌํฐ๊ฐ ํ์ฑํ ๋ฉ๋๋ค.๋ฐ๋ผ์ @EnableWebSecurity๋ฅผ ๋นผ์ ๋ ์ํ๋ฆฌํฐ๊ฐ ์๋ํ๊ณ api ์์ฒญ์ ๋ํ ๊ถํ์ด ํ์ํ๋ฐ ์ด ๋ถ๋ถ์ด ์๊ธฐ ๋๋ฌธ์ 401 unauthorized๊ฐ ๋ฐ์ํฉ๋๋ค.๊ฐ๋จํ ๋ฐฉ๋ฒ ๋ ๊ฐ์ง๋ฅผ ์ ์ํด ๋๋ฆฌ๋๋ฐ ๋ฌธ์  ํด๊ฒฐ์ ๋์์ด ๋์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค.1. ์ข ์์ฑ์์ spring security๋ฅผ ๋นผ๊ณ ์ข ์์ฑ์ ์๋ก ๊ณ ์นจํด์ฃผ์ธ์. ๋ฌผ๋ก ์ด๋ ๊ฒ ํ ๊ฒฝ์ฐ์๋ ์ํ๋ฆฌํฐ๊ฐ ๋นํ์ฑํ๋ฉ๋๋ค.2. ์คํ๋ง ์ํ๋ฆฌํฐ๋ฅผ ํ์ฑํ ํ๊ณ ์ถ์๋ฐ ์ ์๋๋ ๊ฑฐ๋ผ๋ฉด ์๋์ ์ฝ๋์ฒ๋ผ ์งํํด๋ณด์๋ฉด ์ข๊ฒ ์ต๋๋ค. ํน๋ณํ ์ด์ ๋ก new AntPathRequestMatcher๋ฅผ ์จ์ผํ๋ค๋ฉด ์ฌ์ฉ๋ฐฉ๋ฒ์ ์ตํ์๊ณ ๊ณต์๋ฌธ์์์ ํด๋น ํด๋์ค์ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ํ ์ ์ํ๋ ๋ถ๋ถ์ด ๋์ ์์ผ๋ฏ๋ก ๊ทธ ๋ถ๋ถ์ ์ฐธ๊ณ ํ์์ด ์ฌ์ฉํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. ใ ใ https://docs.spring.io/spring-security/reference/5.8/migration/servlet/config.html @Bean public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception { return httpSecurity .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests((registry) -> registry.requestMatchers("/api/hello") .permitAll() .anyRequest().authenticated()) .build(); } - 0
- 2
- 1K
 
- Q&A - mysql ์ค์ ๋ก ์ค์ต์ - spring.jpa.defer-datasource-initialization= trueํด๋น ์ค์ ์ผ๋ก data.sql, schema.sql ์ sql ํ์ผ์ ์ฝ๋ ๊ฒ์ผ๋ก ์๊ณ ์์ต๋๋ค.ํ ์ด๋ธ ๋ง๋๋ ๋ถ๋ถ์ schema.sql๋ก ๋ฐ๋ก ๋ง๋ค๊ณ ๋ฐ์ดํฐ ๋ฃ๋ ๋ถ๋ถ์ ๊ทธ๋๋ก data.sql๋ก ๋ง๋ค์ด์ฃผ์ธ์. ๊ทธ๋ฆฌ๊ณ spring.jpa.hibernate.ddl-auto=create์ด ddl-auto ์ค์ ์ none์ผ๋ก ์ค์ ํ๊ณ ์คํํ๋ฉด ๋์ง ์์๊น.. ์๊ฐํฉ๋๋ค - 0
- 2
- 805
 




