Skip to content

Commit 863d956

Browse files
committed
send email, register, admin
1 parent 139a700 commit 863d956

36 files changed

+22036
-2044
lines changed

code/backend/pom.xml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,53 @@
174174
<groupId>org.springframework.boot</groupId>
175175
<artifactId>spring-boot-starter-security</artifactId>
176176
</dependency>
177+
<dependency>
178+
<groupId>com.google.api-client</groupId>
179+
<artifactId>google-api-client</artifactId>
180+
<version>2.0.0</version>
181+
</dependency>
182+
183+
<dependency>
184+
<groupId>com.google.oauth-client</groupId>
185+
<artifactId>google-oauth-client-jetty</artifactId>
186+
<version>1.34.1</version>
187+
</dependency>
188+
189+
<dependency>
190+
<groupId>jakarta.servlet</groupId>
191+
<artifactId>jakarta.servlet-api</artifactId>
192+
<version>5.0.0</version>
193+
<scope>provided</scope>
194+
</dependency>
195+
<dependency>
196+
<groupId>javax.servlet</groupId>
197+
<artifactId>javax.servlet-api</artifactId>
198+
<version>4.0.1</version>
199+
<scope>provided</scope>
200+
</dependency>
201+
<dependency>
202+
<groupId>com.google.apis</groupId>
203+
<artifactId>google-api-services-gmail</artifactId>
204+
<version>v1-rev20240520-2.0.0</version>
205+
</dependency>
206+
207+
<dependency>
208+
<groupId>com.google.auth</groupId>
209+
<artifactId>google-auth-library-oauth2-http</artifactId>
210+
<version>1.1.0</version>
211+
</dependency>
212+
213+
<dependency>
214+
<groupId>org.projectlombok</groupId>
215+
<artifactId>lombok</artifactId>
216+
<version>1.18.34</version>
217+
</dependency>
218+
<!-- https://mvnrepository.com/artifact/jakarta.mail/jakarta.mail-api -->
219+
<dependency>
220+
<groupId>jakarta.mail</groupId>
221+
<artifactId>jakarta.mail-api</artifactId>
222+
<version>2.1.3</version>
223+
</dependency>
177224

178225
</dependencies>
179226

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,90 @@
11
package edu.bu.cs673.secondhand.config;
2+
//
3+
//import org.springframework.context.annotation.Bean;
4+
//import org.springframework.context.annotation.Configuration;
5+
//import org.springframework.http.HttpMethod;
6+
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9+
//import org.springframework.security.crypto.password.PasswordEncoder;
10+
//import org.springframework.security.web.SecurityFilterChain;
11+
//
12+
////@Configuration
13+
////@EnableWebSecurity
14+
//public class SecurityConfig {
15+
//
16+
// @Bean
17+
// public PasswordEncoder passwordEncoder() {
18+
// return new BCryptPasswordEncoder();
19+
// }
20+
//
21+
// @Bean
22+
// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
23+
// http
24+
// .csrf().disable()
25+
// .authorizeHttpRequests((requests) -> requests
26+
// .requestMatchers(HttpMethod.GET, "/user/activate").permitAll()
27+
// .requestMatchers("/user/**").permitAll()
28+
// .anyRequest().authenticated()
29+
// );
30+
// return http.build();
31+
// }
32+
//}
233

34+
/***
35+
Email: la1993@bu.edu
36+
DateTime: 10/18/24-14:24
37+
*****/
338
import org.springframework.context.annotation.Bean;
439
import org.springframework.context.annotation.Configuration;
5-
import org.springframework.http.HttpMethod;
640
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
741
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
9-
import org.springframework.security.crypto.password.PasswordEncoder;
1042
import org.springframework.security.web.SecurityFilterChain;
43+
import org.springframework.web.cors.CorsConfiguration;
44+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
45+
import org.springframework.web.filter.CorsFilter;
1146

12-
//@Configuration
13-
//@EnableWebSecurity
14-
public class SecurityConfig {
47+
import java.util.List;
1548

16-
@Bean
17-
public PasswordEncoder passwordEncoder() {
18-
return new BCryptPasswordEncoder();
19-
}
49+
@Configuration
50+
@EnableWebSecurity
51+
public class SecurityConfig {
2052

2153
@Bean
2254
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
2355
http
24-
.csrf().disable()
25-
.authorizeHttpRequests((requests) -> requests
26-
.requestMatchers(HttpMethod.GET, "/user/activate").permitAll()
27-
.requestMatchers("/user/**").permitAll()
28-
.anyRequest().authenticated()
29-
);
56+
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
57+
.csrf(csrf -> csrf.disable())
58+
.authorizeHttpRequests(authorize -> authorize
59+
.requestMatchers("/**").permitAll()
60+
);
61+
3062
return http.build();
3163
}
64+
65+
@Bean
66+
public CorsFilter corsFilter() {
67+
CorsConfiguration config = new CorsConfiguration();
68+
config.setAllowedOrigins(List.of("http://localhost:8081"));
69+
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
70+
config.setAllowedHeaders(List.of("*"));
71+
config.setAllowCredentials(true);
72+
73+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
74+
source.registerCorsConfiguration("/**", config);
75+
return new CorsFilter(source);
76+
}
77+
78+
private UrlBasedCorsConfigurationSource corsConfigurationSource() {
79+
CorsConfiguration config = new CorsConfiguration();
80+
config.setAllowedOrigins(List.of("http://localhost:8081"));
81+
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
82+
config.setAllowedHeaders(List.of("*"));
83+
config.setAllowCredentials(true);
84+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
85+
source.registerCorsConfiguration("/**", config);
86+
return source;
87+
}
3288
}
89+
90+

code/backend/src/main/java/edu/bu/cs673/secondhand/controller/UserController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public ResultVo completeReset(@RequestBody ResetPasswordRequest request) {
164164
if (request == null || request.getEmail() == null || request.getResetToken() == null || request.getNewPassword() == null) {
165165
return ResultVo.fail("Email, reset token, and new password are required.");
166166
}
167-
168167
try {
169168
boolean result = userServiceInterface.resetPassword(request.getEmail(), request.getResetToken(), request.getNewPassword());
170169
if (result) {

code/backend/src/main/java/edu/bu/cs673/secondhand/controller/UserControllerLegacy.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,32 @@ public class UserControllerLegacy {
3535
@PostMapping("register")
3636
public ResultVo signIn(@RequestBody User userModel) {
3737
logger.info(userModel.toString());
38+
3839
userModel.setSignInTime(new Timestamp(System.currentTimeMillis()));
3940
if (userModel.getAvatar() == null || "".equals(userModel.getAvatar())) {
4041
userModel.setAvatar("https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
4142
}
4243
if (userService.userSignIn(userModel)) {
44+
userService.insertActiveCode(userModel.getEmail());
45+
userService.sendVerificationEmail(userModel);
46+
System.out.println(userModel.getEmail()+"Email has been sent");
4347
return ResultVo.success(userModel);
4448
}
4549
return ResultVo.fail(ErrorMsg.REGISTER_ERROR);
4650
}
4751

52+
@PostMapping("/verifyCode")
53+
public ResultVo verifyCode(@RequestBody User userModel) {
54+
System.out.println("Email is "+userModel.getEmail() +"code is" + userModel.getActiveCode());
55+
boolean isVerified = userService.verifyCode(userModel);
56+
System.out.println(isVerified);
57+
if (isVerified) {
58+
return ResultVo.success("true");
59+
} else {
60+
return ResultVo.fail("Invalid verification code.");
61+
}
62+
}
63+
4864
/**
4965
*
5066
* @param email
@@ -69,11 +85,6 @@ public ResultVo login(@RequestParam("email") @NotEmpty @NotNull String email,
6985
if(email.equals("") || userPassword.equals("")){
7086
return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
7187
}
72-
73-
// if(userModel.getAccountNumber().length() != 11){
74-
// return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
75-
// }
76-
7788
if(userModel.getUserStatus()!=null&&userModel.getUserStatus().equals((byte) 1)){
7889
return ResultVo.fail(ErrorMsg.ACCOUNT_Ban);
7990
}
@@ -149,4 +160,15 @@ public ResultVo updateUserPassword(@CookieValue("shUserId") @NotNull(message = "
149160
}
150161
return ResultVo.fail(ErrorMsg.PASSWORD_RESET_ERROR);
151162
}
163+
164+
// @GetMapping("/activate")
165+
// public ResponseEntity<String> activateUser(@RequestParam("token") String token) {
166+
// boolean activated = userService.activateUserByToken(token);
167+
// if (activated) {
168+
// return ResponseEntity.ok("账号激活成功!");
169+
// } else {
170+
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("激活失败或链接已失效");
171+
// }
172+
// }
173+
152174
}

0 commit comments

Comments
 (0)