์คํ๋ง ๋ถํธ 2 ๋ฒ์ ์ ์ฌ์ฉํ๋ค๊ฐ 3๋ก ๋ง์ด๊ทธ๋ ์ด์
ํ๊ฒ ๋๋ฉด ์ฌ๋ฌ๊ฐ์ง ์ด์๊ฐ ์๊ธฐ๊ฒ ๋๋ค. ํนํ๋ JDK17 ๊น์ง ์ฌ์ฉํ๊ฒ ๋๋ฉด์ ๊ธฐ์กด์ ์๋ ํจํค์ง ๊ฒฝ๋ก๋ฅผ ๋ชป ์ฐ๊ฒ ๋๋ ๊ฒฝ์ฐ๋ ๋ง๋ค.
์ฌ๋ฌ๊ฐ์ง ๋ฌธ์ ์ ์ ๋ํด์ ์ด๋ป๊ฒ ํด๊ฒฐํ๋์ง ๊ธฐ๋ก์ผ๋ก ๋จ๊ฒจ ๋์์ผ๊ฒ ๋ค.
๊ธฐ์กด | ๋ณ๊ฒฝ ํ | |
์คํ๋ง ๋ถํธ ๋ฒ์ | 2.7.17 | 3.0.11 |
์๋ฐ ๋ฒ์ | 8 | 17 |
JWT ๋ฒ์ | 0.9.1 | 0.11.5 |
Spring Security ๋ ๋ชจ๋ 6๋ฒ์ (6:3.1.1.RELEASE) ์ ์ฌ์ฉํ์๋ค.
thymeleaf์์ Spring Security๋ฅผ ์ฐ๋ ค๋ฉด springsecurity6๋ฅผ ์จ์ผ ํ๋ค.
๐ build.gradle
๋ณ๊ฒฝ ํ
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-security'
// Temporary explicit version to fix Thymeleaf bug
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6:3.1.1.RELEASE'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
WebSecurityConfig
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http)
throws Exception {
http.authorizeHttpRequests()
.requestMatchers("/", "/home", "/join", "/login")
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin().disable()
.csrf().disable()
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider()),
UsernamePasswordAuthenticationFilter.class);
return http.getOrBuild();
}
๋ณ๊ฒฝ ํ .authorizeRequests๊ฐ .authorizeHttpRequests๋ก ๋ฐ๋์๊ณ .antMatchers() ๋ฉ์๋๊ฐ ์ฌ๋ผ์ง๊ณ .requestMatchers() ๋ก ๋ณ๊ฒฝ ๋์๋ค.
.build()๋์ .getOrBuild() ๋ฅผ ์ฌ์ฉ
javax ํจํค์ง
JDK17๋ก ๋ณ๊ฒฝํ์๊ธฐ ๋๋ฌธ์ javax๊ฐ ์ฌ๋ผ์ง๊ณ ๊ธฐ๋ณธ์ผ๋ก jakarta ํจํค์ง๋ก ๋ณ๊ฒฝํด์ฃผ์ด์ผํฉ๋๋ค.
JWT ๋ฐ๊ธ
public class JwtTokenProvider {
private String secretKey = "sleep+mbti";
private long tokenValidTime = 30* 60 * 1000L; // 30min
private Key key;
@Autowired
private UserDetailsService userDetailsService;
// @PostConstruct
// public void init() {
// secretKey = Base64.getEncoder()
// .encodeToString(secretKey.getBytes());
// }
@PostConstruct
public void init() {
String base64EncodedSecretKey = Base64.getEncoder().encodeToString(secretKey.getBytes());
byte[] keyBytes = Decoders.BASE64.decode(base64EncodedSecretKey);
this.key = Keys.hmacShaKeyFor(keyBytes);
}
public String createToken(String username, Collection<? extends GrantedAuthority> roles) {
Claims claims = Jwts.claims().setSubject(username);
claims.put("roles", roles);
Date now = new Date();
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + tokenValidTime))
.signWith(key, SignatureAlgorithm.HS256)
//.signWith(secretKey, SignatureAlgorithm.HS256)
.compact();
}
}
jjwt 0.11.5๋ก ๋ฐ๋๋ฉด์ .signWitH() ๋ฉ์๋๊ฐ ๋ณ๊ฒฝ๋์๋ค. ๊ธฐ์กด์๋ ์๊ณ ๋ฆฌ์ฆ๊ณผ ์ธ์ฝ๋ฉ๋์ด ๋ฌธ์์ดํ ์ํจ, ๋น๋ฐํค ๋ฌธ์์ด์ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด์ฃผ์๋๋ฐ, 0.11.5 ๋ฒ์ ์์ ์ธ์ฝ๋ฉ๋ Key ๊ฐ์ฒด์ ์๊ณ ๋ฆฌ์ฆ ์์๋ก ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค.
Key๊ฐ์ฒด์ ํ๋ผ๋ฏธํฐ ์์ ๋ณ๊ฒฝ์ ์ด ์๊ฒผ๋ค.
ํ์ง๋ง ๋ณ๊ฒฝ ํ์๋ ์คํํ๋ฉด ์ด๋ฐ ์ค๋ฅ๊ฐ ๋์ฌ ๊ฒ์ด๋ค.
Caused by: io.jsonwebtoken.security.WeakKeyException: The specified key byte array is 128 bits which is not secure enough for any JWT HMAC - SHA algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HMAC-SHA algorithms MUST have a size >= 256 bits (the key size must be greater than or equal to the hash output size). Consider using the io.jsonwebtoken.security.Keys#secretKeyFor(SignatureAlgorithm) method to create a key guaranteed to be secure enough for your preferred HMAC-SHA algorithm. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.
์ด ์ค๋ฅ๋ ๋ฐ๋ก ์ ๊ณผ๋ ๋ค๋ฅด๊ฒ ์ด์ ๋น๋ฐํค์ ๋ฐ์ดํธ ํฌ๊ธฐ ์ ํ์ด ์๊ฒจ์ ๋ํ๋๋ ์ค๋ฅ์ด๋ค. ๊ธฐ์กด์๋ ์งง์ ๋ฌธ์์ด์ ์ํฌ๋ฆฟ ํค์ฌ๋ ์๋ํ์ง๋ง, ์ด์ ๋๋ฌด ์งง์ผ๋ฉด ์ฝํ ํค๋ผ๋ ์ค๋ฅ๊ฐ ํฐ์ ธ์ ์คํ์ด ์๋๋ค.
์ฝ๊ฒ ๋งํด ์ฐ๋ฆฌ๊ฐ ์ด๋ค ์น์ฌ์ดํธ ํ์๊ฐ์ ํ๋ คํ ๋ ๋น๋ฐ๋ฒํธ 6๊ธ์๋ฅผ ์น๋ฉด ๋น๋ฐ๋ฒํธ๋ 10๊ธ์ ์ด์์ด์ฌ์ผํฉ๋๋ค. ๋ผ๋ ์ค๋ฅ๊ฐ ๋์ค๋ฉด์ ํ์๊ฐ์ ์ ๋ชปํ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
์์ด๋ 1๊ธ์๋น 1๋ฐ์ดํธ์ด๊ณ , ํ๊ธ์ 1๊ธ์๋น 3๋ฐ์ดํธ ๋๊น ํ๊ธ๋ก ํด์ ๋ฐ์ดํธ ์๋ฅผ ๋๋ ค๋ณด์๋ค.
private String secretKey = "ใ
ใ
ฃ๋จธ๋ใ
์ ใ
ใ
๋ฐใ
ฃ๋ฌใ
๋จธใ
ใ
ใ
ฃใ
๋๋ฆฌใ
๋;ใ
๋จธ๋ฌใ
ฃ๋๋ฌใ
ฃ๋ฌ์ฌ๋ฌ์ฌ๋ฌ๋ใ
๋ฆฌ๋ใ
ใท๋ดใ
ํธ๋งใ
ฃใ
ํใ
ฃ;ํ์ดใ
";
๋ค์๊ณผ ๊ฐ์ด ๋น๋ฐํค๋ฅผ ๊ธธ๊ฒ ํ์ฌ ๋ค์ ์คํํ๋ ์ ์ ์๋ํ์๋ค.
๋๊ธ