Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. package com.realdolmen.ticker.oauth;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
  10. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  11. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  12. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  13. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  14. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  15. import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
  16. import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
  17. import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  18.  
  19. /**
  20. * Created by SPLBD38 on 23/02/2017.
  21. */
  22. @Configuration
  23. @EnableAuthorizationServer
  24. public class OAuth2Config extends AuthorizationServerConfigurerAdapter
  25. {
  26. @Autowired
  27. private AuthenticationManager authenticationManager;
  28.  
  29. @Override
  30. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  31. throws Exception
  32. {
  33.  
  34. endpoints.authenticationManager(authenticationManager);
  35. endpoints.exceptionTranslator(loggingExceptionTranslator());
  36. }
  37.  
  38. @Bean
  39. public WebResponseExceptionTranslator loggingExceptionTranslator() {
  40. return new DefaultWebResponseExceptionTranslator() {
  41. @Override
  42. public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
  43. // This is the line that prints the stack trace to the log. You can customise this to format the trace etc if you like
  44. e.printStackTrace();
  45.  
  46. // Carry on handling the exception
  47. ResponseEntity<OAuth2Exception> responseEntity = super.translate(e);
  48. HttpHeaders headers = new HttpHeaders();
  49. headers.setAll(responseEntity.getHeaders().toSingleValueMap());
  50. OAuth2Exception excBody = responseEntity.getBody();
  51. return new ResponseEntity<>(excBody, headers, responseEntity.getStatusCode());
  52. }
  53. };
  54. }
  55.  
  56. @Override
  57. public void configure(AuthorizationServerSecurityConfigurer security) throws Exception
  58. {
  59. security.checkTokenAccess("isAuthenticated()");
  60. }
  61.  
  62. @Override
  63. public void configure(ClientDetailsServiceConfigurer clients) throws Exception
  64. {
  65. // @formatter:off
  66. clients.inMemory()
  67. .withClient("password")
  68. .secret("password")
  69. .authorizedGrantTypes("password", "refresh_token")
  70. .scopes("read", "write")
  71. .resourceIds("sparklr");
  72. // @formatter:on
  73. }
  74.  
  75. //curl -v localhost:8080/oauth/token -d "grant_type=password" -d "client_id=password-client" -d "client_secret=secret" -d "username=user" -d "password=90e4c1e9-3896-4edd-8da2-9a51310f6494"
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement