Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
web:framework:spring:jwt [2025/03/14 08:14] – jcheron | web:framework:spring:jwt [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 7: | Ligne 7: | ||
< | < | ||
</ | </ | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
</ | </ | ||
Ligne 100: | Ligne 105: | ||
val source = UrlBasedCorsConfigurationSource() | val source = UrlBasedCorsConfigurationSource() | ||
val config = CorsConfiguration() | val config = CorsConfiguration() | ||
- | | + | config.allowedOrigins = allowedOrigins.split("," |
- | | + | config.allowedMethods = listOf(" |
- | config.allowedMethods = listOf(" | + | config.allowedHeaders = listOf(" |
- | config.allowedHeaders = listOf(" | + | config.allowCredentials = true |
- | config.allowCredentials = true | + | source.registerCorsConfiguration("/ |
- | source.registerCorsConfiguration("/ | + | |
- | } | + | |
return source | return source | ||
} | } | ||
Ligne 118: | Ligne 121: | ||
</ | </ | ||
+ | ===== RSA config ===== | ||
+ | <sxh kotlin> | ||
+ | @ConfigurationProperties(prefix = " | ||
+ | @JvmRecord | ||
+ | data class RsaKeyConfigProperties(val publicKey: RSAPublicKey, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Génération des clés RSA ==== | ||
+ | Avec git bash : | ||
+ | <sxh bash; | ||
+ | genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits: | ||
+ | </ | ||
+ | |||
+ | <sxh bash; | ||
+ | openssl rsa -in private.pem -pubout -out public.pem | ||
+ | </ | ||
+ | ==== AuthUser ==== | ||
+ | <sxh kotlin> | ||
+ | class AuthUser(val user: User) : UserDetails { | ||
+ | |||
+ | override fun getAuthorities(): | ||
+ | return mutableListOf(SimpleGrantedAuthority(" | ||
+ | } | ||
+ | |||
+ | override fun getPassword(): | ||
+ | |||
+ | override fun getUsername(): | ||
+ | |||
+ | override fun isAccountNonExpired(): | ||
+ | |||
+ | override fun isAccountNonLocked(): | ||
+ | |||
+ | override fun isCredentialsNonExpired(): | ||
+ | |||
+ | override fun isEnabled(): | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Services ===== | ||
+ | |||
+ | <sxh kotlin> | ||
+ | @Service | ||
+ | class JpaUserDetailsService( | ||
+ | val userRepository: | ||
+ | val logEventRepository: | ||
+ | ) : UserDetailsService { | ||
+ | |||
+ | |||
+ | @Throws(UsernameNotFoundException:: | ||
+ | @Transactional | ||
+ | override fun loadUserByUsername(usernameOrEmail: | ||
+ | val user: User = userRepository | ||
+ | .findByUsernameOrEmail(usernameOrEmail, | ||
+ | .orElseThrow { UsernameNotFoundException(" | ||
+ | return AuthUser(user) | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <sxh kotlin> | ||
+ | @Service | ||
+ | class AuthService { | ||
+ | |||
+ | @Autowired | ||
+ | lateinit var jwtEncoder: JwtEncoder | ||
+ | |||
+ | @Autowired | ||
+ | lateinit var JwtDecoder: JwtDecoder | ||
+ | |||
+ | @Autowired | ||
+ | lateinit var passwordEncoder: | ||
+ | |||
+ | @Autowired | ||
+ | lateinit var userRepository: | ||
+ | |||
+ | fun generateToken(authentication: | ||
+ | val now = Instant.now() | ||
+ | |||
+ | val scope: String = authentication.getAuthorities() | ||
+ | .stream() | ||
+ | .map { obj: GrantedAuthority -> obj.authority } | ||
+ | .collect(Collectors.joining(" | ||
+ | val user = (authentication.principal as AuthUser).user | ||
+ | val claims = JwtClaimsSet.builder() | ||
+ | .issuer(" | ||
+ | .issuedAt(now) | ||
+ | .expiresAt(now.plus(10, | ||
+ | .subject(authentication.getName()) | ||
+ | .claim(" | ||
+ | .claim(" | ||
+ | .claim(" | ||
+ | .claim(" | ||
+ | .build() | ||
+ | |||
+ | return jwtEncoder.encode(JwtEncoderParameters.from(claims)).tokenValue | ||
+ | } | ||
+ | |||
+ | fun getActiveUser(token: | ||
+ | val claims = JwtDecoder.decode(token).claims | ||
+ | val userId = claims[" | ||
+ | return userRepository.findById(userId).orElseThrow { RuntimeException(" | ||
+ | } | ||
+ | |||
+ | fun hashPassword(password: | ||
+ | if (!isBCryptHash(password)) { | ||
+ | return passwordEncoder.encode(password) | ||
+ | } | ||
+ | return password | ||
+ | } | ||
+ | |||
+ | fun isBCryptHash(password: | ||
+ | return password.matches(Regex(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ |