working Spring Boot oauth2 connection to nextcloud oidc

This commit is contained in:
Jörg Henke
2023-12-20 10:43:21 +01:00
parent 4bb3efafb8
commit 04e1899150
17 changed files with 548 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package de.ship.nextcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}

View File

@ -0,0 +1,13 @@
package de.ship.nextcloud;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ClientApplication.class);
}
}

View File

@ -0,0 +1,20 @@
package de.ship.nextcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(r -> r
.requestMatchers("/").permitAll().requestMatchers("/index").permitAll()
.requestMatchers("/inside").authenticated())
.oauth2Login(l -> l.authorizationEndpoint(e -> e.baseUri("/oauth2/authorize-client")))
.oidcLogout();
return http.build();
}
}

View File

@ -0,0 +1,30 @@
package de.ship.nextcloud.modules;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
*
* @author henkej
*
*/
@Controller
public class Check {
@GetMapping("/")
public String getRoot() {
return "redirect:/index";
}
@GetMapping("/index")
public String getIndex() {
return "/index";
}
@GetMapping("/inside")
public String getMapping(Model model) {
model.addAttribute("authenticated", "Die nextcloud-Authentifizierung hat funktioniert.");
return "/inside";
}
}

View File

@ -0,0 +1,8 @@
server.port = 9999
# required
spring.security.oauth2.client.provider.nextcloud.issuer-uri = http://localhost
spring.security.oauth2.client.registration.nextcloud.client-id = XvDdIXcOFERJq4p2si5ydI8EO3u3VcuTDXtEvGybGm2ILhg2vpSV1nXdG9QKyr5C
spring.security.oauth2.client.registration.nextcloud.client-secret = sd5SAhh4TNyj5When1i83JqtJK5MzHaBDY2ChWvFzSsvnBdwyOozFFdyMRIxnFDt
spring.security.oauth2.client.registration.nextcloud.authorization-grant-type = authorization_code
spring.security.oauth2.client.registration.nextcloud.redirect-uri = http://localhost:9999/login/oauth2/code/herbert

View File

@ -0,0 +1,29 @@
version: '3'
services:
db:
image: mariadb
restart: always
volumes:
- nextcloud-db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_PASSWORD=password
app:
image: nextcloud
restart: always
ports:
- 80:80
volumes:
- nextcloud:/var/www/html
environment:
- MYSQL_HOST=db
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
- MYSQL_PASSWORD=password
volumes:
nextcloud-db:
nextcloud:

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="de" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
</head>
<body>
<a th:href="@{/inside}">Betreten</a>
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="de" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
</head>
<body>
<span th:text="${authenticated}"></span>
<br />
<a th:href="@{/logout}">abmelden</a>
</body>
</html>