SlideShare a Scribd company logo
Securing RESTful 
Resources with OAuth2 
Rodrigo Cândido da Silva 
@rcandidosilva 
JavaOne 2014 
CON4990
About Me 
• Brazilian guy ;) 
• Software Architect 
• Java Platform 
• Work for Integritas Tech 
• http://integritastech.com 
• JUG Leader of GUJavaSC 
• http://gujavasc.org 
• Twitter 
• @rcandidosilva 
• Personal 
• http://rodrigocandido.me
Agenda 
• Why use OAuth2? 
• OAuth2 concepts 
• Grant types 
• OAuth2 Tokens 
• Java Implementations 
• Demo
Public Web Service API’s
Security 
Closed 
Closed 
Open 
Authentication Authorization
Securing APIs 
• Securing resources strategies 
• Basic Auth (HTTP Basic) 
• Sending user credentials in http authentication header 
• Mutual Authentication (HTTP Digest) 
• Based on certificates, server authenticate to client, client to server 
• RESTful architecture not defines security procedures 
• HTTP methods: GET, POST, PUT, DELETE 
• REST API’s are equal vulnerable as standard web apps 
• Injection attacks, replay attacks, cross-site scripting, etc.
Without OAuth
With OAuth
Why OAuth 
• Open standard protocol specification defined by IETF 
• Enables applications to access each other’s data without 
sharing credentials 
• Avoid password issues 
• User and password authentication is fine, but what if your API 
needs to be used by other apps? 
• Required for delegating access 
• Third party applications 
• For specified resource 
• For limited time 
• Can be selectively be revoked
Who is using OAuth
OAuth Timeline 
• OAuth 1.0 
• Core specification published in Dec 2007 
• OAuth 1.0a 
• Revised specification published in June 2009 
• Related to fix a security issue 
• OAuth 2.0 
• Standardized since Oct-2012 
• Be more secure, simple, and standard 
• Additional RFCs are still being worked on
OAuth2 
• No username or passwords (only tokens) 
• Protocol for authorization – not authentication 
• Delegated model 
• Fix the password anti-pattern 
• Trust relationship between resource, identity server and client app 
• Goal was simplicity 
• Relies heavily on TLS/SSL 
• Not backwards compatible 
• Easily to revoke
OAuth2 Roles 
• Resource Owner 
• Entity capable of granting 
access to a protected resource 
• Client Application 
• Application making protected resource requests on behalf of 
the resource owner 
• Resource Server 
• The server hosting the protected resources 
• Authorization Server 
• The server issuing access tokens to the clients
OAuth2 Basic Flow
OAuth2 Grant Types 
• Authorization Code (web apps) 
• Optimized for confidential clients 
• Uses a authorization code from the server 
• Implicit (browser-based and mobile apps) 
• Optimized for script heavy web apps 
• User can see the access token 
• Resource Owner Password Credentials (user / password) 
• Used in cases where the user trusts the client 
• Exposes user credentials to the client 
• Client Credentials (application) 
• Clients gets an access token based on client credentials only
Authorization Code
Implicit
Resource Owner Password Credentials
Client Credentials
OAuth2 Tokens 
• Types 
• Bearer 
• Large random token 
• Need SSL to protect it in transit 
• Server needs to store it securely hashed like a user password 
• Mac 
• Uses a nonce to prevent replay 
• Does not required SSL 
• OAuth 1.0 only supported 
• Access Token 
• Short-lived token 
• Refresh Token 
• Long-lived token 
{ 
"access_token":"2YotnFZFEjr1zCsicMWpAA", 
"token_type":“bearer", 
"expires_in":3600, 
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", 
}
OAuth2 Pros & Cons 
• Pros 
• Integration of third party apps to any sites 
• Access can be granted for limited scope or duration 
• No need for users to give password on third party 
site 
• Cons 
• Writing an authorization server is somewhat 
complex 
• Interoperability issues 
• Bad implementations can be security issues
OAuth2 Java Implementations 
• Some Java implementations available 
• Jersey 
• Apache Oltu 
• Spring Security OAuth2 
• And others: CXF, Google OAuth2 API, etc 
• Not available as Java EE standard yet
Jersey 
• Open source RESTful Web services framework 
• The JAX-RS reference implementation 
• Integrates with the Java EE standard security 
• @RolesAllowed 
• @PermitAll 
• @DenyAll 
• Supports entity filtering features 
• @EntityFiltering 
• Only supports OAuth2 at client side :/
Jersey 
Java EE security integration 
@Path("restricted-resource") 
@Produces("application/json") 
public class RestrictedResource { 
@GET @Path(”denyAll") 
@DenyAll 
public RestrictedEntity denyAll() { ... } 
@GET @Path("rolesAllowed") 
@RolesAllowed({"manager"}) 
public RestrictedEntity rolesAllowed() { ... } 
}
Jersey 
OAuth2 client support 
OAuth2CodeGrantFlow.Builder builder = 
OAuth2ClientSupport 
.authorizationCodeGrantFlowBuilder( 
clientId, 
"https://example.com/oauth/authorization", 
"https://example.com/oauth/token"); 
OAuth2CodeGrantFlow flow = builder.property( 
OAuth2CodeGrantFlow.Phase.AUTHORIZATION, 
"readOnly", "true") 
.scope("contact") 
.build(); 
String authorizationUri = flow.start(); 
... 
final TokenResult result = flow.finish(code, state); 
...
Apache Oltu 
• Apache OAuth protocol implementation 
• It also covers others related implementations 
• JSON Web Token (JWT) 
• JSON Web Signature (JWS) 
• OpenID Connect 
• Supports the full OAuth2 features 
• Authorization Server 
• Resource Server 
• Client 
• Provides predefined OAuth2 client types 
• Facebook, Foursquare, Github, Google, etc 
• Still being improved…
Apache Oltu 
Authorization endpoint 
protected void doGet(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException { 
//dynamically recognize an OAuth profile and perform validation 
OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request); 
validateRedirectionURI(oauthRequest) 
//build OAuth response 
OAuthResponse resp = OAuthASResponse 
.authorizationResponse(HttpServletResponse.SC_FOUND) 
.setCode(oauthIssuerImpl.authorizationCode()) 
.location(ex.getRedirectUri()) 
.buildQueryMessage(); 
response.sendRedirect(resp.getLocationUri()); 
}
Apache Oltu 
Token endpoint 
protected void doPost(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException { 
OAuthIssuer oauthIssuerImpl = 
new OAuthIssuerImpl(new MD5Generator()); 
OAuthTokenRequest oauthRequest = 
new OAuthTokenRequest(request); 
validateClient(oauthRequest); 
String authzCode = oauthRequest.getCode(); 
String accessToken = oauthIssuerImpl.accessToken(); 
String refreshToken = oauthIssuerImpl.refreshToken(); 
OAuthResponse r = OAuthASResponse(...); 
}
Apache Oltu 
Protecting the resources 
protected void doGet(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException { 
// Make the OAuth Request and validate it 
OAuthAccessResourceRequest oauthRequest = new 
OAuthAccessResourceRequest(request, 
ParameterStyle.BODY); 
// Get the access token 
String accessToken = 
oauthRequest.getAccessToken(); 
//... validate access token 
}
Apache Oltu 
OAuth2 client 
OAuthClientRequest request = OAuthClientRequest 
.tokenProvider(OAuthProviderType.FACEBOOK) 
.setGrantType(GrantType.AUTHORIZATION_CODE) 
.setClientId("your-facebook-application-client-id") 
.setClientSecret("your-facebook-application-client-secret") 
.setRedirectURI("http://www.example.com/redirect") 
.setCode(code) 
.buildQueryMessage(); 
//create OAuth client that uses custom http client under the hood 
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); 
OAuthAccessTokenResponse oAuthResponse = 
oAuthClient.accessToken(request); 
String accessToken = oAuthResponse.getAccessToken(); 
String expiresIn = oAuthResponse.getExpiresIn();
Spring Security OAuth 
• Provides OAuth (1a) and OAuth2 support 
• Implements 4 types of authorization grants 
• Supports the OAuth2 full features 
• Authorization Server 
• Resources Server 
• Client 
• Good integration with JAX-RS and Spring MVC 
• Configuration using annotation support 
• Integrates with the Spring ecosystem
Spring Authorization Server 
• @EnableAuthorizationServer 
• Annotation used to configure OAuth2 authorization server 
• There is also XML configuration related <authorization-server/> 
• ClientDetailsServiceConfigurer 
• Defines the client details service 
• In-memory or JDBC implementation 
• AuthorizationServerTokenServices 
• Operations to manage OAuth2 tokens 
• Tokens in-memory, JDBC or JSON Web Token (JWT) 
• AuthorizationServerEndpointConfigurer 
• Grant types supported by the server 
• All grant types are supported except password types
Spring Resource Server 
• Can be the same as Authorization Server 
• Or deployed in a separate application 
• Provides a authentication filter for web protection 
• @EnableResourceServer 
• Annotation used to configure OAuth2 resource server 
• There is also XML configuration related <resource-server/> 
• Supports expression-based access control 
• #oauth2.clientHasRole 
• #oauth2.clientHasAnyRole 
• #oauth2.denyClient
Spring OAuth2 Client 
• Creates a filter to store the current request and context 
• Manages the redirection to and from the OAuth 
authentication URI 
• @EnableOAuth2Client 
• Annotation used to configure OAuth2 client 
• There is also XML configuration related <client/> 
• OAuth2RestTemplate 
• Wrapper client object to access the resources
Demo 
• OAuth2 Use Case 
• Conference application sharing resources with different clients 
• http://github.com/rcandidosilva/rest-oauth2-sample
Questions 
?
References 
• http://oauth.net/2/ 
• http://tools.ietf.org/html/rfc6749 
• http://projects.spring.io/spring-security-oauth/ 
• https://github.com/spring-projects/spring-security-oauth 
• http://cxf.apache.org/docs/jax-rs-oauth2.html 
• https://jersey.java.net/documentation/latest/security.html#d0e10940 
• https://oltu.apache.org
Thank you! 
@rcandidosilva 
rodrigocandido.me
Ad

Recommended

PPTX
Spring Security 5
Jesus Perez Franco
 
PDF
How to create a User Defined Policy with IBM APIc (v10)
Shiu-Fun Poon
 
PDF
Keycloak SSO basics
Juan Vicente Herrera Ruiz de Alejo
 
PDF
Istio service mesh introduction
Kyohei Mizumoto
 
PDF
Angular Advanced Routing
Laurent Duveau
 
PDF
OAuth2 and Spring Security
Orest Ivasiv
 
PDF
Spring Boot Interview Questions | Edureka
Edureka!
 
PDF
Microservices with Java, Spring Boot and Spring Cloud
Eberhard Wolff
 
PPTX
BDD (Behavior-Driven Development)
Renato Groff
 
PDF
NestJS
Wilson Su
 
PDF
Kafka Security 101 and Real-World Tips
confluent
 
PPTX
Spring data jpa
Jeevesh Pandey
 
PPTX
Spring boot
Pradeep Shanmugam
 
PPTX
JPA For Beginner's
NarayanaMurthy Ganashree
 
PPTX
Spring Security
Boy Tech
 
PPTX
User Management Life Cycle with Keycloak
Muhammad Edwin
 
PDF
Spring security oauth2
axykim00
 
PPTX
Spring boot
Gyanendra Yadav
 
PPTX
Spring security
Saurabh Sharma
 
DOCX
Servlet
Dhara Joshi
 
PDF
Spring Data JPA
Cheng Ta Yeh
 
PDF
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Edureka!
 
PDF
OAuth 2.0 with IBM WebSphere DataPower
Shiu-Fun Poon
 
PPT
Vue.js Getting Started
Murat Doğan
 
PDF
Nestjs MasterClass Slides
Nir Kaufman
 
ODP
Apache ActiveMQ and Apache Camel
Omi Om
 
PDF
Spring Boot
Jaran Flaath
 
PDF
ConFoo 2015 - Securing RESTful resources with OAuth2
Rodrigo Cândido da Silva
 
PPTX
Web API 2 Token Based Authentication
jeremysbrown
 

More Related Content

What's hot (20)

PPTX
BDD (Behavior-Driven Development)
Renato Groff
 
PDF
NestJS
Wilson Su
 
PDF
Kafka Security 101 and Real-World Tips
confluent
 
PPTX
Spring data jpa
Jeevesh Pandey
 
PPTX
Spring boot
Pradeep Shanmugam
 
PPTX
JPA For Beginner's
NarayanaMurthy Ganashree
 
PPTX
Spring Security
Boy Tech
 
PPTX
User Management Life Cycle with Keycloak
Muhammad Edwin
 
PDF
Spring security oauth2
axykim00
 
PPTX
Spring boot
Gyanendra Yadav
 
PPTX
Spring security
Saurabh Sharma
 
DOCX
Servlet
Dhara Joshi
 
PDF
Spring Data JPA
Cheng Ta Yeh
 
PDF
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Edureka!
 
PDF
OAuth 2.0 with IBM WebSphere DataPower
Shiu-Fun Poon
 
PPT
Vue.js Getting Started
Murat Doğan
 
PDF
Nestjs MasterClass Slides
Nir Kaufman
 
ODP
Apache ActiveMQ and Apache Camel
Omi Om
 
PDF
Spring Boot
Jaran Flaath
 
BDD (Behavior-Driven Development)
Renato Groff
 
NestJS
Wilson Su
 
Kafka Security 101 and Real-World Tips
confluent
 
Spring data jpa
Jeevesh Pandey
 
Spring boot
Pradeep Shanmugam
 
JPA For Beginner's
NarayanaMurthy Ganashree
 
Spring Security
Boy Tech
 
User Management Life Cycle with Keycloak
Muhammad Edwin
 
Spring security oauth2
axykim00
 
Spring boot
Gyanendra Yadav
 
Spring security
Saurabh Sharma
 
Servlet
Dhara Joshi
 
Spring Data JPA
Cheng Ta Yeh
 
Spring Interview Questions and Answers | Spring Tutorial | Spring Framework T...
Edureka!
 
OAuth 2.0 with IBM WebSphere DataPower
Shiu-Fun Poon
 
Vue.js Getting Started
Murat Doğan
 
Nestjs MasterClass Slides
Nir Kaufman
 
Apache ActiveMQ and Apache Camel
Omi Om
 
Spring Boot
Jaran Flaath
 

Similar to JavaOne 2014 - Securing RESTful Resources with OAuth2 (20)

PDF
ConFoo 2015 - Securing RESTful resources with OAuth2
Rodrigo Cândido da Silva
 
PPTX
Web API 2 Token Based Authentication
jeremysbrown
 
PPTX
GSoC Mideterm-OAuth2 Module
Mayank Sharma
 
PPTX
OAuth 2.0 at the Globiots
Tran Thanh Thi
 
PDF
Building an Effective Architecture for Identity and Access Management.pdf
Jorge Alvarez
 
PPTX
Authenticating Angular Apps with JWT
Jennifer Estrada
 
PPTX
REST API Security: OAuth 2.0, JWTs, and More!
Stormpath
 
PDF
AllTheTalks.Online 2020: "Basics of OAuth 2.0 and OpenID Connect"
Andreas Falk
 
PPTX
Introduction to sitecore identity
Gopikrishna Gujjula
 
PPTX
DDD Melbourne 2014 security in ASP.Net Web API 2
Pratik Khasnabis
 
PDF
Authentication in microservice systems - fsto 2017
Dejan Glozic
 
PDF
Securing Microservices using Play and Akka HTTP
Rafal Gancarz
 
PPTX
Adding Identity Management and Access Control to your App
FIWARE
 
PDF
Introduction to OAuth
Wei-Tsung Su
 
PPTX
Api security
teodorcotruta
 
PPTX
Comprehensive_SpringBoot_Auth.pptx wokring
JayaPrakash579769
 
PDF
Java EE Security API - JSR375: Getting Started
Rudy De Busscher
 
PPTX
Adding identity management and access control to your app
Álvaro Alonso González
 
PDF
Getting Started with Spring Authorization Server
VMware Tanzu
 
PPTX
JWT Authentication with AngularJS
robertjd
 
ConFoo 2015 - Securing RESTful resources with OAuth2
Rodrigo Cândido da Silva
 
Web API 2 Token Based Authentication
jeremysbrown
 
GSoC Mideterm-OAuth2 Module
Mayank Sharma
 
OAuth 2.0 at the Globiots
Tran Thanh Thi
 
Building an Effective Architecture for Identity and Access Management.pdf
Jorge Alvarez
 
Authenticating Angular Apps with JWT
Jennifer Estrada
 
REST API Security: OAuth 2.0, JWTs, and More!
Stormpath
 
AllTheTalks.Online 2020: "Basics of OAuth 2.0 and OpenID Connect"
Andreas Falk
 
Introduction to sitecore identity
Gopikrishna Gujjula
 
DDD Melbourne 2014 security in ASP.Net Web API 2
Pratik Khasnabis
 
Authentication in microservice systems - fsto 2017
Dejan Glozic
 
Securing Microservices using Play and Akka HTTP
Rafal Gancarz
 
Adding Identity Management and Access Control to your App
FIWARE
 
Introduction to OAuth
Wei-Tsung Su
 
Api security
teodorcotruta
 
Comprehensive_SpringBoot_Auth.pptx wokring
JayaPrakash579769
 
Java EE Security API - JSR375: Getting Started
Rudy De Busscher
 
Adding identity management and access control to your app
Álvaro Alonso González
 
Getting Started with Spring Authorization Server
VMware Tanzu
 
JWT Authentication with AngularJS
robertjd
 
Ad

More from Rodrigo Cândido da Silva (20)

PDF
Java 9, 10 e ... 11
Rodrigo Cândido da Silva
 
PDF
Cloud Native Java EE
Rodrigo Cândido da Silva
 
PDF
Protegendo Microservices: Boas Práticas e Estratégias de Implementação
Rodrigo Cândido da Silva
 
PDF
Protecting Java Microservices: Best Practices and Strategies
Rodrigo Cândido da Silva
 
PDF
As novidades da nova versão do Java 9
Rodrigo Cândido da Silva
 
PDF
Workshop Microservices - Distribuindo os Microservices com Docker e Kubernetes
Rodrigo Cândido da Silva
 
PDF
Workshop Microservices - Microservices com Spring Cloud e Netflix OSS
Rodrigo Cândido da Silva
 
PDF
Workshop Microservices - Construindo APIs RESTful com Spring Boot
Rodrigo Cândido da Silva
 
PDF
Workshop Microservices - Arquitetura Microservices
Rodrigo Cândido da Silva
 
PDF
GUJavaSC - Protegendo Microservices em Java
Rodrigo Cândido da Silva
 
PDF
TDC Floripa 2017 - Criando Microservices Reativos com Java
Rodrigo Cândido da Silva
 
PDF
GUJavaSC - Combinando Micro-serviços com Práticas DevOps
Rodrigo Cândido da Silva
 
PDF
GUJavaSC - Criando Micro-serviços Reativos com Java
Rodrigo Cândido da Silva
 
PDF
JavaOne 2016 - Reactive Microservices with Java and Java EE
Rodrigo Cândido da Silva
 
PDF
JavaOne LATAM 2016 - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
PDF
JavaOne LATAM 2016 - RESTful Services Simplificado com Spring Data REST
Rodrigo Cândido da Silva
 
PDF
TDC Floripa 2016 - Decolando seus micro-serviços na Spring Cloud
Rodrigo Cândido da Silva
 
PDF
GUJavaSC - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
PDF
QCon SP 2016 - Construindo Microservices Auto-curáveis com Spring Cloud e Net...
Rodrigo Cândido da Silva
 
PDF
QCon 2015 - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
Java 9, 10 e ... 11
Rodrigo Cândido da Silva
 
Cloud Native Java EE
Rodrigo Cândido da Silva
 
Protegendo Microservices: Boas Práticas e Estratégias de Implementação
Rodrigo Cândido da Silva
 
Protecting Java Microservices: Best Practices and Strategies
Rodrigo Cândido da Silva
 
As novidades da nova versão do Java 9
Rodrigo Cândido da Silva
 
Workshop Microservices - Distribuindo os Microservices com Docker e Kubernetes
Rodrigo Cândido da Silva
 
Workshop Microservices - Microservices com Spring Cloud e Netflix OSS
Rodrigo Cândido da Silva
 
Workshop Microservices - Construindo APIs RESTful com Spring Boot
Rodrigo Cândido da Silva
 
Workshop Microservices - Arquitetura Microservices
Rodrigo Cândido da Silva
 
GUJavaSC - Protegendo Microservices em Java
Rodrigo Cândido da Silva
 
TDC Floripa 2017 - Criando Microservices Reativos com Java
Rodrigo Cândido da Silva
 
GUJavaSC - Combinando Micro-serviços com Práticas DevOps
Rodrigo Cândido da Silva
 
GUJavaSC - Criando Micro-serviços Reativos com Java
Rodrigo Cândido da Silva
 
JavaOne 2016 - Reactive Microservices with Java and Java EE
Rodrigo Cândido da Silva
 
JavaOne LATAM 2016 - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
JavaOne LATAM 2016 - RESTful Services Simplificado com Spring Data REST
Rodrigo Cândido da Silva
 
TDC Floripa 2016 - Decolando seus micro-serviços na Spring Cloud
Rodrigo Cândido da Silva
 
GUJavaSC - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
QCon SP 2016 - Construindo Microservices Auto-curáveis com Spring Cloud e Net...
Rodrigo Cândido da Silva
 
QCon 2015 - Combinando AngularJS com Java EE
Rodrigo Cândido da Silva
 
Ad

Recently uploaded (20)

PDF
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
PDF
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
PDF
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
PDF
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
PDF
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PPTX
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
PDF
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
PDF
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
PPTX
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
PDF
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
PPTX
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
PDF
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 

JavaOne 2014 - Securing RESTful Resources with OAuth2

  • 1. Securing RESTful Resources with OAuth2 Rodrigo Cândido da Silva @rcandidosilva JavaOne 2014 CON4990
  • 2. About Me • Brazilian guy ;) • Software Architect • Java Platform • Work for Integritas Tech • http://integritastech.com • JUG Leader of GUJavaSC • http://gujavasc.org • Twitter • @rcandidosilva • Personal • http://rodrigocandido.me
  • 3. Agenda • Why use OAuth2? • OAuth2 concepts • Grant types • OAuth2 Tokens • Java Implementations • Demo
  • 5. Security Closed Closed Open Authentication Authorization
  • 6. Securing APIs • Securing resources strategies • Basic Auth (HTTP Basic) • Sending user credentials in http authentication header • Mutual Authentication (HTTP Digest) • Based on certificates, server authenticate to client, client to server • RESTful architecture not defines security procedures • HTTP methods: GET, POST, PUT, DELETE • REST API’s are equal vulnerable as standard web apps • Injection attacks, replay attacks, cross-site scripting, etc.
  • 9. Why OAuth • Open standard protocol specification defined by IETF • Enables applications to access each other’s data without sharing credentials • Avoid password issues • User and password authentication is fine, but what if your API needs to be used by other apps? • Required for delegating access • Third party applications • For specified resource • For limited time • Can be selectively be revoked
  • 10. Who is using OAuth
  • 11. OAuth Timeline • OAuth 1.0 • Core specification published in Dec 2007 • OAuth 1.0a • Revised specification published in June 2009 • Related to fix a security issue • OAuth 2.0 • Standardized since Oct-2012 • Be more secure, simple, and standard • Additional RFCs are still being worked on
  • 12. OAuth2 • No username or passwords (only tokens) • Protocol for authorization – not authentication • Delegated model • Fix the password anti-pattern • Trust relationship between resource, identity server and client app • Goal was simplicity • Relies heavily on TLS/SSL • Not backwards compatible • Easily to revoke
  • 13. OAuth2 Roles • Resource Owner • Entity capable of granting access to a protected resource • Client Application • Application making protected resource requests on behalf of the resource owner • Resource Server • The server hosting the protected resources • Authorization Server • The server issuing access tokens to the clients
  • 15. OAuth2 Grant Types • Authorization Code (web apps) • Optimized for confidential clients • Uses a authorization code from the server • Implicit (browser-based and mobile apps) • Optimized for script heavy web apps • User can see the access token • Resource Owner Password Credentials (user / password) • Used in cases where the user trusts the client • Exposes user credentials to the client • Client Credentials (application) • Clients gets an access token based on client credentials only
  • 18. Resource Owner Password Credentials
  • 20. OAuth2 Tokens • Types • Bearer • Large random token • Need SSL to protect it in transit • Server needs to store it securely hashed like a user password • Mac • Uses a nonce to prevent replay • Does not required SSL • OAuth 1.0 only supported • Access Token • Short-lived token • Refresh Token • Long-lived token { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":“bearer", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", }
  • 21. OAuth2 Pros & Cons • Pros • Integration of third party apps to any sites • Access can be granted for limited scope or duration • No need for users to give password on third party site • Cons • Writing an authorization server is somewhat complex • Interoperability issues • Bad implementations can be security issues
  • 22. OAuth2 Java Implementations • Some Java implementations available • Jersey • Apache Oltu • Spring Security OAuth2 • And others: CXF, Google OAuth2 API, etc • Not available as Java EE standard yet
  • 23. Jersey • Open source RESTful Web services framework • The JAX-RS reference implementation • Integrates with the Java EE standard security • @RolesAllowed • @PermitAll • @DenyAll • Supports entity filtering features • @EntityFiltering • Only supports OAuth2 at client side :/
  • 24. Jersey Java EE security integration @Path("restricted-resource") @Produces("application/json") public class RestrictedResource { @GET @Path(”denyAll") @DenyAll public RestrictedEntity denyAll() { ... } @GET @Path("rolesAllowed") @RolesAllowed({"manager"}) public RestrictedEntity rolesAllowed() { ... } }
  • 25. Jersey OAuth2 client support OAuth2CodeGrantFlow.Builder builder = OAuth2ClientSupport .authorizationCodeGrantFlowBuilder( clientId, "https://example.com/oauth/authorization", "https://example.com/oauth/token"); OAuth2CodeGrantFlow flow = builder.property( OAuth2CodeGrantFlow.Phase.AUTHORIZATION, "readOnly", "true") .scope("contact") .build(); String authorizationUri = flow.start(); ... final TokenResult result = flow.finish(code, state); ...
  • 26. Apache Oltu • Apache OAuth protocol implementation • It also covers others related implementations • JSON Web Token (JWT) • JSON Web Signature (JWS) • OpenID Connect • Supports the full OAuth2 features • Authorization Server • Resource Server • Client • Provides predefined OAuth2 client types • Facebook, Foursquare, Github, Google, etc • Still being improved…
  • 27. Apache Oltu Authorization endpoint protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //dynamically recognize an OAuth profile and perform validation OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request); validateRedirectionURI(oauthRequest) //build OAuth response OAuthResponse resp = OAuthASResponse .authorizationResponse(HttpServletResponse.SC_FOUND) .setCode(oauthIssuerImpl.authorizationCode()) .location(ex.getRedirectUri()) .buildQueryMessage(); response.sendRedirect(resp.getLocationUri()); }
  • 28. Apache Oltu Token endpoint protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator()); OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request); validateClient(oauthRequest); String authzCode = oauthRequest.getCode(); String accessToken = oauthIssuerImpl.accessToken(); String refreshToken = oauthIssuerImpl.refreshToken(); OAuthResponse r = OAuthASResponse(...); }
  • 29. Apache Oltu Protecting the resources protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Make the OAuth Request and validate it OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request, ParameterStyle.BODY); // Get the access token String accessToken = oauthRequest.getAccessToken(); //... validate access token }
  • 30. Apache Oltu OAuth2 client OAuthClientRequest request = OAuthClientRequest .tokenProvider(OAuthProviderType.FACEBOOK) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId("your-facebook-application-client-id") .setClientSecret("your-facebook-application-client-secret") .setRedirectURI("http://www.example.com/redirect") .setCode(code) .buildQueryMessage(); //create OAuth client that uses custom http client under the hood OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request); String accessToken = oAuthResponse.getAccessToken(); String expiresIn = oAuthResponse.getExpiresIn();
  • 31. Spring Security OAuth • Provides OAuth (1a) and OAuth2 support • Implements 4 types of authorization grants • Supports the OAuth2 full features • Authorization Server • Resources Server • Client • Good integration with JAX-RS and Spring MVC • Configuration using annotation support • Integrates with the Spring ecosystem
  • 32. Spring Authorization Server • @EnableAuthorizationServer • Annotation used to configure OAuth2 authorization server • There is also XML configuration related <authorization-server/> • ClientDetailsServiceConfigurer • Defines the client details service • In-memory or JDBC implementation • AuthorizationServerTokenServices • Operations to manage OAuth2 tokens • Tokens in-memory, JDBC or JSON Web Token (JWT) • AuthorizationServerEndpointConfigurer • Grant types supported by the server • All grant types are supported except password types
  • 33. Spring Resource Server • Can be the same as Authorization Server • Or deployed in a separate application • Provides a authentication filter for web protection • @EnableResourceServer • Annotation used to configure OAuth2 resource server • There is also XML configuration related <resource-server/> • Supports expression-based access control • #oauth2.clientHasRole • #oauth2.clientHasAnyRole • #oauth2.denyClient
  • 34. Spring OAuth2 Client • Creates a filter to store the current request and context • Manages the redirection to and from the OAuth authentication URI • @EnableOAuth2Client • Annotation used to configure OAuth2 client • There is also XML configuration related <client/> • OAuth2RestTemplate • Wrapper client object to access the resources
  • 35. Demo • OAuth2 Use Case • Conference application sharing resources with different clients • http://github.com/rcandidosilva/rest-oauth2-sample
  • 37. References • http://oauth.net/2/ • http://tools.ietf.org/html/rfc6749 • http://projects.spring.io/spring-security-oauth/ • https://github.com/spring-projects/spring-security-oauth • http://cxf.apache.org/docs/jax-rs-oauth2.html • https://jersey.java.net/documentation/latest/security.html#d0e10940 • https://oltu.apache.org
  • 38. Thank you! @rcandidosilva rodrigocandido.me