Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.realdolmen.ticker.oauth;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.ResponseEntity;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
- import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
- import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
- import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
- import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
- import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
- import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
- import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
- import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
- /**
- * Created by SPLBD38 on 23/02/2017.
- */
- @Configuration
- @EnableAuthorizationServer
- public class OAuth2Config extends AuthorizationServerConfigurerAdapter
- {
- @Autowired
- private AuthenticationManager authenticationManager;
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints)
- throws Exception
- {
- endpoints.authenticationManager(authenticationManager);
- endpoints.exceptionTranslator(loggingExceptionTranslator());
- }
- @Bean
- public WebResponseExceptionTranslator loggingExceptionTranslator() {
- return new DefaultWebResponseExceptionTranslator() {
- @Override
- public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
- // This is the line that prints the stack trace to the log. You can customise this to format the trace etc if you like
- e.printStackTrace();
- // Carry on handling the exception
- ResponseEntity<OAuth2Exception> responseEntity = super.translate(e);
- HttpHeaders headers = new HttpHeaders();
- headers.setAll(responseEntity.getHeaders().toSingleValueMap());
- OAuth2Exception excBody = responseEntity.getBody();
- return new ResponseEntity<>(excBody, headers, responseEntity.getStatusCode());
- }
- };
- }
- @Override
- public void configure(AuthorizationServerSecurityConfigurer security) throws Exception
- {
- security.checkTokenAccess("isAuthenticated()");
- }
- @Override
- public void configure(ClientDetailsServiceConfigurer clients) throws Exception
- {
- // @formatter:off
- clients.inMemory()
- .withClient("password")
- .secret("password")
- .authorizedGrantTypes("password", "refresh_token")
- .scopes("read", "write")
- .resourceIds("sparklr");
- // @formatter:on
- }
- //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"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement