diff --git a/nbactions.xml b/nbactions.xml new file mode 100644 index 0000000..4a41b70 --- /dev/null +++ b/nbactions.xml @@ -0,0 +1,17 @@ + + + + run + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:1.2.1:exec + + + -classpath %classpath com.example.DemoOauth2SpringSecurityApplication + java + + + diff --git a/pom.xml b/pom.xml index 1c0b9e6..a8ba148 100644 --- a/pom.xml +++ b/pom.xml @@ -1,64 +1,75 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.example - demo-oauth2-spring-security - 1.0.0-SNAPSHOT - jar + com.example + demo-oauth2-spring-security + 1.0.0-SNAPSHOT + jar - demo-oauth2-spring-security - Demo project for Spring Boot + demo-oauth2-spring-security + Demo project for Spring Boot - - org.springframework.boot - spring-boot-starter-parent - 1.5.3.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + - - UTF-8 - UTF-8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + Finchley.SR1 + - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.security.oauth - spring-security-oauth2 - + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-oauth2 + + + org.springframework.boot + spring-boot-devtools + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + - - org.springframework.boot - spring-boot-devtools - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/src/main/java/com/example/DemoOauth2SpringSecurityApplication.java b/src/main/java/com/example/DemoOauth2SpringSecurityApplication.java index 5ccf335..1bbd342 100644 --- a/src/main/java/com/example/DemoOauth2SpringSecurityApplication.java +++ b/src/main/java/com/example/DemoOauth2SpringSecurityApplication.java @@ -6,7 +6,7 @@ @SpringBootApplication public class DemoOauth2SpringSecurityApplication { - public static void main(String[] args) { - SpringApplication.run(DemoOauth2SpringSecurityApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(DemoOauth2SpringSecurityApplication.class, args); + } } diff --git a/src/main/java/com/example/config/AuthorizationServerConfig.java b/src/main/java/com/example/config/AuthorizationServerConfig.java index 74215c4..89efc2e 100644 --- a/src/main/java/com/example/config/AuthorizationServerConfig.java +++ b/src/main/java/com/example/config/AuthorizationServerConfig.java @@ -15,28 +15,29 @@ @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { - @Autowired + @Autowired private AuthenticationManager authenticationManager; - - @Override + + @Override public void configure(final AuthorizationServerEndpointsConfigurer endpoints) { - endpoints.tokenStore(tokenStore()) - .authenticationManager(authenticationManager); + endpoints + .tokenStore(tokenStore()) + .authenticationManager(authenticationManager); } - - @Override + + @Override public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { - clients.inMemory() - .withClient("client") - .secret("clientpassword") - .scopes("read", "write") - .authorizedGrantTypes("password") - .accessTokenValiditySeconds(3600); + clients.inMemory() + .withClient("client") + .secret("{noop}clientpassword")//See: https://stackoverflow.com/questions/49654143/spring-security-5-there-is-no-passwordencoder-mapped-for-the-id-null + .scopes("read", "write") + .authorizedGrantTypes("password") + .accessTokenValiditySeconds(3600); + } + + @Bean + public TokenStore tokenStore() { + return new InMemoryTokenStore(); } - - @Bean - public TokenStore tokenStore() { - return new InMemoryTokenStore(); - } } diff --git a/src/main/java/com/example/config/ResourceServerConfig.java b/src/main/java/com/example/config/ResourceServerConfig.java index ec593a9..0c44ba8 100644 --- a/src/main/java/com/example/config/ResourceServerConfig.java +++ b/src/main/java/com/example/config/ResourceServerConfig.java @@ -1,33 +1,23 @@ package com.example.config; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; -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.EnableWebSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; @Configuration @EnableResourceServer -@EnableWebSecurity public class ResourceServerConfig extends ResourceServerConfigurerAdapter { - @Autowired - public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication() - .withUser("user").password("user").roles("ROLE"); + @Override + public void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/products/**").authenticated() + .and() + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS); } - - @Override - public void configure(HttpSecurity http) throws Exception { - http - .authorizeRequests() - .anyRequest().authenticated().and() - .sessionManagement() - .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() - .csrf().disable(); - } - + } diff --git a/src/main/java/com/example/config/WebSecurityConfig.java b/src/main/java/com/example/config/WebSecurityConfig.java new file mode 100644 index 0000000..15416a4 --- /dev/null +++ b/src/main/java/com/example/config/WebSecurityConfig.java @@ -0,0 +1,50 @@ +package com.example.config; + +import org.springframework.beans.factory.annotation.Autowired; +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.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.http.SessionCreationPolicy; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user") + .password("{noop}user") //See: https://stackoverflow.com/questions/46999940/spring-boot-passwordencoder-error + .roles("USER"); + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/oauth/token").permitAll() + .anyRequest().authenticated() + .and() + .httpBasic() + .and() + .csrf().disable(); + } + + /** + * See: https://github.com/spring-projects/spring-boot/issues/11136 + * + * @return + * @throws Exception + */ + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + +} diff --git a/src/main/java/com/example/resource/ProductResource.java b/src/main/java/com/example/resource/ProductResource.java index 0c92494..40e5da5 100644 --- a/src/main/java/com/example/resource/ProductResource.java +++ b/src/main/java/com/example/resource/ProductResource.java @@ -11,37 +11,37 @@ @RequestMapping("/products") public class ProductResource { - @GetMapping - public List list() { - return Arrays.asList(new Product("Mug for Coffee", 12.99), new Product("Coffee cup", 4.21)); - } + @GetMapping + public List list() { + return Arrays.asList(new Product("Mug for Coffee", 12.99), new Product("Coffee cup", 4.21)); + } - public static class Product { + public static class Product { - private String name; - private double value; + private String name; + private double value; - public Product(String name, double value) { - this.name = name; - this.value = value; - } + public Product(String name, double value) { + this.name = name; + this.value = value; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public void setName(String name) { - this.name = name; - } + public void setName(String name) { + this.name = name; + } - public double getValue() { - return value; - } + public double getValue() { + return value; + } - public void setValue(double value) { - this.value = value; - } + public void setValue(double value) { + this.value = value; + } - } + } }