mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	Login fixes (#881)
* init * user and pass to just pass lang update * session management fixes and avoid demo user locking * Hardening suggestions for Stirling-PDF / loginFixes (#882) Switch order of literals to prevent NullPointerException Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com> --------- Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									67e4d6e3a2
								
							
						
					
					
						commit
						9246b42057
					
				@ -1,6 +1,7 @@
 | 
				
			|||||||
package stirling.software.SPDF.config.security;
 | 
					package stirling.software.SPDF.config.security;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.util.Optional;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
import org.springframework.security.authentication.BadCredentialsException;
 | 
					import org.springframework.security.authentication.BadCredentialsException;
 | 
				
			||||||
@ -12,15 +13,19 @@ import org.springframework.stereotype.Component;
 | 
				
			|||||||
import jakarta.servlet.ServletException;
 | 
					import jakarta.servlet.ServletException;
 | 
				
			||||||
import jakarta.servlet.http.HttpServletRequest;
 | 
					import jakarta.servlet.http.HttpServletRequest;
 | 
				
			||||||
import jakarta.servlet.http.HttpServletResponse;
 | 
					import jakarta.servlet.http.HttpServletResponse;
 | 
				
			||||||
 | 
					import stirling.software.SPDF.model.User;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Component
 | 
					@Component
 | 
				
			||||||
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
 | 
					public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Autowired private final LoginAttemptService loginAttemptService;
 | 
					    @Autowired private final LoginAttemptService loginAttemptService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Autowired
 | 
					    @Autowired private final UserService userService; // Inject the UserService
 | 
				
			||||||
    public CustomAuthenticationFailureHandler(LoginAttemptService loginAttemptService) {
 | 
					
 | 
				
			||||||
 | 
					    public CustomAuthenticationFailureHandler(
 | 
				
			||||||
 | 
					            LoginAttemptService loginAttemptService, UserService userService) {
 | 
				
			||||||
        this.loginAttemptService = loginAttemptService;
 | 
					        this.loginAttemptService = loginAttemptService;
 | 
				
			||||||
 | 
					        this.userService = userService;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
@ -33,17 +38,27 @@ public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationF
 | 
				
			|||||||
        logger.error("Failed login attempt from IP: " + ip);
 | 
					        logger.error("Failed login attempt from IP: " + ip);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String username = request.getParameter("username");
 | 
					        String username = request.getParameter("username");
 | 
				
			||||||
 | 
					        if (!isDemoUser(username)) {
 | 
				
			||||||
            if (loginAttemptService.loginAttemptCheck(username)) {
 | 
					            if (loginAttemptService.loginAttemptCheck(username)) {
 | 
				
			||||||
                setDefaultFailureUrl("/login?error=locked");
 | 
					                setDefaultFailureUrl("/login?error=locked");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
            if (exception.getClass().isAssignableFrom(BadCredentialsException.class)) {
 | 
					                if (exception.getClass().isAssignableFrom(LockedException.class)) {
 | 
				
			||||||
                setDefaultFailureUrl("/login?error=badcredentials");
 | 
					 | 
				
			||||||
            } else if (exception.getClass().isAssignableFrom(LockedException.class)) {
 | 
					 | 
				
			||||||
                    setDefaultFailureUrl("/login?error=locked");
 | 
					                    setDefaultFailureUrl("/login?error=locked");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (exception.getClass().isAssignableFrom(BadCredentialsException.class)) {
 | 
				
			||||||
 | 
					            setDefaultFailureUrl("/login?error=badcredentials");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        super.onAuthenticationFailure(request, response, exception);
 | 
					        super.onAuthenticationFailure(request, response, exception);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private boolean isDemoUser(String username) {
 | 
				
			||||||
 | 
					        Optional<User> user = userService.findByUsername(username);
 | 
				
			||||||
 | 
					        return user.isPresent()
 | 
				
			||||||
 | 
					                && user.get().getAuthorities().stream()
 | 
				
			||||||
 | 
					                        .anyMatch(authority -> "ROLE_DEMO_USER".equals(authority.getAuthority()));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -21,6 +21,7 @@ import org.springframework.security.web.authentication.rememberme.PersistentToke
 | 
				
			|||||||
import org.springframework.security.web.savedrequest.NullRequestCache;
 | 
					import org.springframework.security.web.savedrequest.NullRequestCache;
 | 
				
			||||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 | 
					import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import jakarta.servlet.http.HttpSession;
 | 
				
			||||||
import stirling.software.SPDF.repository.JPATokenRepositoryImpl;
 | 
					import stirling.software.SPDF.repository.JPATokenRepositoryImpl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Configuration
 | 
					@Configuration
 | 
				
			||||||
@ -78,7 +79,7 @@ public class SecurityConfiguration {
 | 
				
			|||||||
                                            .defaultSuccessUrl("/")
 | 
					                                            .defaultSuccessUrl("/")
 | 
				
			||||||
                                            .failureHandler(
 | 
					                                            .failureHandler(
 | 
				
			||||||
                                                    new CustomAuthenticationFailureHandler(
 | 
					                                                    new CustomAuthenticationFailureHandler(
 | 
				
			||||||
                                                            loginAttemptService))
 | 
					                                                            loginAttemptService, userService))
 | 
				
			||||||
                                            .permitAll())
 | 
					                                            .permitAll())
 | 
				
			||||||
                    .requestCache(requestCache -> requestCache.requestCache(new NullRequestCache()))
 | 
					                    .requestCache(requestCache -> requestCache.requestCache(new NullRequestCache()))
 | 
				
			||||||
                    .logout(
 | 
					                    .logout(
 | 
				
			||||||
@ -87,7 +88,19 @@ public class SecurityConfiguration {
 | 
				
			|||||||
                                                    new AntPathRequestMatcher("/logout"))
 | 
					                                                    new AntPathRequestMatcher("/logout"))
 | 
				
			||||||
                                            .logoutSuccessUrl("/login?logout=true")
 | 
					                                            .logoutSuccessUrl("/login?logout=true")
 | 
				
			||||||
                                            .invalidateHttpSession(true) // Invalidate session
 | 
					                                            .invalidateHttpSession(true) // Invalidate session
 | 
				
			||||||
                                            .deleteCookies("JSESSIONID", "remember-me"))
 | 
					                                            .deleteCookies("JSESSIONID", "remember-me")
 | 
				
			||||||
 | 
					                                            .addLogoutHandler(
 | 
				
			||||||
 | 
					                                                    (request, response, authentication) -> {
 | 
				
			||||||
 | 
					                                                        HttpSession session =
 | 
				
			||||||
 | 
					                                                                request.getSession(
 | 
				
			||||||
 | 
					                                                                        false); 
 | 
				
			||||||
 | 
					                                                        if (session != null) {
 | 
				
			||||||
 | 
					                                                            String sessionId = session.getId();
 | 
				
			||||||
 | 
					                                                            sessionRegistry()
 | 
				
			||||||
 | 
					                                                                    .removeSessionInformation(
 | 
				
			||||||
 | 
					                                                                            sessionId);
 | 
				
			||||||
 | 
					                                                        }
 | 
				
			||||||
 | 
					                                                    }))
 | 
				
			||||||
                    .rememberMe(
 | 
					                    .rememberMe(
 | 
				
			||||||
                            rememberMeConfigurer ->
 | 
					                            rememberMeConfigurer ->
 | 
				
			||||||
                                    rememberMeConfigurer // Use the configurator directly
 | 
					                                    rememberMeConfigurer // Use the configurator directly
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user