Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableAuthorizationServer
- protected static class AuthorizationServerConfiguration extends
- AuthorizationServerConfigurerAdapter {
- @Autowired
- private DataSource dataSource;
- private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
- @Autowired
- private AuthenticationManager authenticationManager;
- @Bean
- public JdbcTokenStore tokenStore(){
- return new JdbcTokenStore(dataSource);
- }
- @Bean
- protected AuthorizationCodeServices authorizationCodeServices(){
- return new JdbcAuthorizationCodeServices(dataSource);
- }
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints)
- throws Exception {
- endpoints
- .tokenStore(new JdbcTokenStore(dataSource))
- .authenticationManager(authenticationManager);
- }
- @Override
- public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
- oauthServer.passwordEncoder(passwordEncoder);
- }
- @Override
- public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
- clients.jdbc(dataSource);
- }
- private static final String RESOURCE_ID = "test";
- @Configuration
- @EnableResourceServer
- protected static class ResourceServerConfiguration extends
- ResourceServerConfigurerAdapter {
- @Autowired
- private DataSource dataSource;
- @Autowired
- private TokenStore tokenStore;
- @Override
- public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
- resources.tokenStore(tokenStore).resourceId(RESOURCE_ID);
- }
- @Bean
- public TokenStore tokenStore() {
- return new JdbcTokenStore(dataSource);
- }
- @Override
- public void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests().anyRequest().authenticated();
- }
- }
- }
- @RequestMapping(value = "/transaction", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces =MediaType.APPLICATION_JSON_VALUE)
- public Map<String, Object> haloApi(@RequestBody(required = false) Map<String, String> input){Map<String, Object> data = new HashMap<>();
- data.put("kwame", new Date());
- if(input != null){
- String name = input.get("test");
- if(name != null && !name.isEmpty()){
- data.put("nice", "Hello "+name);
- }
- }
- return data;
- }
- $ curl -X POST -H "Accept: application/json" -d "grant_type=client_credentials" -u "daniel:123456" "http://localhost:5600/oauth/token"
- {
- "access_token": "cddc1b75-87d9-4a2f-9d66-210eae85b0f9",
- "token_type": "bearer",
- "expires_in": 149,
- "scope": "read write"
- }
- 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"}'
- {
- "error": "invalid_token",
- "error_description": "ac72b34f-437d-4134-8760-16f1ca3f0483"
- }
- 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
- spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxxxx
- spring.datasource.username=xxxxxxxxx
- spring.datasource.password=xxxxxxxxx
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
- server.port=5700
- security.oauth2.resource.user-info-uri=localhost:5600/oauth/check_token
- logging.level.org.springframework.security=DEBUG
- resources.tokenStore(tokenStore).resourceId(RESOURCE_ID);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement