forked from Baeldung/spring-security-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
clients-thymeleaf/oauth-ui-authorization-code-thymeleaf/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/build/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ |
54 changes: 54 additions & 0 deletions
54
clients-thymeleaf/oauth-ui-authorization-code-thymeleaf/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>oauth-ui-authorization-code-thymeleaf</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>oauth-ui-authorization-code-thymeleaf</name> | ||
<description>Oauth client using Thymeleaf</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.1.RELEASE</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-oauth2-client</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.thymeleaf.extras</groupId> | ||
<artifactId>thymeleaf-extras-springsecurity5</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
36 changes: 36 additions & 0 deletions
36
...ation-code-thymeleaf/src/main/java/com/baeldung/oauth/AuthorizationHeaderInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.baeldung.oauth; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
|
||
public class AuthorizationHeaderInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
private OAuth2AuthorizedClientService clientService; | ||
|
||
public AuthorizationHeaderInterceptor(OAuth2AuthorizedClientService clientService) { | ||
this.clientService = clientService; | ||
} | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String accessToken = null; | ||
if (authentication != null && authentication.getClass().isAssignableFrom(OAuth2AuthenticationToken.class)) { | ||
OAuth2AuthenticationToken auth = (OAuth2AuthenticationToken) authentication; | ||
String clientRegistrationId = auth.getAuthorizedClientRegistrationId(); | ||
OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(clientRegistrationId, auth.getName()); | ||
accessToken = client.getAccessToken().getTokenValue(); | ||
request.getHeaders().add("Authorization", "Bearer " + accessToken); | ||
} | ||
return execution.execute(request, bytes); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...auth-ui-authorization-code-thymeleaf/src/main/java/com/baeldung/oauth/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baeldung.oauth; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
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.oauth2.client.OAuth2AuthorizedClientService; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@EnableWebSecurity | ||
public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.antMatchers("/").permitAll() | ||
.anyRequest().authenticated() | ||
.and() | ||
.oauth2Login() | ||
.and() | ||
.logout().logoutSuccessUrl("/"); | ||
} | ||
|
||
@Bean | ||
public RestTemplate restTemplate(OAuth2AuthorizedClientService clientService) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); | ||
if (CollectionUtils.isEmpty(interceptors)) { | ||
interceptors = new ArrayList<>(); | ||
} | ||
interceptors.add(new AuthorizationHeaderInterceptor(clientService)); | ||
restTemplate.setInterceptors(interceptors); | ||
return restTemplate; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...oauth-ui-authorization-code-thymeleaf/src/main/java/com/baeldung/oauth/UiApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.oauth; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class UiApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(UiApplication.class, args); | ||
} | ||
} | ||
|
33 changes: 33 additions & 0 deletions
33
...ization-code-thymeleaf/src/main/java/com/baeldung/oauth/web/controller/FooController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.baeldung.oauth.web.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.baeldung.oauth.web.dto.Foo; | ||
|
||
@Controller | ||
public class FooController { | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
@GetMapping("/foos/{id}") | ||
public String getFooResource(@PathVariable long id, Model model) { | ||
Foo foo = restTemplate.getForEntity("http://localhost:8088/spring-security-oauth-resource/foos/" + id, Foo.class).getBody(); | ||
model.addAttribute("foo", foo); | ||
return "foo"; | ||
} | ||
|
||
@PostMapping("/foos") | ||
public String addNewFoo(Foo foo, Model model) { | ||
Foo created = restTemplate.postForEntity("http://localhost:8088/spring-security-oauth-resource/foos/", foo, Foo.class).getBody(); | ||
model.addAttribute("foo", created); | ||
return "foo"; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...f/oauth-ui-authorization-code-thymeleaf/src/main/java/com/baeldung/oauth/web/dto/Foo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.baeldung.oauth.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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
clients-thymeleaf/oauth-ui-authorization-code-thymeleaf/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
spring: | ||
security: | ||
oauth2: | ||
client: | ||
registration: | ||
custom: | ||
client-id: fooClientIdPassword | ||
client-secret: secret | ||
scopes: read,foo | ||
authorization-grant-type: authorization_code | ||
redirect-uri-template: http://localhost:8080/login/oauth2/code/custom | ||
provider: | ||
custom: | ||
authorization-uri: http://localhost:8081/spring-security-oauth-server/oauth/authorize | ||
token-uri: http://localhost:8081/spring-security-oauth-server/oauth/token | ||
user-info-uri: http://localhost:8088/spring-security-oauth-resource/users/extra | ||
user-name-attribute: user_name |
39 changes: 39 additions & 0 deletions
39
...nts-thymeleaf/oauth-ui-authorization-code-thymeleaf/src/main/resources/templates/foo.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>Spring OAuth Client Thymeleaf</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" /> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm p-3 mb-5"> | ||
<a class="navbar-brand">Spring OAuth Client Thymeleaf</a> | ||
<ul class="navbar-nav ml-auto"> | ||
<li class="navbar-text"> Hi, <span sec:authentication="name">username</span> </li> | ||
<li class="nav-item"> <a href="/logout" class="btn btn-outline-secondary">Logout</a> </li> | ||
</ul> | ||
</nav> | ||
|
||
<div class="container"> | ||
<label>Foo Details</label> | ||
<form th:object="${foo}" th:action="@{/foos}" method="post"> | ||
<div class="form-group row"> | ||
<label for="fooId" class="col-sm-1 col-form-label">ID</label> | ||
<div class="col-sm-10"> | ||
<input type="text" class="col-sm-6 form-control" id="fooId" th:field="*{id}"/> | ||
</div> | ||
</div> | ||
<div class="form-group row"> | ||
<label for="name" class="col-sm-1 col-form-label">Name</label> | ||
<div class="col-sm-10"> | ||
<input type="text" class="col-sm-6 form-control" id="name" th:field="*{name}"/> | ||
</div> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Add Foo</button> | ||
<a href="/foos/1" class="btn btn-primary">Get Foo</a> | ||
|
||
</form> | ||
</div> | ||
|
||
</body> | ||
</html> |
19 changes: 19 additions & 0 deletions
19
...s-thymeleaf/oauth-ui-authorization-code-thymeleaf/src/main/resources/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>Spring OAuth Client Thymeleaf</title> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" /> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm p-3 mb-5"> | ||
<a class="navbar-brand">Spring OAuth Client Thymeleaf</a> | ||
</nav> | ||
|
||
<div class="container"> | ||
<label>Welcome ! </label> | ||
<br/> | ||
<a href="/foos/1" class="btn btn-primary">Login</a> | ||
</div> | ||
</body> | ||
</html> |
16 changes: 16 additions & 0 deletions
16
...ization-code-thymeleaf/src/test/java/com/baeldung/oauth/UiApplicationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.oauth; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class UiApplicationIntegrationTest { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |