Skip to content
Merged
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
25 changes: 25 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Java CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
cache: maven
- name: Build with Maven
run: mvn -T 1C -B clean com.spotify.fmt:fmt-maven-plugin:2.27:check -Dstyle=aosp test
5 changes: 2 additions & 3 deletions src/main/java/org/example/springbank/Application.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.example.springbank;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
Expand All @@ -13,5 +13,4 @@ public static void main(String[] args) {
logger.info("Starting the application...");
SpringApplication.run(Application.class, args);
}

}
8 changes: 4 additions & 4 deletions src/main/java/org/example/springbank/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public SimpleMailMessage messageTemplate() {
SimpleMailMessage message = new SimpleMailMessage();

message.setSubject("Transaction notification");
message.setText("Transaction completed successfully:\n\n [%s] Sender: %s, Receiver: %s, Amount: %s");
message.setText(
"Transaction completed successfully:\n\n [%s] Sender: %s, Receiver: %s, Amount: %s");
message.setFrom(fromAddress);

return message;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("/WEB-INF/static/");
registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
}

@Profile("local")
@Bean
public CommandLineRunner demo(final DemoDataService demoDataService) {
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/example/springbank/config/AuthHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthHandler implements AuthenticationSuccessHandler {

Expand All @@ -24,9 +25,11 @@ public AuthHandler(UserService userService) {
}

@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication) throws IOException {
public void onAuthenticationSuccess(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication)
throws IOException {
System.out.println("OAuth2 authentication successful!!!");
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
Map<String, Object> attributes = oAuth2User.getAttributes();
Expand All @@ -45,11 +48,11 @@ public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,

if (user == null) {
System.out.println("User not found, creating new Google user");
CustomUserDTO userDTO = CustomUserDTO.of(
email,
(String) attributes.get("name"),
(String) attributes.get("picture")
);
CustomUserDTO userDTO =
CustomUserDTO.of(
email,
(String) attributes.get("name"),
(String) attributes.get("picture"));

userService.addGoogleUser(userDTO);
httpServletResponse.sendRedirect("/");
Expand Down
69 changes: 39 additions & 30 deletions src/main/java/org/example/springbank/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,52 @@ public SecurityConfig(AuthHandler authHandler) {
this.authenticationSuccessHandler = authHandler;
System.out.println("Authentication Success Handler: " + authenticationSuccessHandler);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests(auth -> auth
.antMatchers("/").hasAnyRole("USER", "ADMIN")
.antMatchers("/register","/login", "/js/**", "/css/**", "/images/**", "/favicon.ico", "/logout")
.permitAll()
.anyRequest()
.authenticated()
)
.exceptionHandling(ex -> ex
.accessDeniedPage("/error/403")
)
.formLogin(form -> form
.loginPage("/login")
.loginProcessingUrl("/j_spring_security_check")
.failureUrl("/login?error")
.usernameParameter("j_login")
.passwordParameter("j_password")
.permitAll()
)
.oauth2Login(oauth -> oauth
.loginPage("/login")
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler("/login?error"))
)
.logout(logout -> logout
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.permitAll()
);
http.csrf()
.disable()
.authorizeRequests(
auth ->
auth.antMatchers("/")
.hasAnyRole("USER", "ADMIN")
.antMatchers(
"/register",
"/login",
"/js/**",
"/css/**",
"/images/**",
"/favicon.ico",
"/logout")
.permitAll()
.anyRequest()
.authenticated())
.exceptionHandling(ex -> ex.accessDeniedPage("/error/403"))
.formLogin(
form ->
form.loginPage("/login")
.loginProcessingUrl("/j_spring_security_check")
.failureUrl("/login?error")
.usernameParameter("j_login")
.passwordParameter("j_password")
.permitAll())
.oauth2Login(
oauth ->
oauth.loginPage("/login")
.successHandler(authenticationSuccessHandler)
.failureHandler(
new SimpleUrlAuthenticationFailureHandler(
"/login?error")))
.logout(
logout ->
logout.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.permitAll());
return http.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ public class AccountController {
private final AccountService accountService;
private final DemoDataService demoDataService;

public AccountController(AccountService accountService, DemoDataService demoDataService){
public AccountController(AccountService accountService, DemoDataService demoDataService) {
this.accountService = accountService;
this.demoDataService = demoDataService;
}

@GetMapping("/")
public String listAllAccounts(Model model,
@RequestParam(required = false, defaultValue = "0") Integer page){
public String listAllAccounts(
Model model, @RequestParam(required = false, defaultValue = "0") Integer page) {
if (page < 0) page = 0;

List<Account> accounts = accountService
.findAll(PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));
List<Account> accounts =
accountService.findAll(
PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));

model.addAttribute("clients", accountService.findClients());
model.addAttribute("accounts", accounts);
Expand All @@ -44,13 +45,17 @@ public String listAllAccounts(Model model,
public String listAccountsByClient(
@PathVariable(value = "id") long clientId,
@RequestParam(required = false, defaultValue = "0") Integer page,
Model model)
{
Client client = (clientId != Constants.DEFAULT_CLIENT_ID) ? accountService.findClient(clientId) : null;
Model model) {
Client client =
(clientId != Constants.DEFAULT_CLIENT_ID)
? accountService.findClient(clientId)
: null;
if (page < 0) page = 0;

List<Account> accounts = accountService
.findByClient(client, PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));
List<Account> accounts =
accountService.findByClient(
client,
PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));

model.addAttribute("clients", accountService.findClients());
model.addAttribute("accounts", accounts);
Expand All @@ -61,20 +66,22 @@ public String listAccountsByClient(
}

@GetMapping("/account_add_page/{id}")
public String accountAddPage(Model model,
@PathVariable(value = "id") long clientId) {
public String accountAddPage(Model model, @PathVariable(value = "id") long clientId) {
model.addAttribute("clients", accountService.findClients());
model.addAttribute("client", accountService.findClient(clientId));
model.addAttribute("currencies", CurrencyType.values());
return "account/account_add_page";
}

@PostMapping(value="/add")
public String accountAdd(@RequestParam(value = "clientId") long clientId,
@RequestParam double balance,
@RequestParam CurrencyType currency)
{
Client client = (clientId != Constants.DEFAULT_CLIENT_ID) ? accountService.findClient(clientId) : null;
@PostMapping(value = "/add")
public String accountAdd(
@RequestParam(value = "clientId") long clientId,
@RequestParam double balance,
@RequestParam CurrencyType currency) {
Client client =
(clientId != Constants.DEFAULT_CLIENT_ID)
? accountService.findClient(clientId)
: null;

Account account = new Account(client, balance, currency);
accountService.addAccount(account);
Expand All @@ -98,11 +105,13 @@ public String search(@RequestParam String pattern, Model model) {

private long getPageCount() {
long totalCount = accountService.count();
return (totalCount / Constants.ITEMS_PER_PAGE) + ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
return (totalCount / Constants.ITEMS_PER_PAGE)
+ ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
}

private long getPageCount(Client client) {
long totalCount = accountService.countByClient(client);
return (totalCount / Constants.ITEMS_PER_PAGE) + ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
return (totalCount / Constants.ITEMS_PER_PAGE)
+ ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ClientController {
private final ClientService clientService;
private final DemoDataService demoDataService;

public ClientController(ClientService clientService, DemoDataService demoDataService){
public ClientController(ClientService clientService, DemoDataService demoDataService) {
this.clientService = clientService;
this.demoDataService = demoDataService;
}
Expand All @@ -40,11 +40,13 @@ public Client getClientByName(@PathVariable String name) {
}

@GetMapping("/")
public String index(Model model,
@RequestParam(required = false, defaultValue = "0") Integer page){
if(page < 0) page = 0;
public String index(
Model model, @RequestParam(required = false, defaultValue = "0") Integer page) {
if (page < 0) page = 0;

List<Client> clients = clientService.findAll(PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));
List<Client> clients =
clientService.findAll(
PageRequest.of(page, Constants.ITEMS_PER_PAGE, Sort.Direction.DESC, "id"));

model.addAttribute("clients", clients);
model.addAttribute("allPages", getPageCount());
Expand All @@ -56,13 +58,13 @@ public String clientAddPage() {
return "client/client_add_page";
}

@PostMapping(value="/add")
public String clientAdd(@RequestParam String name,
@RequestParam String surname,
@RequestParam String phone,
@RequestParam String email,
@RequestParam String address)
{
@PostMapping(value = "/add")
public String clientAdd(
@RequestParam String name,
@RequestParam String surname,
@RequestParam String phone,
@RequestParam String email,
@RequestParam String address) {
Client client = new Client(name, surname, phone, email, address);
clientService.addClient(client);

Expand All @@ -78,8 +80,7 @@ public String resetDemoData() {
@PostMapping(value = "/delete")
public ResponseEntity<Void> delete(
@RequestParam(value = "toDelete[]", required = false) long[] toDelete) {
if (toDelete != null && toDelete.length > 0)
clientService.deleteClient(toDelete);
if (toDelete != null && toDelete.length > 0) clientService.deleteClient(toDelete);

return new ResponseEntity<>(HttpStatus.OK);
}
Expand All @@ -93,6 +94,7 @@ public String search(@RequestParam String pattern, Model model) {

private long getPageCount() {
long totalCount = clientService.count();
return (totalCount / Constants.ITEMS_PER_PAGE) + ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
return (totalCount / Constants.ITEMS_PER_PAGE)
+ ((totalCount % Constants.ITEMS_PER_PAGE > 0) ? 1 : 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RequestMapping("/demo")
// @RequestMapping("/demo")
public class DemoDataController {
private final DemoDataService demoDataService;

Expand Down
Loading
Loading