web:framework:spring:jwt

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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:16] – [Configuration] jcheronweb:framework:spring:jwt [2025/08/12 02:35] (Version actuelle) – modification externe 127.0.0.1
Ligne 7: Ligne 7:
             <artifactId>spring-boot-starter-security</artifactId>             <artifactId>spring-boot-starter-security</artifactId>
         </dependency>         </dependency>
 +
 +        <dependency>
 +            <groupId>org.springframework.boot</groupId>
 +            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
 +        </dependency>        
 </sxh> </sxh>
  
Ligne 100: Ligne 105:
         val source = UrlBasedCorsConfigurationSource()         val source = UrlBasedCorsConfigurationSource()
         val config = CorsConfiguration()         val config = CorsConfiguration()
-        if (activeProfile == "dev") { +        config.allowedOrigins = allowedOrigins.split(","
-            config.allowedOrigins = allowedOrigins.split(","+        config.allowedMethods = listOf("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH", "HEAD"
-            config.allowedMethods = listOf("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH", "HEAD"+        config.allowedHeaders = listOf("*"
-            config.allowedHeaders = listOf("*"+        config.allowCredentials = true 
-            config.allowCredentials = true +        source.registerCorsConfiguration("/api/**", config)
-            source.registerCorsConfiguration("/api/**", config) +
-        }+
         return source         return source
     }     }
Ligne 118: Ligne 121:
 </sxh> </sxh>
  
 +===== RSA config =====
 +<sxh kotlin>
 +@ConfigurationProperties(prefix = "rsa")
 +@JvmRecord
 +data class RsaKeyConfigProperties(val publicKey: RSAPublicKey, val privateKey: RSAPrivateKey)
 +</sxh>
 +
 +
 +==== Génération des clés RSA ====
 +Avec git bash :
 +<sxh bash;gutter:false>
 +genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
 +</sxh>
 +
 +<sxh bash;gutter:false>
 +openssl rsa -in private.pem -pubout -out public.pem
 +</sxh>
 +==== AuthUser ====
 +<sxh kotlin>
 +class AuthUser(val user: User) : UserDetails {
 +
 +    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
 +        return mutableListOf(SimpleGrantedAuthority("ROLE_${user.role.name}"))
 +    }
 +
 +    override fun getPassword(): String? = user.password
 +
 +    override fun getUsername(): String? = user.username
 +
 +    override fun isAccountNonExpired(): Boolean = true
 +
 +    override fun isAccountNonLocked(): Boolean = true
 +
 +    override fun isCredentialsNonExpired(): Boolean = true
 +
 +    override fun isEnabled(): Boolean = user.enabled
 +}
 +</sxh>
  
 ===== Services ===== ===== Services =====
 +
 +<sxh kotlin>
 +@Service
 +class JpaUserDetailsService(
 +    val userRepository: UserRepository,
 +    val logEventRepository: LogEventRepository,
 +) : UserDetailsService {
 +
 +
 +    @Throws(UsernameNotFoundException::class)
 +    @Transactional
 +    override fun loadUserByUsername(usernameOrEmail: String): UserDetails {
 +        val user: User = userRepository
 +            .findByUsernameOrEmail(usernameOrEmail, usernameOrEmail)
 +            .orElseThrow { UsernameNotFoundException("User name or email not found: $usernameOrEmail") }
 +        return AuthUser(user)
 +    }
 +}
 +</sxh>
 +
 <sxh kotlin> <sxh kotlin>
 @Service @Service
  • web/framework/spring/jwt.1741936590.txt.gz
  • Dernière modification : il y a 8 semaines
  • (modification externe)