Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. @Configuration
  2. @EnableAuthorizationServer
  3. protected static class AuthorizationServerConfiguration extends
  4. AuthorizationServerConfigurerAdapter {
  5.  
  6. @Autowired
  7. private DataSource dataSource;
  8.  
  9. private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  10.  
  11. @Autowired
  12. private AuthenticationManager authenticationManager;
  13.  
  14. @Bean
  15. public JdbcTokenStore tokenStore(){
  16. return new JdbcTokenStore(dataSource);
  17. }
  18.  
  19. @Bean
  20. protected AuthorizationCodeServices authorizationCodeServices(){
  21. return new JdbcAuthorizationCodeServices(dataSource);
  22. }
  23.  
  24. @Override
  25. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  26. throws Exception {
  27. endpoints
  28. .tokenStore(new JdbcTokenStore(dataSource))
  29. .authenticationManager(authenticationManager);
  30. }
  31.  
  32. @Override
  33. public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  34. oauthServer.passwordEncoder(passwordEncoder);
  35. }
  36.  
  37. @Override
  38. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  39. clients.jdbc(dataSource);
  40. }
  41.  
  42. private static final String RESOURCE_ID = "test";
  43.  
  44. @Configuration
  45. @EnableResourceServer
  46. protected static class ResourceServerConfiguration extends
  47. ResourceServerConfigurerAdapter {
  48.  
  49.  
  50. @Autowired
  51. private DataSource dataSource;
  52.  
  53. @Autowired
  54. private TokenStore tokenStore;
  55.  
  56. @Override
  57. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  58. resources.tokenStore(tokenStore).resourceId(RESOURCE_ID);
  59. }
  60.  
  61. @Bean
  62. public TokenStore tokenStore() {
  63. return new JdbcTokenStore(dataSource);
  64. }
  65.  
  66. @Override
  67. public void configure(HttpSecurity http) throws Exception {
  68. http.authorizeRequests().anyRequest().authenticated();
  69. }
  70.  
  71. }
  72. }
  73.  
  74. @RequestMapping(value = "/transaction", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces =MediaType.APPLICATION_JSON_VALUE)
  75.  
  76.  
  77. public Map<String, Object> haloApi(@RequestBody(required = false) Map<String, String> input){Map<String, Object> data = new HashMap<>();
  78. data.put("kwame", new Date());
  79. if(input != null){
  80. String name = input.get("test");
  81. if(name != null && !name.isEmpty()){
  82. data.put("nice", "Hello "+name);
  83. }
  84. }
  85.  
  86. return data;
  87. }
  88.  
  89. $ curl -X POST -H "Accept: application/json" -d "grant_type=client_credentials" -u "daniel:123456" "http://localhost:5600/oauth/token"
  90.  
  91. {
  92. "access_token": "cddc1b75-87d9-4a2f-9d66-210eae85b0f9",
  93. "token_type": "bearer",
  94. "expires_in": 149,
  95. "scope": "read write"
  96. }
  97.  
  98. curl -X POST http://localhost:5700/checkout/transaction -v -H 'Content-Type: application/json' -H 'Authorization: Bearer ac72b34f-437d-4134-8760-16f1ca3f0483' -d '{"test": "test"}'
  99.  
  100. {
  101. "error": "invalid_token",
  102. "error_description": "ac72b34f-437d-4134-8760-16f1ca3f0483"
  103. }
  104.  
  105. 2017-01-08 18:45:36.375 DEBUG 11012 --- [io-22000-exec-1] o.s.s.oauth2.client.OAuth2RestTemplate : GET request for "ip&port/oauth/check_token" resulted in 401 (null); invoking error handler
  106.  
  107. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxxxx
  108. spring.datasource.username=xxxxxxxxx
  109. spring.datasource.password=xxxxxxxxx
  110. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  111. spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
  112. server.port=5700
  113. security.oauth2.resource.user-info-uri=localhost:5600/oauth/check_token
  114. logging.level.org.springframework.security=DEBUG
  115.  
  116. resources.tokenStore(tokenStore).resourceId(RESOURCE_ID);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement