Instantly share code, notes, and snippets.
Created
April 9, 2018 00:14
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save mistergamarra/d414622374f3d516413d69ecec757ab8 to your computer and use it in GitHub Desktop.
AuthorizationServer for Oauth2 in Memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xploit.config.oauth2; | |
import javax.sql.DataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.security.authentication.AuthenticationManager; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |
import org.springframework.security.crypto.password.PasswordEncoder; | |
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.provider.error.DefaultWebResponseExceptionTranslator; | |
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; | |
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; | |
import org.springframework.security.oauth2.provider.token.DefaultTokenServices; | |
import org.springframework.security.oauth2.provider.token.TokenEnhancer; | |
import org.springframework.security.oauth2.provider.token.TokenStore; | |
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; | |
import com.xploit.manager.MessageManager; | |
@Configuration | |
@EnableAuthorizationServer | |
public class AuthorizationServerOAuth2Config extends AuthorizationServerConfigurerAdapter { | |
@Autowired | |
@Qualifier("userService") | |
private UserDetailsService userDetailsService; | |
@Autowired | |
private AuthenticationManager authenticationManager; | |
@Value("${gigy.oauth.tokenTimeout:3600}") | |
private int expiration; | |
@Bean | |
public PasswordEncoder passwordEncoder() { | |
return new BCryptPasswordEncoder(4); | |
} | |
@Override | |
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { | |
endpoints.tokenStore(tokenStore()) | |
.authenticationManager(this.authenticationManager) | |
.userDetailsService(userDetailsService) | |
.tokenEnhancer(tokenEnhancer()) | |
.exceptionTranslator(webResponseExceptionTranslator()); | |
} | |
@Autowired | |
DataSource dataSource; | |
@Override | |
public void configure(ClientDetailsServiceConfigurer clients) throws Exception { | |
clients.inMemory().withClient("artemisa") | |
.secret("artemisa2017") | |
.accessTokenValiditySeconds(expiration) | |
.scopes("read", "write") | |
.authorizedGrantTypes("password", "refresh_token") | |
.resourceIds("resource"); | |
} | |
@Bean | |
public TokenStore tokenStore() { | |
return new InMemoryTokenStore(); | |
} | |
@Bean | |
@Primary | |
public DefaultTokenServices tokenServices() { | |
DefaultTokenServices defaultTokenServices = new DefaultTokenServices(); | |
defaultTokenServices.setSupportRefreshToken(true); | |
defaultTokenServices.setTokenStore(tokenStore()); | |
defaultTokenServices.setTokenEnhancer(tokenEnhancer()); | |
return defaultTokenServices; | |
} | |
@Bean | |
public TokenEnhancer tokenEnhancer() { | |
return new CustomTokenEnhancer(); | |
} | |
@Bean | |
public WebResponseExceptionTranslator webResponseExceptionTranslator() { | |
return new DefaultWebResponseExceptionTranslator() { | |
@Autowired | |
MessageManager messageManager; | |
@Override | |
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception { | |
ResponseEntity<OAuth2Exception> responseEntity = super.translate(e); | |
OAuth2Exception body = responseEntity.getBody(); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.setAll(responseEntity.getHeaders().toSingleValueMap()); | |
System.out.println("Error Handler interceptor" + body.getMessage()); | |
String mensaje = messageManager.getMessage("MSJ-00081"); | |
body.addAdditionalInformation("mensaje", mensaje); | |
body.addAdditionalInformation("codigo", "0"); | |
return new ResponseEntity<>(body, headers, responseEntity.getStatusCode()); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment