Skip to content

Commit 86663a9

Browse files
committed
# refactor: remove @autowire, remove SecurityConfigurerAdapter, change entites name, update readme file
1 parent 83d496b commit 86663a9

File tree

94 files changed

+693
-1094
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+693
-1094
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# recauction-web
1+
# Rec-Auction Web Application
22
This is an e-commerce website that uses a reverse auction mechanism. Users can place bids on the products they want to buy and the sellers will bid to find out who is the winning bidder in the auction session. The website aims to provide a platform for buyers and sellers to interact and transact in a fair, transparent, and competitive environment.
33
Website include: Website a user interface for customers and a CMS for admin and vendor
4-
# tech
4+
# Tech
55
- JDK 11
66
- MySQL
77
- Spring Framework:
@@ -13,3 +13,51 @@ Website include: Website a user interface for customers and a CMS for admin and
1313
- Cloudinary for upload image
1414
- Paypal Sandbox for payment service
1515
- Fontend: Boostrap + JQuery and more js library ...
16+
17+
# Getting Started
18+
- Clone the project
19+
- Import the project into your IDE (Recommend IntelliJ IDEA)
20+
- Put your configuration in application.yml
21+
- Database configuration
22+
- (**Imortant**) Email configuration (username and password)
23+
- Paypal configuration (app and secret)
24+
- Cloudinary configuration (cloud name, api key, api secret)
25+
- Create database schema name `recauction_db` in MySQL by using command `create database recauction_db;`
26+
- Run the project
27+
- Run the project with the main class `RecAuctionWebApplication` by IDE
28+
- Or use maven build `.\mvnw clean package -DskipTest`
29+
- Then run the jar file: `java -jar target/recauction-web-0.0.1-SNAPSHOT.jar`
30+
- After the project is started, the database schema will be created automatically. You need to run `recauction_db.sql` to insert some initial data (categories, product_tags)
31+
- Access the website at `http://localhost:8080/`
32+
33+
# How to use (primary features)
34+
- Create Admin account
35+
- Step 1: Register as User and verify your email
36+
- Step 2: Go to `user` table in database and update the `role_id` field to `1`
37+
- Step 3: Login new admin account and go to `/admin` page
38+
39+
- Create Supplier account
40+
- Step 1: Register as User and verify your email
41+
- Step 2: Go to Account Page and click `Become a Supplier` and fill in the form
42+
- Step 3: Login new supplier account and go to `/vendor` page
43+
- Step 4: Add product to your inventory (if you don't have any product, you can't attend any bid session)
44+
- Pay in wallet via Paypal
45+
- Step 1: Login as User
46+
- Step 2: Go to `Wallet` page
47+
- Step 3: Click `Pay in via Paypal` and choose amount to pay
48+
- Step 4: You will be redirected to Paypal payment page
49+
- Step 5: Login to your Paypal account and complete the payment
50+
- Step 6: You will be redirected back to the website and your wallet will be updated
51+
- Create a bid session
52+
- **Note**: Before creating a bid session, you need to have enough account balance to pay for the bid (30% of the product price), and you must have at least address.
53+
- Step 1: Login as User
54+
- Step 2: Go to `Create Bid Session` page
55+
- Step 3: Fill in the form and submit
56+
57+
- Attend a bid session
58+
- Step 1: Login as Supplier
59+
- Step 2: Go to `Bid Session` in home page
60+
- Step 3: Find the bid session you want to attend and click `Attend`
61+
- Step 4: Place your bid
62+
- **Note**: You can only place a bid if you have product in your inventory and that product is matched with the bid session (base on category and product tag)
63+

recauction_db.sql

Lines changed: 1 addition & 461 deletions
Large diffs are not rendered by default.

src/main/java/com/ec/recauctionec/RecauctionEcApplication.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,4 @@ public static void main(String[] args) {
1515
SpringApplication.run(RecauctionEcApplication.class, args);
1616
}
1717

18-
@Bean
19-
public Cloudinary cloudinary() {
20-
Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
21-
"cloud_name", "dddb8btv0",
22-
"api_key", "159138865977743",
23-
"api_secret", "xz-CUQykgKnBja571VNtfhX2gsU",
24-
"secure",true
25-
));
26-
return cloudinary;
27-
}
28-
2918
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ec.recauctionec.configs;
2+
3+
import com.cloudinary.Cloudinary;
4+
import com.cloudinary.utils.ObjectUtils;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class CloudinarySDK {
11+
@Value("${cloudinary.cloud_name}")
12+
private String cloudName;
13+
14+
@Value("${cloudinary.api_key}")
15+
private String apiKey;
16+
17+
@Value("${cloudinary.api_secret}")
18+
private String apiSecret;
19+
20+
@Bean
21+
public Cloudinary cloudinary() {
22+
return new Cloudinary(ObjectUtils.asMap(
23+
"cloud_name", cloudName,
24+
"api_key", apiKey,
25+
"api_secret", apiSecret,
26+
"secure", true
27+
));
28+
}
29+
}

src/main/java/com/ec/recauctionec/configs/WebSecurityConfig.java

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,99 @@
11
package com.ec.recauctionec.configs;
22

3+
import com.ec.recauctionec.data.repositories.UserRepo;
34
import com.ec.recauctionec.services.impl.UserDetailsServiceImpl;
5+
import org.springframework.beans.factory.annotation.Qualifier;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.security.authentication.AuthenticationManager;
69
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
710
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
811
import org.springframework.security.config.annotation.web.builders.WebSecurity;
912
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1013
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
14+
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
1115
import org.springframework.security.core.userdetails.UserDetailsService;
1216
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
17+
import org.springframework.security.web.SecurityFilterChain;
1318
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
1419

1520
@Configuration
1621
@EnableWebSecurity
17-
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
22+
public class WebSecurityConfig {
1823
@Bean
19-
public UserDetailsService userDetailsService() {
20-
return new UserDetailsServiceImpl();
24+
public UserDetailsService userDetailsService(UserRepo userRepo) {
25+
return new UserDetailsServiceImpl(userRepo);
2126
}
2227

2328
@Bean
2429
public BCryptPasswordEncoder passwordEncoder() {
2530
return new BCryptPasswordEncoder();
2631
}
2732

33+
@Bean
34+
public AuthenticationManager authenticationManager(HttpSecurity http, BCryptPasswordEncoder bCryptPasswordEncoder,
35+
UserDetailsService userDetailService) throws Exception {
36+
return http.getSharedObject(AuthenticationManagerBuilder.class)
37+
.userDetailsService(userDetailService)
38+
.passwordEncoder(bCryptPasswordEncoder)
39+
.and()
40+
.build();
41+
}
2842

29-
@Override
30-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
31-
auth.userDetailsService(userDetailsService())
32-
.passwordEncoder(passwordEncoder());
43+
@Bean
44+
public WebSecurityCustomizer webSecurityCustomizer() {
45+
return web -> web.ignoring().antMatchers("/resources/**");
3346
}
3447

35-
@Override
36-
public void configure(WebSecurity web) throws Exception {
37-
web
38-
.ignoring()
39-
.antMatchers("/resources/**");
48+
@Bean
49+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
50+
http.authorizeRequests()
51+
.antMatchers("/",
52+
"/dang-ky",
53+
"/dang-nhap",
54+
"/trang-chu",
55+
"/danh-muc",
56+
"/tat-ca-phien",
57+
"/san-pham").permitAll()
58+
.antMatchers("/tai-khoan/**",
59+
"/dau-gia/**",
60+
"/don-hang/**",
61+
"/thanh-toan/**",
62+
"/api/v1/supplier/**",
63+
"/api/v1/admin/**").authenticated()
64+
.and()
65+
.logout()
66+
.invalidateHttpSession(true)
67+
.clearAuthentication(true)
68+
.logoutRequestMatcher(new AntPathRequestMatcher("/dang-xuat"))
69+
.logoutSuccessUrl("/trang-chu")
70+
.permitAll();
71+
http.formLogin().loginPage("/dang-nhap")
72+
.usernameParameter("email")
73+
.passwordParameter("password");
74+
http.formLogin()
75+
.defaultSuccessUrl("/")
76+
.failureUrl("/dang-nhap?error=true");
77+
http.csrf().disable();
78+
79+
//authorize
80+
//admin
81+
http.authorizeRequests()
82+
.antMatchers("/admin/**",
83+
"/api/v1/admin/**")
84+
.access("hasRole('ADMIN')");
85+
//supplier
86+
http.authorizeRequests()
87+
.antMatchers("/supplier/**",
88+
"/api/v1/supplier/**")
89+
.access("hasRole('SUPPLIER')");
90+
//handle when user not have permission
91+
http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");
92+
93+
return http.build();
4094
}
4195

42-
@Override
43-
protected void configure(HttpSecurity http) throws Exception {
96+
/*protected void configure(HttpSecurity http) throws Exception {
4497
//authentication
4598
http.authorizeRequests()
4699
.antMatchers("/",
@@ -86,6 +139,6 @@ protected void configure(HttpSecurity http) throws Exception {
86139
//handle when user not have permission
87140
http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");
88141
89-
}
142+
}*/
90143

91144
}

src/main/java/com/ec/recauctionec/controller/AccountController.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import com.ec.recauctionec.data.entities.*;
44
import com.ec.recauctionec.data.repositories.UserAddressRepo;
5-
import com.ec.recauctionec.data.repositories.WalletHistoryRepo;
5+
import com.ec.recauctionec.data.repositories.WalletTransactionRepo;
66
import com.ec.recauctionec.data.repositories.WalletRepo;
77
import com.ec.recauctionec.services.SupplierService;
88
import com.ec.recauctionec.services.UserService;
9-
import org.springframework.beans.factory.annotation.Autowired;
9+
import lombok.RequiredArgsConstructor;
1010
import org.springframework.format.annotation.DateTimeFormat;
1111
import org.springframework.security.core.Authentication;
1212
import org.springframework.security.core.context.SecurityContextHolder;
@@ -19,18 +19,13 @@
1919

2020
@Controller
2121
@RequestMapping(value = "/tai-khoan")
22+
@RequiredArgsConstructor
2223
public class AccountController {
23-
@Autowired
24-
UserService userService;
25-
@Autowired
26-
UserAddressRepo addressRepo;
27-
@Autowired
28-
private WalletRepo walletRepo;
29-
@Autowired
30-
private WalletHistoryRepo historyRepo;
31-
@Autowired
32-
private SupplierService supplierService;
33-
24+
final UserService userService;
25+
final UserAddressRepo addressRepo;
26+
private final WalletRepo walletRepo;
27+
private final WalletTransactionRepo historyRepo;
28+
private final SupplierService supplierService;
3429
private Authentication auth;
3530

3631
@RequestMapping(value = {"/thong-tin", ""}, method = RequestMethod.GET)
@@ -72,9 +67,9 @@ public String getWalletPage(@RequestParam(value = "filter", required = false)
7267
User us = ((CustomUserDetails) auth.getPrincipal()).getUser();
7368
//get wallet from user
7469
Wallet wallet = walletRepo.findByUserId(us.getUserId()).get(0);
75-
WalletHistory recent = historyRepo.findTop1ByWalletOrderByCreateDateDesc(wallet);
70+
WalletTransaction recent = historyRepo.findTop1ByWalletOrderByCreatedDateDesc(wallet);
7671
//if date query is null get 5 transaction recent
77-
List<WalletHistory> logs = date == null ?
72+
List<WalletTransaction> logs = date == null ?
7873
historyRepo.find5RecentLogByWallet(wallet.getWalletId())
7974
: historyRepo.findLogByDate(date, wallet.getWalletId());
8075
modelMap.addAttribute("logs", logs);

src/main/java/com/ec/recauctionec/controller/BidController.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.ec.recauctionec.data.entities.*;
55
import com.ec.recauctionec.data.variable.Router;
66
import com.ec.recauctionec.services.*;
7-
import org.springframework.beans.factory.annotation.Autowired;
7+
import lombok.RequiredArgsConstructor;
88
import org.springframework.http.HttpStatus;
99
import org.springframework.http.ResponseEntity;
1010
import org.springframework.security.core.Authentication;
@@ -19,20 +19,16 @@
1919
import java.util.List;
2020

2121
@Controller
22+
@RequiredArgsConstructor
2223
@RequestMapping(value = "/dau-gia")
2324
public class BidController {
24-
@Autowired
25-
private CategoryService categoryService;
26-
@Autowired
27-
private ProductTagService productTagService;
28-
@Autowired
29-
private BidService bidService;
30-
@Autowired
31-
private UserService userService;
32-
@Autowired
33-
private BidJoinService joinService;
34-
@Autowired
35-
private ProductService productService;
25+
26+
private final CategoryService categoryService;
27+
private final ProductTagService productTagService;
28+
private final BidService bidService;
29+
private final UserService userService;
30+
private final BidParticipantService joinService;
31+
private final ProductService productService;
3632
private Authentication auth;
3733

3834

@@ -67,13 +63,13 @@ public String joinAuction(@RequestParam("auctionId") int auctionId,
6763
Product product = productService.findById(productId);
6864
if (product.getSupplier()
6965
.getUser().getUserId() != auction.getUser().getUserId()) {
70-
BidJoin bidJoin = new BidJoin();
71-
bidJoin.setPrice(price);
72-
bidJoin.setProduct(product);
73-
bidJoin.setBid(auction);
74-
bidJoin.setTime(new Timestamp(new java.util.Date().getTime()));
75-
bidJoin.setStatus(BidJoin.ACTIVE);
76-
joinService.joinAuction(bidJoin);
66+
BidParticipant bidParticipant = new BidParticipant();
67+
bidParticipant.setPrice(price);
68+
bidParticipant.setProduct(product);
69+
bidParticipant.setBid(auction);
70+
bidParticipant.setTime(new Timestamp(new java.util.Date().getTime()));
71+
bidParticipant.setStatus(BidParticipant.ACTIVE);
72+
joinService.joinAuction(bidParticipant);
7773
}
7874
return "redirect:/chi-tiet-dau-gia/" + auctionId;
7975
}
@@ -98,8 +94,8 @@ public ResponseEntity getJoinAuction(@RequestParam("auctionId") int auctionId,
9894
auth = SecurityContextHolder.getContext().getAuthentication();
9995
User us = ((CustomUserDetails) auth.getPrincipal()).getUser();
10096
Bid auction = bidService.findById(auctionId);
101-
List<BidJoin> joins = new ArrayList<>(auction.getBidId());
102-
for (BidJoin j : joins) {
97+
List<BidParticipant> joins = new ArrayList<>(auction.getBidId());
98+
for (BidParticipant j : joins) {
10399
if (j.getProduct()
104100
.getSupplier()
105101
.getUser().getUserId() == us.getUserId()) {

src/main/java/com/ec/recauctionec/controller/CategoryController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.ec.recauctionec.data.entities.Product;
44
import com.ec.recauctionec.services.ProductService;
5+
import lombok.RequiredArgsConstructor;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.stereotype.Controller;
78
import org.springframework.ui.ModelMap;
@@ -12,10 +13,10 @@
1213
import java.util.List;
1314

1415
@Controller
16+
@RequiredArgsConstructor
1517
@RequestMapping(value = "/danh-muc-san-pham")
1618
public class CategoryController {
17-
@Autowired
18-
private ProductService productService;
19+
private final ProductService productService;
1920

2021
@RequestMapping(value = "", method = RequestMethod.GET)
2122
public String getProduct(@RequestParam("id") int id,

0 commit comments

Comments
 (0)