Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. // org.springframework.cloud:spring-cloud-starter-oauth2
  2. // org.springframework.boot:spring-boot-starter-data-jpa
  3. // com.h2database:h2
  4. // redefine: spring-security.version == 4.1.0.RELEASE
  5.  
  6. package com.example
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired
  9. import org.springframework.boot.CommandLineRunner
  10. import org.springframework.boot.SpringApplication
  11. import org.springframework.boot.autoconfigure.SpringBootApplication
  12. import org.springframework.context.annotation.Bean
  13. import org.springframework.data.jpa.repository.JpaRepository
  14. import org.springframework.security.authentication.AuthenticationManager
  15. import org.springframework.security.core.authority.AuthorityUtils
  16. import org.springframework.security.core.userdetails.User
  17. import org.springframework.security.core.userdetails.UserDetailsService
  18. import org.springframework.security.core.userdetails.UsernameNotFoundException
  19. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer
  20. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter
  21. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer
  22. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer
  23. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer
  24. import org.springframework.web.bind.annotation.RequestMapping
  25. import org.springframework.web.bind.annotation.RestController
  26. import java.security.Principal
  27. import java.util.*
  28. import java.util.stream.Stream
  29. import javax.persistence.Entity
  30. import javax.persistence.GeneratedValue
  31. import javax.persistence.Id
  32.  
  33. @EnableResourceServer
  34. @EnableAuthorizationServer
  35. @SpringBootApplication
  36. @RestController
  37. open class KotlinAuthenticationServiceApplication :
  38. AuthorizationServerConfigurerAdapter() {
  39.  
  40. @Autowired
  41. lateinit var authenticationManager: AuthenticationManager
  42.  
  43. @Bean
  44. open fun userDetailsService(accountRepository: AccountRepository) = UserDetailsService {
  45. username ->
  46. accountRepository.findByUsername(username)
  47. .map { account ->
  48. User (account.username,
  49. account.password,
  50. account.active,
  51. account.active,
  52. account.active,
  53. account.active,
  54. AuthorityUtils.createAuthorityList("SCOPE_READ",
  55. "SCOPE_ADMIN"))
  56. }
  57. .orElseThrow {
  58. UsernameNotFoundException (
  59. "couldn't find the user ${username}!")
  60. }
  61. }
  62.  
  63. @Bean
  64. open fun sampleDataCLR(accountRepository: AccountRepository): CommandLineRunner =
  65. CommandLineRunner {
  66. Stream.of("jlong,spring", "pwebb,boot", "dsyer,cloud")
  67. .map { s -> s.split(",") }
  68. .forEach { tuple ->
  69. accountRepository.save(
  70. Account(tuple[0], tuple[1], true))
  71. }
  72. }
  73.  
  74. override fun configure(clients: ClientDetailsServiceConfigurer?) {
  75. clients
  76. ?.inMemory()
  77. ?.withClient("acme")
  78. ?.secret("acmesecret")
  79. ?.authorizedGrantTypes("password")
  80. ?.scopes("openid")
  81. }
  82.  
  83. override fun configure(endpoints: AuthorizationServerEndpointsConfigurer?) {
  84. endpoints?.authenticationManager(this.authenticationManager)
  85. }
  86.  
  87. @RequestMapping ("/user")
  88. open fun user(principal: Principal) = principal
  89.  
  90. }
  91.  
  92. fun main(args: Array<String>) {
  93. SpringApplication.run(KotlinAuthenticationServiceApplication::class.java, *args)
  94. }
  95.  
  96. interface AccountRepository : JpaRepository <Account, Long> {
  97.  
  98. fun findByUsername(username: String): Optional <Account>
  99. }
  100.  
  101. @Entity
  102. open class Account(var username: String ? = null,
  103. var password: String ? = null,
  104. var active: Boolean = false) {
  105.  
  106. constructor() : this(null, null, false)
  107.  
  108. @Id
  109. @GeneratedValue
  110. var id: Long = 0
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement