Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ target
.flattened-pom.xml
secrets.yml
.gradletasknamecache
.sts4-cache
.sts4-cache
cookies.txt
6 changes: 6 additions & 0 deletions JtProject/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target/
.git
.gitignore
*.iml
.idea
*.md
28 changes: 28 additions & 0 deletions JtProject/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Build stage
FROM maven:3.8-eclipse-temurin-11 AS build
WORKDIR /app

# Copy pom.xml and download dependencies
COPY pom.xml .
RUN mvn dependency:go-offline -B

# Copy source code and build
COPY src ./src
RUN mvn clean package -DskipTests -B

# Run stage
FROM eclipse-temurin:11-jre-alpine
WORKDIR /app

# Create non-root user
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring

# Copy jar from build stage
COPY --from=build /app/target/*.jar app.jar

# Expose port
EXPOSE 8080

# Run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
14 changes: 8 additions & 6 deletions JtProject/basedata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
SET SQL_MODE ='IGNORE_SPACE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

# create database and use it
CREATE DATABASE IF NOT EXISTS ecommjava;
USE ecommjava;
CREATE DATABASE IF NOT EXISTS ecomjava;
USE ecomjava;
Comment on lines +5 to +6

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script creates/uses ecomjava, but Docker Compose and the README still reference ecommjava. Standardize the database name across all configs/scripts so setup instructions and Docker runs target the same schema.

Suggested change
CREATE DATABASE IF NOT EXISTS ecomjava;
USE ecomjava;
CREATE DATABASE IF NOT EXISTS ecommjava;
USE ecommjava;

Copilot uses AI. Check for mistakes.

# create the category table
CREATE TABLE IF NOT EXISTS CATEGORY(
Expand All @@ -30,13 +30,15 @@ email varchar(255) null,
password varchar(255) null,
role varchar(255) null,
username varchar(255) null,
is_active tinyint(1) not null default 1,
UNIQUE (username)
);

# insert default customers
INSERT INTO CUSTOMER(address, email, password, role, username) VALUES
('123, Albany Street', 'admin@nyan.cat', '123', 'ROLE_ADMIN', 'admin'),
('765, 5th Avenue', 'lisa@gmail.com', '765', 'ROLE_NORMAL', 'lisa');
# insert default customers (BCrypt: admin=123)
# For lisa=765, register via /register or update password in DB with BCrypt hash
INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES
('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1),
('765, 5th Avenue', 'lisa@gmail.com', '765', 'ROLE_NORMAL', 'lisa', 1);
Comment on lines +37 to +41

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default user lisa is inserted with a plain-text password ('765'), but authentication now uses BCryptPasswordEncoder, which will reject non-BCrypt stored values and break login. Store a BCrypt hash for lisa too (or remove the credential from the default dataset/docs).

Suggested change
# insert default customers (BCrypt: admin=123)
# For lisa=765, register via /register or update password in DB with BCrypt hash
INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES
('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1),
('765, 5th Avenue', 'lisa@gmail.com', '765', 'ROLE_NORMAL', 'lisa', 1);
# insert default customers (BCrypt: admin=123, lisa=765)
# Both default customer passwords are stored as BCrypt hashes for compatibility with BCryptPasswordEncoder
INSERT INTO CUSTOMER(address, email, password, role, username, is_active) VALUES
('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1),
('765, 5th Avenue', 'lisa@gmail.com', '$2a$10$7EqJtq98hPqEX7fNZaFWoOHiWqD3oB8LE6wS6vKrrf5wM9vY7Gk8W', 'ROLE_NORMAL', 'lisa', 1);

Copilot uses AI. Check for mistakes.

# create the product table
CREATE TABLE IF NOT EXISTS PRODUCT(
Expand Down
10 changes: 10 additions & 0 deletions JtProject/fix_admin_password.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Fix admin password for BCrypt authentication
-- Run this in your ecomjava database if admin/123 login fails
-- This updates the admin user's password to BCrypt hash of "123"

USE ecomjava;

UPDATE CUSTOMER
SET password = '$2a$10$Gqo6QT80w8V9jOd0hUlA2uj2lDe3bwNgz.eMP8UwnsB6zMVJ2hYh6',
is_active = 1
WHERE username = 'admin';
4 changes: 4 additions & 0 deletions JtProject/insert_admin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
USE ecomjava;

INSERT INTO customer (address, email, password, role, username, is_active) VALUES

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script inserts into customer (lowercase), but the schema bootstrap creates CUSTOMER (uppercase). On MySQL installations with case-sensitive table names (common on Linux depending on lower_case_table_names), this will fail. Use consistent table casing in all SQL scripts.

Suggested change
INSERT INTO customer (address, email, password, role, username, is_active) VALUES
INSERT INTO CUSTOMER (address, email, password, role, username, is_active) VALUES

Copilot uses AI. Check for mistakes.
('123, Albany Street', 'admin@nyan.cat', '$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG', 'ROLE_ADMIN', 'admin', 1);
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import java.util.Properties;

import javax.sql.DataSource;
import javax.persistence.EntityManagerFactory;

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import javax.persistence.EntityManagerFactory; is added but not used in this class. Removing unused imports keeps the configuration class easier to maintain and avoids confusion about which persistence API is intended here.

Suggested change
import javax.persistence.EntityManagerFactory;

Copilot uses AI. Check for mistakes.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
Expand Down Expand Up @@ -68,5 +71,19 @@ public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(PACKAGES_TO_SCAN);
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Properties props = new Properties();
props.put("hibernate.dialect", DIALECT);
props.put("hibernate.show_sql", SHOW_SQL);
props.put("hibernate.hbm2ddl.auto", HBM2DDL_AUTO);
em.setJpaProperties(props);
return em;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.jtspringproject.JtSpringProject.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import com.jtspringproject.JtSpringProject.models.User;
import com.jtspringproject.JtSpringProject.services.userService;

@Component
public class AdminDataLoader implements CommandLineRunner {

private final userService userService;
private final PasswordEncoder passwordEncoder;

public AdminDataLoader(userService userService, PasswordEncoder passwordEncoder) {
this.userService = userService;
this.passwordEncoder = passwordEncoder;
}

@Override
public void run(String... args) {
try {
User admin = userService.getUserByUsername("admin");
if (admin != null && !passwordEncoder.matches("123", admin.getPassword())) {
admin.setPassword(passwordEncoder.encode("123"));
admin.setActive(true);
Comment on lines +24 to +27

Copilot AI Apr 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding the admin username/password here (admin / 123) effectively resets credentials at startup and is unsafe outside local/dev usage. Gate this behind an explicit property/profile and source the password from configuration rather than embedding it in code.

Copilot uses AI. Check for mistakes.
userService.updateUser(admin);
System.out.println(">>> Admin password updated.");
}
} catch (Exception e) {
System.err.println("AdminDataLoader: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SecurityFilterChain adminFilterChain(HttpSecurity http) throws Exception {
http.antMatcher("/admin/**")
.authorizeHttpRequests(requests -> requests
.requestMatchers(new AntPathRequestMatcher("/admin/login")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/loginvalidate")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/**")).hasRole("ADMIN")
)
.formLogin(login -> login
Expand Down Expand Up @@ -93,11 +94,13 @@ UserDetailsService userDetailsService() {
if(user == null) {
throw new UsernameNotFoundException("User with username " + username + " not found.");
}
if(!user.isActive()) {
throw new UsernameNotFoundException("User account is deactivated.");
}
String role = user.getRole().equals("ROLE_ADMIN") ? "ADMIN":"USER";

return org.springframework.security.core.userdetails.User
.withUsername(username)
.passwordEncoder(input->passwordEncoder().encode(input))
.password(user.getPassword())
.roles(role)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.jtspringproject.JtSpringProject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jtspringproject.JtSpringProject.models.Category;
import com.jtspringproject.JtSpringProject.services.categoryService;

@Controller
@RequestMapping("/admin")
public class AdminCategoryController {

private final categoryService categoryService;

@Autowired
public AdminCategoryController(categoryService categoryService) {
this.categoryService = categoryService;
}

@GetMapping("categories")
public ModelAndView getCategories() {
ModelAndView mView = new ModelAndView("categories");
List<Category> categories = this.categoryService.getCategories();
mView.addObject("categories", categories);
return mView;
}

@PostMapping("/categories")
public String addCategory(@RequestParam("categoryname") String categoryName) {
this.categoryService.addCategory(categoryName);
return "redirect:categories";
}

@GetMapping("categories/delete")
public String removeCategory(@RequestParam("id") int id) {
this.categoryService.deleteCategory(id);
return "redirect:/admin/categories";
}

@GetMapping("categories/update")
public String updateCategory(@RequestParam("categoryid") int id, @RequestParam("categoryname") String categoryName) {
this.categoryService.updateCategory(id, categoryName);
return "redirect:/admin/categories";
}
}
Loading