Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blog-dev-010-rx-java-test - Modify blog service with RX Java #170

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 82 additions & 11 deletions springBootBlog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->

<!-- Webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
Expand Down Expand Up @@ -103,28 +109,93 @@
https://ithelp.ithome.com.tw/articles/10250502
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.6.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- <version>2.6.3</version>-->
<!-- </dependency>-->

<!--
Oauth2
https://github.com/danvega/spring-security-social-login/blob/main/pom.xml#L26
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
<!-- </dependency>-->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<!--
R2DBC for mysql

https://mvnrepository.com/artifact/io.asyncer/r2dbc-mysql

https://youtu.be/_1HwzpWx5UM?si=LZsO06P998GAoVzp&t=81
-->
<!-- <dependency>-->
<!-- <groupId>io.asyncer</groupId>-->
<!-- <artifactId>r2dbc-mysql</artifactId>-->
<!-- <version>1.1.3</version>-->
<!-- </dependency>-->

<!-- https://mvnrepository.com/artifact/io.asyncer/r2dbc-mysql -->
<dependency>
<groupId>io.asyncer</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.9.4</version>
</dependency>

<!--
https://youtu.be/_1HwzpWx5UM?si=LZsO06P998GAoVzp&t=81
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-r2dbc
-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-r2dbc</artifactId>-->
<!-- <version>3.2.5</version>-->
<!-- </dependency>-->

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-r2dbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.5.2</version>
</dependency>


<!--
fix R2DBC network issue (suggested by GPT)

The error you encountered is related to the Netty library,
which is used by R2DBC under the hood for network operations.
Specifically, the error suggests that a native library required by Netty for DNS resolution on macOS is missing.
This doesn’t directly affect the execution of your SQL query but can cause network-related issues.
Fixing the Issue To resolve this issue, you can add the necessary Netty dependency for macOS.
Update your pom.xml to include the following dependency:
-->
<!-- <dependency>-->
<!-- <groupId>io.netty</groupId>-->
<!-- <artifactId>netty-resolver-dns-native-macos</artifactId>-->
<!-- <version>${netty.version}</version>-->
<!-- <classifier>osx-aarch_64</classifier>-->
<!-- </dependency>-->


<!-- https://mvnrepository.com/artifact/io.netty/netty-resolver-dns-native-macos -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver-dns-native-macos</artifactId>
<version>4.1.111.Final</version>
<scope>runtime</scope>
</dependency>


</dependencies>

<build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.config.EnableWebFlux;

@EnableWebFlux
// all default setting by @WebFluxAutoConfiguration will be disabled : https://youtu.be/xUux3Ycjh7U?si=YCYzEiMo7vQTiotv&t=2068
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.yen.mdblog.config;

// https://youtu.be/xUux3Ycjh7U?si=ymKyfKFYDxxjpT4g&t=2113
// custom webflux config

import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

public class MyWebFluxConfig {

@Bean
public WebFluxConfigurer webFluxConfigurer(){

return new WebFluxConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
// WebFluxConfigurer.super.addCorsMappings(registry);
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("localhost");
}
};

}

}
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
package com.yen.mdblog.config;

// social login : https://youtu.be/us0VjFiHogo?t=241

import com.yen.mdblog.handler.MyLogoutHandler;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class OAuthSecurityConfig {

@Autowired MyLogoutHandler myLogoutHandler;

// in memory user
// https://www.youtube.com/watch?v=66DtzkhBlSA&t=515s
// https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/in-memory.html
@Bean
public UserDetailsService userDetailsService() {

UserDetails user =
User.withUsername("user1").password("{noop}123").authorities("READ", "ROLE_USER").build();

UserDetails admin =
User.withUsername("admin").password("{noop}123").authorities("READ", "ROLE_USER").build();

return new InMemoryUserDetailsManager(user, admin);
}

// social login : https://youtu.be/us0VjFiHogo?t=241
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {

// LogoutConfigurer<HttpSecurity> LogoutConfigurer = httpSecurity.authorizeRequests()
// .and()
// .logout().addLogoutHandler(myLogoutHandler); // 新增自訂的登出處理器

httpSecurity =
httpSecurity
.csrf()
.disable()
.cors()
.and()
.logout(
logout ->
logout
.permitAll()
.logoutSuccessHandler(
(request, response, authentication) -> {
response.setStatus(HttpServletResponse.SC_OK);
}));

return httpSecurity
// login
.formLogin()
.defaultSuccessUrl("/posts/all", true)
.permitAll()
.and()
// logout // TODO : fix logout redirect
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl(
"classpath:/templates/logout.html") // Redirect to "/logout.html" after logout
.permitAll()
.and()
// auth
.authorizeRequests(
auth -> {
auth.anyRequest().authenticated();
})
.formLogin(Customizer.withDefaults())
.oauth2Login(Customizer.withDefaults())
.build();
}
}
//package com.yen.mdblog.config;
//
//// social login : https://youtu.be/us0VjFiHogo?t=241
//
//import com.yen.mdblog.handler.MyLogoutHandler;
//import javax.servlet.http.HttpServletResponse;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.Customizer;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//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.provisioning.InMemoryUserDetailsManager;
//import org.springframework.security.web.SecurityFilterChain;
//
//@Configuration
//@EnableWebSecurity
//public class OAuthSecurityConfig {
//
// @Autowired MyLogoutHandler myLogoutHandler;
//
// // in memory user
// // https://www.youtube.com/watch?v=66DtzkhBlSA&t=515s
// // https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/in-memory.html
// @Bean
// public UserDetailsService userDetailsService() {
//
// UserDetails user =
// User.withUsername("user1").password("{noop}123").authorities("READ", "ROLE_USER").build();
//
// UserDetails admin =
// User.withUsername("admin").password("{noop}123").authorities("READ", "ROLE_USER").build();
//
// return new InMemoryUserDetailsManager(user, admin);
// }
//
// // social login : https://youtu.be/us0VjFiHogo?t=241
// @Bean
// SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
//
// // LogoutConfigurer<HttpSecurity> LogoutConfigurer = httpSecurity.authorizeRequests()
// // .and()
// // .logout().addLogoutHandler(myLogoutHandler); // 新增自訂的登出處理器
//
// httpSecurity =
// httpSecurity
// .csrf()
// .disable()
// .cors()
// .and()
// .logout(
// logout ->
// logout
// .permitAll()
// .logoutSuccessHandler(
// (request, response, authentication) -> {
// response.setStatus(HttpServletResponse.SC_OK);
// }));
//
// return httpSecurity
// // login
// .formLogin()
// .defaultSuccessUrl("/posts/all", true)
// .permitAll()
// .and()
// // logout // TODO : fix logout redirect
// .logout()
// .logoutUrl("/logout")
// .logoutSuccessUrl(
// "classpath:/templates/logout.html") // Redirect to "/logout.html" after logout
// .permitAll()
// .and()
// // auth
// .authorizeRequests(
// auth -> {
// auth.anyRequest().authenticated();
// })
// .formLogin(Customizer.withDefaults())
// .oauth2Login(Customizer.withDefaults())
// .build();
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yen.mdblog.config;

// https://youtu.be/42MTtF44XAs?si=VnXx5TwPHOVeSZv5&t=53

import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

// TODO : check how it works / if is necessary
@EnableR2dbcRepositories // enable r2dbc repo : similar as JPA
@Configuration
public class R2dbcConfig {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Controller
@RequestMapping("/author")
Expand All @@ -22,18 +24,24 @@ public class AuthorController {
@GetMapping("/all")
String getAllAuthor(Model model, Principal principal) {

List<Author> authors = authorService.getAllAuthors();
//List<Author> authors = authorService.getAllAuthors();
Flux<Author> authors = authorService.getAllAuthors();

model.addAttribute("authors", authors);
model.addAttribute("user", principal.getName());
//model.addAttribute("user", principal.getName());
model.addAttribute("user", "admin");
return "author/authors";
}

@GetMapping("/{id}")
public String getAuthorById(@PathVariable Integer id, Model model, Principal principal) {

Author author = authorService.getById(id);
//Author author = authorService.getById(id);
Mono<Author> author = authorService.getById(id);

model.addAttribute("author", author);
model.addAttribute("user", principal.getName());
//model.addAttribute("user", principal.getName());
model.addAttribute("user", "admin");
return "author/author";
}
}
Loading
Loading