Compare commits

..

2 Commits

Author SHA1 Message Date
b0e2f0e25b ReactJS: user managment 2023-08-21 18:05:19 +02:00
fa2e4b0669 SpringBoot: add user security 2023-08-21 18:04:29 +02:00
4 changed files with 59 additions and 8 deletions

View File

@ -16,12 +16,13 @@ repositories {
} }
dependencies { dependencies {
//implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1' implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test' testImplementation 'io.projectreactor:reactor-test'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
//testImplementation 'org.springframework.security:spring-security-test' //testImplementation 'org.springframework.security:spring-security-test'
} }

View File

@ -1,5 +1,6 @@
package djmil.cordacheckers; package djmil.cordacheckers;
import java.security.Principal;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -26,4 +27,14 @@ public class ApiController {
return ResponseEntity.ok(joke); return ResponseEntity.ok(joke);
} }
/**
*
* @return a Json list of active games
*/
@GetMapping("/api/activegames")
public ResponseEntity<String> dashboard(Principal principal) {
return ResponseEntity.ok("{ \"ActiveGames\" : [\"game\", \"GAME\", \""+principal.getName()+ "\" ] }" );
}
} }

View File

@ -0,0 +1,38 @@
package djmil.cordacheckers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
public class SecurityConfig {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
UserDetailsService hardcodedCordaUsers(PasswordEncoder passwordEncoder) {
User.UserBuilder users = User.builder();
UserDetails alice = users
.username("alice")
.password(passwordEncoder.encode("qaz123"))
.build();
UserDetails bob = users
.username("bob")
.password(passwordEncoder.encode("qaz123"))
.build();
return new InMemoryUserDetailsManager(alice, bob);
}
}

View File

@ -3,14 +3,15 @@ import React, { useState, useEffect } from 'react';
function App() { function App() {
const [joke, setJoke] = useState(null); const [activeGames, setActiveGames] = useState(null);
useEffect(() => { useEffect(() => {
fetch('/api/badjokes') fetch('/api/activegames')
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((games) => {
console.log(data.joke); console.log(games.ActiveGames.length);
setJoke(data.joke); console.log("games: " +games.ActiveGames);
setActiveGames(games.ActiveGames);
}) })
.catch((err) => { .catch((err) => {
console.log(err.message); console.log(err.message);
@ -21,7 +22,7 @@ function App() {
<div className="App"> <div className="App">
<header className="App-header"> <header className="App-header">
<p> <p>
Here is your joke: {joke ? <span>{joke}</span> : <span>Loading...</span>} Here is list of your active games: {activeGames ? <span>{activeGames}</span> : <span>Loading...</span>}
</p> </p>
</header> </header>
</div> </div>