From 3cf4dc5a78444d816ffd53b9d85d64b0ec43732b Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Thu, 1 Jun 2017 20:14:41 +0200 Subject: [PATCH] second client fo sso (#1971) * minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso --- spring-security-sso/pom.xml | 1 + .../spring-security-sso-ui-2/pom.xml | 44 +++++++++++++++++++ .../org/baeldung/config/UiApplication.java | 33 ++++++++++++++ .../java/org/baeldung/config/UiWebConfig.java | 41 +++++++++++++++++ .../src/main/resources/application.yml | 20 +++++++++ .../src/main/resources/templates/index.html | 18 ++++++++ .../main/resources/templates/securedPage.html | 20 +++++++++ .../org/baeldung/test/UiIntegrationTest.java | 18 ++++++++ 8 files changed, 195 insertions(+) create mode 100644 spring-security-sso/spring-security-sso-ui-2/pom.xml create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/main/java/org/baeldung/config/UiApplication.java create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/main/java/org/baeldung/config/UiWebConfig.java create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/main/resources/application.yml create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/main/resources/templates/index.html create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/main/resources/templates/securedPage.html create mode 100644 spring-security-sso/spring-security-sso-ui-2/src/test/java/org/baeldung/test/UiIntegrationTest.java diff --git a/spring-security-sso/pom.xml b/spring-security-sso/pom.xml index 36c0c13052..7438cede16 100644 --- a/spring-security-sso/pom.xml +++ b/spring-security-sso/pom.xml @@ -18,6 +18,7 @@ spring-security-sso-auth-server spring-security-sso-ui + spring-security-sso-ui-2 diff --git a/spring-security-sso/spring-security-sso-ui-2/pom.xml b/spring-security-sso/spring-security-sso-ui-2/pom.xml new file mode 100644 index 0000000000..6a8dc5385b --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui-2/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + spring-security-sso-ui-2 + + spring-security-sso-ui-2 + 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-2/src/main/java/org/baeldung/config/UiApplication.java b/spring-security-sso/spring-security-sso-ui-2/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-2/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-2/src/main/java/org/baeldung/config/UiWebConfig.java b/spring-security-sso/spring-security-sso-ui-2/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-2/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-2/src/main/resources/application.yml b/spring-security-sso/spring-security-sso-ui-2/src/main/resources/application.yml new file mode 100644 index 0000000000..6b0d3db5ad --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui-2/src/main/resources/application.yml @@ -0,0 +1,20 @@ +server: + port: 8083 + context-path: /ui2 + session: + cookie: + name: UI2SESSION +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-2/src/main/resources/templates/index.html b/spring-security-sso/spring-security-sso-ui-2/src/main/resources/templates/index.html new file mode 100644 index 0000000000..38fa8bda50 --- /dev/null +++ b/spring-security-sso/spring-security-sso-ui-2/src/main/resources/templates/index.html @@ -0,0 +1,18 @@ + + + + +Spring Security SSO Client 2 + + + + +
+
+

Spring Security SSO Client 2

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

Secured Page

+ Welcome, Name +
+ Your authorities are authorities +
+
+ + \ No newline at end of file diff --git a/spring-security-sso/spring-security-sso-ui-2/src/test/java/org/baeldung/test/UiIntegrationTest.java b/spring-security-sso/spring-security-sso-ui-2/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-2/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() { + + } +}