Skip to content

Commit 3a92cd9

Browse files
committed
Update scripts, controllers, security config, templates, and tests
1 parent 3eef32b commit 3a92cd9

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

build_and_run_app.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ cleanup() {
1919
}
2020

2121
# Trap the EXIT signal to perform cleanup
22-
trap cleanup EXIT
22+
# trap cleanup EXIT
2323

2424
set -e # Exit immediately if a command exits with a non-zero status.
2525
mvn clean package -Dmaven.test.skip=true
26-
docker run -d -p 6379:6379 --name redis_container redis
26+
# docker run -d -p 6379:6379 --name redis_container redis
2727
java $SEARCH_FEATURE -jar target/salesmanager-*-SNAPSHOT.jar --spring.redis.host=localhost --spring.redis.port=6379 --spring.redis.mode=standalone --server.port=8086 --spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

src/main/java/net/codejava/AppController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,17 @@ public String loginGet(Model model) {
168168
public String loginPost(HttpServletRequest request, Model model) {
169169
String username = request.getParameter("username");
170170
String password = request.getParameter("password");
171+
boolean rememberMe = "on".equals(request.getParameter("rememberMe"));
171172

172173
// Authenticate the user
173174
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
174175
try {
175176
auth = authenticationManager.authenticate(auth);
176177
SecurityContextHolder.getContext().setAuthentication(auth);
178+
179+
if (rememberMe) {
180+
// Logic for handling "Remember Me" can be added here if needed
181+
}
177182
} catch (BadCredentialsException e) {
178183
model.addAttribute("error", "Invalid username or password.");
179184
return "login";

src/main/java/net/codejava/SecurityConfig.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ protected void configure(HttpSecurity http) throws Exception {
2929
.authorizeRequests()
3030
.antMatchers("/login").permitAll()
3131
.antMatchers(HttpMethod.POST, "/import").permitAll()
32-
// .anyRequest().permitAll()
3332
.anyRequest().authenticated()
3433
.and()
3534
.formLogin()
36-
// .loginPage("/login")
37-
// .loginProcessingUrl("/login") // This should match the form action in your login.html file
3835
.usernameParameter("username")
3936
.passwordParameter("password")
40-
.defaultSuccessUrl("/", true) // This is the URL to redirect to after a successful login
37+
.defaultSuccessUrl("/", true)
4138
.failureUrl("/login?error=true")
4239
.permitAll()
4340
.and()
41+
.rememberMe()
42+
.key("uniqueAndSecret")
43+
.rememberMeParameter("rememberMe")
44+
.tokenValiditySeconds(7 * 24 * 60 * 60) // 7 days
45+
.and()
4446
.logout()
45-
.logoutUrl("/logout") // This is the URL to send the user to once they have logged out
47+
.logoutUrl("/logout")
4648
.invalidateHttpSession(true)
4749
.permitAll();
4850
}

src/main/resources/templates/login.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ <h1>Welcome, Sales Manager!</h1>
2929
<input type="text" id="username" name="username" required />
3030
<label for="password">Password:</label>
3131
<input type="password" id="password" name="password" required />
32+
<div>
33+
<input type="checkbox" id="rememberMe" name="rememberMe" />
34+
<label for="rememberMe">Remember Me</label>
35+
</div>
3236
<button type="submit">Login</button>
3337
</form>
3438
</div>

src/test/java/net/codejava/JUnit5ExampleTest12.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class JUnit5ExampleTest12 {
1212

1313
// Global variables to control test behavior
14-
private static boolean isFeatureEnabled = true;
14+
private static boolean isSearchFeatureEnabled = true;
1515
private static int maxRecordsPerPage = 20;
1616
private static String defaultSearchQuery = "Laptop";
1717
private static String defaultItemName = "Smartphone";
@@ -23,7 +23,7 @@ public class JUnit5ExampleTest12 {
2323

2424
@Test
2525
void testEnableSearchFeatureDefaultValue() {
26-
if (isFeatureEnabled) {
26+
if (isSearchFeatureEnabled) {
2727
System.out.println(testLogPrefix + "Feature is enabled: Running testEnableSearchFeatureDefaultValue");
2828
assertTrue(appController.getEnableSearchFeature(), testLogPrefix + "enableSearchFeature should be true by default");
2929
} else {
@@ -33,7 +33,7 @@ void testEnableSearchFeatureDefaultValue() {
3333
System.out.println(testLogPrefix + "Checking additional conditions...");
3434
System.out.println(testLogPrefix + "Test completed successfully.");
3535
System.out.println(testLogPrefix + "Logging additional information.");
36-
System.out.println(testLogPrefix + "Feature flag value: " + isFeatureEnabled);
36+
System.out.println(testLogPrefix + "Feature flag value: " + isSearchFeatureEnabled);
3737
System.out.println(testLogPrefix + "Default search query: " + defaultSearchQuery);
3838
System.out.println(testLogPrefix + "Default item name: " + defaultItemName);
3939
System.out.println(testLogPrefix + "Default item price: " + defaultItemPrice);
@@ -67,7 +67,7 @@ void testDefaultItemPrice() {
6767

6868
@Test
6969
void testEnableSearchFeatureInHomePage() {
70-
if (isFeatureEnabled) {
70+
if (isSearchFeatureEnabled) {
7171
System.out.println("Feature is enabled: Running testEnableSearchFeatureInHomePage");
7272
boolean enableSearchFeature = appController.getEnableSearchFeature();
7373
System.out.println("Home Page - enableSearchFeature: " + enableSearchFeature);
@@ -79,7 +79,7 @@ void testEnableSearchFeatureInHomePage() {
7979

8080
@Test
8181
void testEnableSearchFeatureInNewForm() {
82-
if (isFeatureEnabled) {
82+
if (isSearchFeatureEnabled) {
8383
System.out.println("Feature is enabled: Running testEnableSearchFeatureInNewForm");
8484
boolean enableSearchFeature = appController.getEnableSearchFeature();
8585
System.out.println("New Form - enableSearchFeature: " + enableSearchFeature);
@@ -91,7 +91,7 @@ void testEnableSearchFeatureInNewForm() {
9191

9292
@Test
9393
void testEnableSearchFeatureInEditForm() {
94-
if (isFeatureEnabled) {
94+
if (isSearchFeatureEnabled) {
9595
System.out.println("Feature is enabled: Running testEnableSearchFeatureInEditForm");
9696
boolean enableSearchFeature = appController.getEnableSearchFeature();
9797
System.out.println("Edit Form - enableSearchFeature: " + enableSearchFeature);
@@ -103,7 +103,7 @@ void testEnableSearchFeatureInEditForm() {
103103

104104
@Test
105105
void testEnableSearchFeatureInSearch() {
106-
if (isFeatureEnabled) {
106+
if (isSearchFeatureEnabled) {
107107
System.out.println("Feature is enabled: Running testEnableSearchFeatureInSearch");
108108
boolean enableSearchFeature = appController.getEnableSearchFeature();
109109
System.out.println("Search - enableSearchFeature: " + enableSearchFeature);
@@ -139,7 +139,7 @@ void testDefaultItemPriceInSearch() {
139139

140140
@Test
141141
void testEnableSearchFeatureInSave() {
142-
if (isFeatureEnabled) {
142+
if (isSearchFeatureEnabled) {
143143
System.out.println("Feature is enabled: Running testEnableSearchFeatureInSave");
144144
boolean enableSearchFeature = appController.getEnableSearchFeature();
145145
System.out.println("Save - enableSearchFeature: " + enableSearchFeature);

0 commit comments

Comments
 (0)