30

I have recently built a REST API in Spring which I am consuming with a .Net C# client. At the moment there is no security so I would like to implement OAuth2. I am going to be the only user of my REST API so I don't need something complicated. Is there a simple example somewhere of Spring Security OAuth2 with java configuration? I looked in many places but either the examples use xml or they are too old and out of date or simply too complicated for what I need. I would appreciate any pointers with this.

By the way I looked at the Sparklr tutorial and github repo but they seem too complicated for what I need.

Dimitris
  • 2,030
  • 3
  • 27
  • 45

3 Answers3

17

This one the best I ever found https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation

nKognito
  • 6,297
  • 17
  • 77
  • 138
  • Thanks that does seem to be a better example. Now I have a different problem. I can get the access token but I get "Access is denied (user is anonymous" when I try to retrieve a resource using that token. Any ideas of whats going wrong? – Dimitris May 08 '15 at 10:19
  • If your resource server is a separate application then you need to configure the token store which will be the same as authorization server uses – nKognito May 08 '15 at 10:23
  • I am following the vanilla example step by step and don't really get what I need to do with the token store. Any pointers? – Dimitris May 08 '15 at 10:25
  • And check the request's headers. It should be Authorization: Bearer your_token – nKognito May 08 '15 at 10:26
  • I think I found the problem. I didn't have @EnableResourceServer on my Application class so the ResourceServer wasn't configured. – Dimitris May 08 '15 at 10:32
  • https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation/vanilla is killer – Erich Dec 03 '15 at 22:09
  • Another one with Java configuration http://javabycode.com/spring-framework-tutorial/spring-security/secure-spring-rest-api-using-spring-security-oauth2-example.html – David Pham Dec 21 '16 at 09:55
7

Below project provided a good example, in both server/client side.

https://github.com/bassemZohdy/Spring_REST_OAuth_Demo

Hope that helps.

santc mc
  • 91
  • 4
5

You see the examples of the spring project?

https://github.com/spring-projects/spring-security-oauth/tree/master/samples

Matthias
  • 1,378
  • 10
  • 23