diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/.classpath b/spring-security-oauth/spring-security-oauth-resource-demo/.classpath
new file mode 100644
index 0000000000..0cad5db2d0
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/.classpath
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/.project b/spring-security-oauth/spring-security-oauth-resource-demo/.project
new file mode 100644
index 0000000000..f6e67b2426
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/.project
@@ -0,0 +1,48 @@
+
+
+ spring-security-oauth-resource-demo
+
+
+
+
+
+ org.eclipse.wst.jsdt.core.javascriptValidator
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.wst.common.project.facet.core.builder
+
+
+
+
+ org.springframework.ide.eclipse.core.springbuilder
+
+
+
+
+ org.eclipse.wst.validation.validationbuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.jem.workbench.JavaEMFNature
+ org.eclipse.wst.common.modulecore.ModuleCoreNature
+ org.springframework.ide.eclipse.core.springnature
+ org.eclipse.jdt.core.javanature
+ org.eclipse.m2e.core.maven2Nature
+ org.eclipse.wst.common.project.facet.core.nature
+ org.eclipse.wst.jsdt.core.jsNature
+
+
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/pom.xml b/spring-security-oauth/spring-security-oauth-resource-demo/pom.xml
new file mode 100644
index 0000000000..a1a7e045fb
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/pom.xml
@@ -0,0 +1,46 @@
+
+ 4.0.0
+ spring-security-oauth-resource-demo
+ spring-security-oauth-resource-demo
+ war
+
+
+ org.baeldung
+ spring-security-oauth
+ 1.0.0-SNAPSHOT
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.springframework.security.oauth
+ spring-security-oauth2
+ ${oauth.version}
+
+
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
+
+
+
+
+ spring-security-oauth-resource-demo
+
+
+ src/main/resources
+ true
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/MethodSecurityConfig.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/MethodSecurityConfig.java
new file mode 100644
index 0000000000..25fcab2ffd
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/MethodSecurityConfig.java
@@ -0,0 +1,18 @@
+package org.baeldung.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
+import org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;
+
+@Configuration
+@EnableGlobalMethodSecurity(prePostEnabled = true)
+public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
+
+ @Override
+ protected MethodSecurityExpressionHandler createExpressionHandler() {
+ return new OAuth2MethodSecurityExpressionHandler();
+ }
+
+}
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/OAuth2ResourceServerConfigDemo.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/OAuth2ResourceServerConfigDemo.java
new file mode 100644
index 0000000000..0e80b7bc9c
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/OAuth2ResourceServerConfigDemo.java
@@ -0,0 +1,31 @@
+package org.baeldung.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
+import org.springframework.security.oauth2.provider.token.TokenStore;
+import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
+
+//@Configuration
+//@EnableResourceServer
+public class OAuth2ResourceServerConfigDemo extends ResourceServerConfigurerAdapter {
+
+ //
+
+ @Override
+ public void configure(final HttpSecurity http) throws Exception {
+ // @formatter:off
+ http
+ .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
+ .and().authorizeRequests().anyRequest().authenticated();
+ ;
+ // @formatter:on
+ }
+
+ @Bean
+ public TokenStore tokenStore() {
+ return new InMemoryTokenStore();
+ }
+
+}
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerApplication.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerApplication.java
new file mode 100644
index 0000000000..1e35eff551
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerApplication.java
@@ -0,0 +1,14 @@
+package org.baeldung.config;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.web.SpringBootServletInitializer;
+
+@SpringBootApplication
+public class ResourceServerApplication extends SpringBootServletInitializer {
+
+ public static void main(String[] args) {
+ SpringApplication.run(ResourceServerApplication.class, args);
+ }
+
+}
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerWebConfig.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerWebConfig.java
new file mode 100644
index 0000000000..81b2d242ac
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/config/ResourceServerWebConfig.java
@@ -0,0 +1,13 @@
+package org.baeldung.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+
+@Configuration
+@EnableWebMvc
+@ComponentScan({ "org.baeldung.web.controller" })
+public class ResourceServerWebConfig extends WebMvcConfigurerAdapter {
+ //
+}
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/BarController.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/BarController.java
new file mode 100644
index 0000000000..72163ff9ff
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/BarController.java
@@ -0,0 +1,41 @@
+package org.baeldung.web.controller;
+
+import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
+import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
+
+import org.baeldung.web.dto.Bar;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@Controller
+public class BarController {
+
+ public BarController() {
+ super();
+ }
+
+ // API - read
+ // @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('read')")
+ @RequestMapping(method = RequestMethod.GET, value = "/bars/{id}")
+ @ResponseBody
+ public Bar findById(@PathVariable final long id) {
+ return new Bar(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
+ }
+
+ // API - write
+ // @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')")
+ @RequestMapping(method = RequestMethod.POST, value = "/bars")
+ @ResponseStatus(HttpStatus.CREATED)
+ @ResponseBody
+ public Bar create(@RequestBody final Bar bar) {
+ bar.setId(Long.parseLong(randomNumeric(2)));
+ return bar;
+ }
+
+}
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/FooController.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/FooController.java
new file mode 100644
index 0000000000..d9ef1baa31
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/controller/FooController.java
@@ -0,0 +1,41 @@
+package org.baeldung.web.controller;
+
+import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
+import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
+
+import org.baeldung.web.dto.Foo;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@Controller
+public class FooController {
+
+ public FooController() {
+ super();
+ }
+
+ // API - read
+ // @PreAuthorize("#oauth2.hasScope('foo') and #oauth2.hasScope('read')")
+ @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
+ @ResponseBody
+ public Foo findById(@PathVariable final long id) {
+ return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
+ }
+
+ // API - write
+ // @PreAuthorize("#oauth2.hasScope('foo') and #oauth2.hasScope('write')")
+ @RequestMapping(method = RequestMethod.POST, value = "/foos")
+ @ResponseStatus(HttpStatus.CREATED)
+ @ResponseBody
+ public Foo create(@RequestBody final Foo foo) {
+ foo.setId(Long.parseLong(randomNumeric(2)));
+ return foo;
+ }
+
+}
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Bar.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Bar.java
new file mode 100644
index 0000000000..adbb2aa2ad
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Bar.java
@@ -0,0 +1,36 @@
+package org.baeldung.web.dto;
+
+public class Bar {
+ private long id;
+ private String name;
+
+ public Bar() {
+ super();
+ }
+
+ public Bar(final long id, final String name) {
+ super();
+
+ this.id = id;
+ this.name = name;
+ }
+
+ //
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(final long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Foo.java b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Foo.java
new file mode 100644
index 0000000000..9d26618e7f
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/java/org/baeldung/web/dto/Foo.java
@@ -0,0 +1,36 @@
+package org.baeldung.web.dto;
+
+public class Foo {
+ private long id;
+ private String name;
+
+ public Foo() {
+ super();
+ }
+
+ public Foo(final long id, final String name) {
+ super();
+
+ this.id = id;
+ this.name = name;
+ }
+
+ //
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(final long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-resource-demo/src/main/resources/application.properties b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/resources/application.properties
new file mode 100644
index 0000000000..62a10d1751
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-resource-demo/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+server.contextPath=/spring-security-oauth-resource
+server.port=8081
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/.classpath b/spring-security-oauth/spring-security-oauth-server-demo/.classpath
new file mode 100644
index 0000000000..0cad5db2d0
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/.classpath
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/.project b/spring-security-oauth/spring-security-oauth-server-demo/.project
new file mode 100644
index 0000000000..0ee409f87b
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/.project
@@ -0,0 +1,48 @@
+
+
+ spring-security-oauth-server-demo
+
+
+
+
+
+ org.eclipse.wst.jsdt.core.javascriptValidator
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.wst.common.project.facet.core.builder
+
+
+
+
+ org.springframework.ide.eclipse.core.springbuilder
+
+
+
+
+ org.eclipse.wst.validation.validationbuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+
+ org.eclipse.jem.workbench.JavaEMFNature
+ org.eclipse.wst.common.modulecore.ModuleCoreNature
+ org.springframework.ide.eclipse.core.springnature
+ org.eclipse.jdt.core.javanature
+ org.eclipse.m2e.core.maven2Nature
+ org.eclipse.wst.common.project.facet.core.nature
+ org.eclipse.wst.jsdt.core.jsNature
+
+
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/.springBeans b/spring-security-oauth/spring-security-oauth-server-demo/.springBeans
new file mode 100644
index 0000000000..18656ebe2e
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/.springBeans
@@ -0,0 +1,16 @@
+
+
+ 1
+
+
+
+
+
+
+ java:org.baeldung.config.AuthorizationServerApplication
+
+
+
+
+
+
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/pom.xml b/spring-security-oauth/spring-security-oauth-server-demo/pom.xml
new file mode 100644
index 0000000000..ae575293e8
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/pom.xml
@@ -0,0 +1,46 @@
+
+ 4.0.0
+ spring-security-oauth-server-demo
+
+ spring-security-oauth-server-demo
+ war
+
+
+ org.baeldung
+ spring-security-oauth
+ 1.0.0-SNAPSHOT
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ org.springframework.security.oauth
+ spring-security-oauth2
+ ${oauth.version}
+
+
+
+
+
+ spring-security-oauth-server-demo
+
+
+ src/main/resources
+ true
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/AuthorizationServerApplication.java b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/AuthorizationServerApplication.java
new file mode 100644
index 0000000000..73b8fc1976
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/AuthorizationServerApplication.java
@@ -0,0 +1,14 @@
+package org.baeldung.config;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.web.SpringBootServletInitializer;
+
+@SpringBootApplication
+public class AuthorizationServerApplication extends SpringBootServletInitializer {
+
+ public static void main(String[] args) {
+ SpringApplication.run(AuthorizationServerApplication.class, args);
+ }
+
+}
\ No newline at end of file
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/OAuth2AuthorizationServerConfigDemo.java b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/OAuth2AuthorizationServerConfigDemo.java
new file mode 100644
index 0000000000..e37cd93e63
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/OAuth2AuthorizationServerConfigDemo.java
@@ -0,0 +1,51 @@
+package org.baeldung.config;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.Bean;
+import org.springframework.security.authentication.AuthenticationManager;
+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.configurers.AuthorizationServerEndpointsConfigurer;
+import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
+import org.springframework.security.oauth2.provider.token.TokenStore;
+import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
+
+//@Configuration
+//@EnableAuthorizationServer
+public class OAuth2AuthorizationServerConfigDemo extends AuthorizationServerConfigurerAdapter {
+
+ @Autowired
+ @Qualifier("authenticationManagerBean")
+ private AuthenticationManager authenticationManager;
+
+ //
+
+ @Override
+ public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
+ oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
+ }
+
+ @Override
+ public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { // @formatter:off
+ clients.inMemory()
+ .withClient("fooClientIdPassword")
+ .secret("secret")
+ .authorizedGrantTypes("password", "authorization_code", "refresh_token")
+ .scopes("foo", "read", "write")
+ .accessTokenValiditySeconds(3600) // 1 hour
+ .refreshTokenValiditySeconds(2592000) // 30 days
+ ;
+ } // @formatter:on
+
+ @Override
+ public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
+ endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
+ }
+
+ @Bean
+ public TokenStore tokenStore() {
+ return new InMemoryTokenStore();
+ }
+
+}
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/WebSecurityConfig.java b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/WebSecurityConfig.java
new file mode 100644
index 0000000000..63ae10ae03
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/src/main/java/org/baeldung/config/WebSecurityConfig.java
@@ -0,0 +1,38 @@
+package org.baeldung.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+
+@Configuration
+public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
+
+ @Override
+ protected void configure(final AuthenticationManagerBuilder auth) throws Exception {// @formatter:off
+ auth.inMemoryAuthentication().
+ withUser("john").password("123").roles("USER").
+ and().
+ withUser("tom").password("111").roles("ADMIN");
+ }// @formatter:on
+
+ @Override
+ @Bean
+ public AuthenticationManager authenticationManagerBean() throws Exception {
+ return super.authenticationManagerBean();
+ }
+
+ @Override
+ protected void configure(final HttpSecurity http) throws Exception {
+ // @formatter:off
+ http.authorizeRequests()
+ .antMatchers("/login").permitAll()
+ .anyRequest().authenticated()
+ .and().formLogin().permitAll()
+ ;
+ // @formatter:on
+ }
+
+}
diff --git a/spring-security-oauth/spring-security-oauth-server-demo/src/main/resources/application.properties b/spring-security-oauth/spring-security-oauth-server-demo/src/main/resources/application.properties
new file mode 100644
index 0000000000..e33e7dabf6
--- /dev/null
+++ b/spring-security-oauth/spring-security-oauth-server-demo/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+server.contextPath=/spring-security-oauth-server
+server.port=8081
\ No newline at end of file