Merge pull request #9871 from sampada07/JAVA-1639

JAVA-1639: Upgrade spring-5-security-cognito to use latest Spring Boot
This commit is contained in:
Dhawal Kapil 2020-08-16 08:40:03 +05:30 committed by GitHub
commit 4c75754f64
9 changed files with 74 additions and 68 deletions

View File

@ -34,11 +34,6 @@
</dependency> </dependency>
<!-- oauth2 --> <!-- oauth2 -->
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>${oauth-auto.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId> <artifactId>spring-security-oauth2-client</artifactId>
@ -64,8 +59,6 @@
</dependencies> </dependencies>
<properties> <properties>
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
<oauth-auto.version>2.1.0.RELEASE</oauth-auto.version>
<start-class>com.baeldung.cognito.SpringCognitoApplication</start-class> <start-class>com.baeldung.cognito.SpringCognitoApplication</start-class>
</properties> </properties>

View File

@ -1,12 +1,10 @@
package com.baeldung.cognito; package com.baeldung.cognito;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
@PropertySource("cognito/application-cognito.yml")
public class CognitoWebConfiguration implements WebMvcConfigurer { public class CognitoWebConfiguration implements WebMvcConfigurer {
@Override @Override

View File

@ -0,0 +1,23 @@
package com.baeldung.cognito;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf()
.and()
.authorizeRequests(authz -> authz.mvcMatchers("/")
.permitAll()
.anyRequest()
.authenticated())
.oauth2Login()
.and()
.logout()
.logoutSuccessUrl("/");
}
}

View File

@ -2,10 +2,8 @@ package com.baeldung.cognito;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication @SpringBootApplication
@PropertySource("cognito/application-cognito.yml")
public class SpringCognitoApplication { public class SpringCognitoApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -0,0 +1,15 @@
spring:
security:
oauth2:
client:
registration:
cognito:
client-id: your_clientId
client-secret: your_clientSecret
scope: openid
redirect-uri: http://localhost:8080/login/oauth2/code/cognito
clientName: your_clientName
provider:
cognito:
issuerUri: https://cognito-idp.{region}.amazonaws.com/{poolId}
user-name-attribute: cognito:username

View File

@ -1,15 +0,0 @@
spring:
security:
oauth2:
client:
registration:
cognito:
client-id: clientId
client-secret: clientSecret
scope: openid
redirectUriTemplate: "http://localhost:8080/login/oauth2/code/cognito"
clientName: cognito-client-name
provider:
cognito:
issuerUri: https://cognito-idp.{region}.amazonaws.com/{poolId}
usernameAttribute: cognito:username

View File

@ -1,32 +0,0 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OAuth2 Cognito Demo</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css">
<link rel="stylesheet" th:href="@{/cognito/style.css}">
</head>
<body>
<section class="section">
<div class="container has-text-centered">
<div>
<h1 class="title">OAuth2 Spring Security Cognito Demo</h1>
<div sec:authorize="isAuthenticated()">
<div class="box">
Hello, <strong th:text="${#authentication.name}"></strong>!
</div>
</div>
<div sec:authorize="isAnonymous()">
<div class="box">
<a class="button login is-primary" th:href="@{/oauth2/authorization/cognito}">Log in with Amazon Cognito</a>
</div>
</div>
</div>
</div>
</section>
</body>
</html>

View File

@ -1,9 +0,0 @@
.login {
background-color: #7289da;
color: #fff;
}
.login:hover {
background-color: #697ec4;
color: #fff;
}

View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>OAuth2 Cognito Demo</title>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container has-text-centered">
<div>
<h1 class="title">OAuth2 Spring Security Cognito Demo</h1>
<div sec:authorize="isAuthenticated()">
<div class="box">
Hello, <strong th:text="${#authentication.name}"></strong>!
</div>
</div>
<div sec:authorize="isAnonymous()">
<div class="box">
<a class="button login is-primary"
th:href="@{/oauth2/authorization/cognito}">Log in with Amazon
Cognito</a>
</div>
</div>
</div>
</div>
</section>
</body>
</html>