Guest User

Untitled

a guest
Jan 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. curl -X POST -d "client_id=client-id&client_secret=secret&grant_type=password&username=demo&password=1234" http://localhost:8080/oauth/token
  2.  
  3. {"timestamp":"2018-01-25T14:47:42.286+0000","status":401,"error":"Unauthorized","message":"Unauthorized","path":"/oauth/token"}
  4.  
  5. @Configuration
  6. @EnableAuthorizationServer
  7. class AuthorizationServerConfiguration : AuthorizationServerConfigurerAdapter() {
  8. @Autowired
  9. private val tokenStore: TokenStore? = null
  10.  
  11. @Autowired
  12. private val userApprovalHandler: UserApprovalHandler? = null
  13.  
  14. @Autowired
  15. @Qualifier("authenticationManagerBean")
  16. private val authenticationManager: AuthenticationManager? = null
  17.  
  18. @Throws(Exception::class)
  19. override fun configure(clients: ClientDetailsServiceConfigurer?) {
  20. clients!!.inMemory()
  21. .withClient("client-id")
  22. .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
  23. .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
  24. .scopes("read", "write", "trust")
  25. .secret("secret")
  26. .accessTokenValiditySeconds(120)//Access token is only valid for 2 minutes.
  27. .refreshTokenValiditySeconds(600)//Refresh token is only valid for 10 minutes.
  28. }
  29.  
  30. @Throws(Exception::class)
  31. override fun configure(endpoints: AuthorizationServerEndpointsConfigurer?) {
  32. endpoints!!.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
  33. .authenticationManager(authenticationManager)
  34. }
  35.  
  36. @Throws(Exception::class)
  37. override fun configure(oauthServer: AuthorizationServerSecurityConfigurer?) {
  38. oauthServer!!.realm(REALM + "/client")
  39. }
  40.  
  41. companion object {
  42. private val REALM = "MY_OAUTH_REALM"
  43. }
  44. }
  45.  
  46. @Configuration
  47. @EnableResourceServer
  48. class ResourceServerConfiguration : ResourceServerConfigurerAdapter() {
  49.  
  50. override fun configure(resources: ResourceServerSecurityConfigurer?) {
  51. resources!!.resourceId(RESOURCE_ID).stateless(false)
  52. }
  53.  
  54. @Throws(Exception::class)
  55. override fun configure(http: HttpSecurity) {
  56. http.anonymous().disable()
  57. .requestMatchers().antMatchers("/users/**")
  58. .and().authorizeRequests()
  59. .antMatchers("/users/**").access("hasRole('ADMIN')")
  60. .and().exceptionHandling().accessDeniedHandler(OAuth2AccessDeniedHandler())
  61. }
  62.  
  63. companion object {
  64. private val RESOURCE_ID = "my_rest_api"
  65. }
  66.  
  67. }
  68.  
  69. @Configuration
  70. @EnableWebSecurity
  71. class OAuth2SecurityConfiguration : WebSecurityConfigurerAdapter() {
  72.  
  73. @Autowired
  74. private val clientDetailsService: ClientDetailsService? = null
  75.  
  76. @Autowired
  77. @Throws(Exception::class)
  78. fun globalUserDetails(auth: AuthenticationManagerBuilder) {
  79. auth.inMemoryAuthentication()
  80. .withUser("bill").password("abc123").roles("ADMIN").and()
  81. .withUser("demo").password("1234").roles("USER")
  82. }
  83.  
  84. @Throws(Exception::class)
  85. override fun configure(http: HttpSecurity) {
  86. http
  87. .csrf().disable()
  88. .anonymous().disable()
  89. .authorizeRequests()
  90. .antMatchers("/oauth/token").permitAll()
  91. }
  92.  
  93. @Bean
  94. @Throws(Exception::class)
  95. override fun authenticationManagerBean(): AuthenticationManager {
  96. return super.authenticationManagerBean()
  97. }
  98.  
  99.  
  100. @Bean
  101. fun tokenStore(): TokenStore {
  102. return InMemoryTokenStore()
  103. }
  104.  
  105. @Bean
  106. @Autowired
  107. fun userApprovalHandler(tokenStore: TokenStore): TokenStoreUserApprovalHandler {
  108. val handler = TokenStoreUserApprovalHandler()
  109. handler.setTokenStore(tokenStore)
  110. handler.setRequestFactory(DefaultOAuth2RequestFactory(clientDetailsService))
  111. handler.setClientDetailsService(clientDetailsService)
  112. return handler
  113. }
  114.  
  115. @Bean
  116. @Autowired
  117. @Throws(Exception::class)
  118. fun approvalStore(tokenStore: TokenStore): ApprovalStore {
  119. val store = TokenApprovalStore()
  120. store.setTokenStore(tokenStore)
  121. return store
  122. }
  123. }
Add Comment
Please, Sign In to add comment