diff --git a/spring-security-sso/pom.xml b/spring-security-sso/pom.xml new file mode 100644 index 0000000000..36c0c13052 --- /dev/null +++ b/spring-security-sso/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + org.baeldung + spring-security-sso + 1.0.0-SNAPSHOT + + spring-security-sso + pom + + + parent-boot-5 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-5 + + + + spring-security-sso-auth-server + spring-security-sso-ui + + + + 3.0.1 + + + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-auth-server/pom.xml b/spring-security-sso/spring-security-sso-auth-server/pom.xml new file mode 100644 index 0000000000..c7b23249f9 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/pom.xml @@ -0,0 +1,28 @@ + + 4.0.0 + spring-security-sso-auth-server + + spring-security-sso-auth-server + war + + + org.baeldung + spring-security-sso + 1.0.0-SNAPSHOT + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.security.oauth + spring-security-oauth2 + + + + + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthServerConfig.java b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthServerConfig.java new file mode 100644 index 0000000000..20cde21073 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthServerConfig.java @@ -0,0 +1,41 @@ +package org.baeldung.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +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.configuration.EnableAuthorizationServer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; +import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; + +@Configuration +@EnableAuthorizationServer +public class AuthServerConfig extends AuthorizationServerConfigurerAdapter { + @Autowired + 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 { + clients.inMemory() + .withClient("SampleClientId") + .secret("secret") + .authorizedGrantTypes("authorization_code") + .scopes("user_info") + .autoApprove(true) + // .accessTokenValiditySeconds(3600) + ; // 1 hour + } + + @Override + public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception { + endpoints.authenticationManager(authenticationManager); + } + +} diff --git a/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthorizationServerApplication.java b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthorizationServerApplication.java new file mode 100644 index 0000000000..5b0b39b444 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/AuthorizationServerApplication.java @@ -0,0 +1,16 @@ +package org.baeldung.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; + +@SpringBootApplication +@EnableResourceServer +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-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/SecurityConfig.java b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/SecurityConfig.java new file mode 100644 index 0000000000..9de203b7c3 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/SecurityConfig.java @@ -0,0 +1,38 @@ +package org.baeldung.config; + +import org.springframework.beans.factory.annotation.Autowired; +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 SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private AuthenticationManager authenticationManager; + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.requestMatchers() + .antMatchers("/login", "/oauth/authorize") + .and() + .authorizeRequests() + .anyRequest() + .authenticated() + .and() + .formLogin() + .permitAll(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.parentAuthenticationManager(authenticationManager) + .inMemoryAuthentication() + .withUser("john") + .password("123") + .roles("USER"); + } + +} diff --git a/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/UserController.java b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/UserController.java new file mode 100644 index 0000000000..28128f9d97 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/main/java/org/baeldung/config/UserController.java @@ -0,0 +1,16 @@ +package org.baeldung.config; + +import java.security.Principal; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class UserController { + + @RequestMapping("/user/me") + public Principal user(Principal principal) { + System.out.println(principal); + return principal; + } +} diff --git a/spring-security-sso/spring-security-sso-auth-server/src/main/resources/application.properties b/spring-security-sso/spring-security-sso-auth-server/src/main/resources/application.properties new file mode 100644 index 0000000000..32a0993b04 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=8081 +server.context-path=/auth +security.basic.enabled=false +#logging.level.org.springframework=DEBUG \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-auth-server/src/test/java/org/baeldung/test/AuthServerIntegrationTest.java b/spring-security-sso/spring-security-sso-auth-server/src/test/java/org/baeldung/test/AuthServerIntegrationTest.java new file mode 100644 index 0000000000..9e2d6feca1 --- /dev/null +++ b/spring-security-sso/spring-security-sso-auth-server/src/test/java/org/baeldung/test/AuthServerIntegrationTest.java @@ -0,0 +1,18 @@ +package org.baeldung.test; + +import org.baeldung.config.AuthorizationServerApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = AuthorizationServerApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) +public class AuthServerIntegrationTest { + + @Test + public void whenLoadApplication_thenSuccess() { + + } +} diff --git a/spring-security-sso/spring-security-sso-ui/pom.xml b/spring-security-sso/spring-security-sso-ui/pom.xml new file mode 100644 index 0000000000..fe1cd56a76 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + spring-security-sso-ui + + spring-security-sso-ui + war + + + org.baeldung + spring-security-sso + 1.0.0-SNAPSHOT + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-security + + + + org.springframework.security.oauth + spring-security-oauth2 + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.thymeleaf.extras + thymeleaf-extras-springsecurity4 + + + + + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiApplication.java b/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiApplication.java new file mode 100644 index 0000000000..5ef699d264 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiApplication.java @@ -0,0 +1,33 @@ +package org.baeldung.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; +import org.springframework.context.annotation.Bean; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.web.context.request.RequestContextListener; + +@EnableOAuth2Sso +@SpringBootApplication +public class UiApplication extends WebSecurityConfigurerAdapter { + + @Override + public void configure(HttpSecurity http) throws Exception { + http.antMatcher("/**") + .authorizeRequests() + .antMatchers("/", "/login**") + .permitAll() + .anyRequest() + .authenticated(); + } + + @Bean + public RequestContextListener requestContextListener() { + return new RequestContextListener(); + } + + public static void main(String[] args) { + SpringApplication.run(UiApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiWebConfig.java b/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiWebConfig.java new file mode 100644 index 0000000000..24d6c9b5d8 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/main/java/org/baeldung/config/UiWebConfig.java @@ -0,0 +1,41 @@ +package org.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +@EnableWebMvc +public class UiWebConfig extends WebMvcConfigurerAdapter { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Override + public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + registry.addViewController("/") + .setViewName("forward:/index"); + registry.addViewController("/index"); + registry.addViewController("/securedPage"); + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**") + .addResourceLocations("/resources/"); + } + +} \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/main/resources/application.yml b/spring-security-sso/spring-security-sso-ui/src/main/resources/application.yml new file mode 100644 index 0000000000..bb4bd92033 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/main/resources/application.yml @@ -0,0 +1,20 @@ +server: + port: 8082 + context-path: /ui + session: + cookie: + name: UISESSION +security: + basic: + enabled: false + oauth2: + client: + clientId: SampleClientId + clientSecret: secret + accessTokenUri: http://localhost:8081/auth/oauth/token + userAuthorizationUri: http://localhost:8081/auth/oauth/authorize + resource: + userInfoUri: http://localhost:8081/auth/user/me +spring: + thymeleaf: + cache: false \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/index.html b/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/index.html new file mode 100644 index 0000000000..12948e0738 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + + +Spring Security SSO + + + + +
+
+

Spring Security SSO

+ Login +
+
+ + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/securedPage.html b/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/securedPage.html new file mode 100644 index 0000000000..81ed51b2d6 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/main/resources/templates/securedPage.html @@ -0,0 +1,18 @@ + + + + +Spring Security SSO + + + + +
+
+

Secured Page

+ Welcome, Name +
+
+ + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui/src/test/java/org/baeldung/test/UiIntegrationTest.java b/spring-security-sso/spring-security-sso-ui/src/test/java/org/baeldung/test/UiIntegrationTest.java new file mode 100644 index 0000000000..6386eac0b8 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui/src/test/java/org/baeldung/test/UiIntegrationTest.java @@ -0,0 +1,18 @@ +package org.baeldung.test; + +import org.baeldung.config.UiApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = UiApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) +public class UiIntegrationTest { + + @Test + public void whenLoadApplication_thenSuccess() { + + } +}