Beruflich Dokumente
Kultur Dokumente
• Pequenos
• Deployment interdependente
• Independente de tecnologia
• Infra-estrutura separada
Segurança
Autenticação Autorização
Principais requisitos de segurança?
“Stop bad guys from accessing your resources"
Resource
Server
Important Stuff
OAuth 2.0
• Protocolo baseado em uma especificação de padrão
aberto definido pelo IETF
• Habilita as aplicações acessarem e compartilharem
serviços sem necessidade de compartilhar credenciais
• Evita problemas com "passwords"
• Essencial para mecanismo via delegação de acesso
• Aplicações terceiras
• Para serviços específicos
• Por um tempo limitado
• Pode trabalhar com revogação seletiva
OAuth 2.0 Flow
OAuth 2.0 Grant Types
• Authorization Code (web apps)
• Confidencialidade aos clientes
• Utiliza um código de autorização emitido pelo servidor
• Implicit (browser-based and mobile apps)
• Script heavy web apps
• Usuário final pode ver o access token gerado
• Resource Owner Password Credentials (user / password)
• Utilizado em casos aonde o usuário confia no cliente
• Expõe as credenciais do usuário para o cliente
• Client Credentials (application)
• Clientes recebem um token (secret) para acesso
• Ideal para acesso entre aplicações
Authorization Server
@Configuration
@EnableAuthorizationServer
public class OAuth2Server extends AuthorizationServerConfigurerAdapter {
@Autowired
TokenStore tokenStore;
@Override
public void configure(ClientDetailsServiceConfigurer clients)
throws Exception {
clients.inMemory()
.withClient("client").secret("secret")
.authorizedGrantTypes(“authorization_code",
"refresh_token", "password",
"implicit", "client_credentials").scopes("openid");
}
}
Resource Server
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/products/**").authenticated();
}
}
JSON Web Token
“Padrão aberto que define uma forma
compacta e auto-contida para transmitir de
forma segura, informações entre duas partes“
JWT Configuration
@Configuration
public class JwtConfig {
@Autowired
JwtAccessTokenConverter jwtAccessTokenConverter;
@Bean
@Qualifier("tokenStore")
public TokenStore tokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter);
}
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
KeyPair keyPair = new KeyStoreKeyFactory(
new ClassPathResource("keystore.jks"),
"foobar".toCharArray()).getKeyPair("test");
converter.setKeyPair(keyPair);
return converter;
}
}
JWT Response
{
"access_token":
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTMyODMxNzksInVzZXJfbmFtZSI6InVzZXI
iLCJhdXRob3JpdGllcyI6WyJVU0VSIiwiTUFOQUdFUiJdLCJqdGkiOiJkMGY5OTY2OC01NzdkLTRkZTEtODkw
Yi1hNDY1MTBkZjg2YjAiLCJjbGllbnRfaWQiOiJjbGllbnQiLCJzY29wZSI6WyJvcGVuaWQiXX0.iH1pnwJZP
Zi05hdpY9MDGIvtx34Dj8lxc5fdU5c5NCCtUblT_L9kdZO6NaOIIZffbGzSHoyVUEZkSwkGXm6lT1jRTcOHq2
khAZlwmO3hN3c1xb8bumAgmpF8fJSIKTVIkFJpbVO4uDfHSSbBm6QsTbqHkNgNwWSWbNG1n6ZlsHCcZCh37cm
gbh-B4tPD9QEfH3CSI6Z7AgUbS9UCIytjm02sgxgAr3liOcykRrdcOvxgIBx_yGDvornQ5JOBVdW-TS0-
uJmHe6sHCFYeBNchJhRi7xqZCMYFD6IcP4dftPupzg3IMl5oWberxhZTCCLoi18JtQyZgIgqmSlOAIq8wg",
"token_type": "bearer",
"refresh_token":
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJ1c2VyIiwic2NvcGUiOlsib3Blbml
kIl0sImF0aSI6ImQwZjk5NjY4LTU3N2QtNGRlMS04OTBiLWE0NjUxMGRmODZiMCIsImV4cCI6MTQ5NTgzMTk3
OSwiYXV0aG9yaXRpZXMiOlsiVVNFUiIsIk1BTkFHRVIiXSwianRpIjoiOWVjNWQwY2QtYzhiNy00ZDJmLWFjZ
GQtMGFhMDNhYThjNWM5IiwiY2xpZW50X2lkIjoiY2xpZW50In0.fXSafC9Fuj7rKnDIau9vOmIU4Bn11tLUgx
_5sMOvzFW1sZtzdmJ8cZ7vMJc34Gu9F_gYtBa-
AczYPNFp6ZL2aWgUXllVekSpfCgqPqruTjX2IZdJFk0_4koVAfCpgg3CS88_joQhi3hU0vnXpvse2waA7yT-
zoytt0kbeh_FPsYkTsmZjgmeEyyJqFlxJ0ihSGqvMwCUnPKDz9uZxgvgv_mV_2dXX6PYuDAejIjeHkTCnLeT_
i1jH5BtJbLzBliIu2XxdyXzpwgJRnUHNkLWIih0UvJZrDAETTVEIbqm58AG9l-d7c1T-3idTU-
XC09VZsaL1BQnzp0FzqHXmIY5Og",
"expires_in": 43199,
"scope": "openid",
"jti": "d0f99668-577d-4de1-890b-a46510df86b0"
}
Demo
• Segurança com Microservices
• https://github.com/rcandidosilva/spring-cloud-sample
Perguntas
?
Conclusões…
• Segurança é importante, e não deve ser intrusiva
• Requisitos de segurança em microservices é complexo
• OAuth 2 e JWT oferecem uma boa combinação para
implementação de segurança com serviços
• Spring Cloud Security fornece uma ótima infra-estrutura
para configuração de segurança em uma arquitetura de
microservices
• Enjoy it ;)
Referências
• https://cloud.spring.io/spring-cloud-security/
• https://oauth.net/2/
• https://jwt.io/
• http://presos.dsyer.com/decks/microservice-security.html
• https://github.com/absolutegalaber/jwt-oauth2-example
• http://www.baeldung.com/spring-security-oauth-jwt
• https://stormpath.com/blog/service-to-service
Muito Obrigado!
@rcandidosilva
rodrigocandido.me