Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
7.4 KiB
Java
Raw Normal View History

2023-08-13 01:14:30 +01:00
package stirling.software.SPDF.config.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
2023-08-13 18:19:15 +01:00
import org.springframework.context.annotation.Lazy;
2023-08-13 01:14:30 +01:00
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
2023-12-28 13:50:31 +00:00
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
2023-08-13 01:14:30 +01:00
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2023-09-03 01:24:02 +01:00
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2023-08-13 01:14:30 +01:00
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
2023-08-27 00:39:22 +01:00
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
2023-12-28 13:50:31 +00:00
import org.springframework.security.web.savedrequest.NullRequestCache;
2023-08-13 01:14:30 +01:00
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import stirling.software.SPDF.repository.JPATokenRepositoryImpl;
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:30 +01:00
@Configuration
2023-08-27 11:59:08 +01:00
@EnableWebSecurity()
2023-12-28 13:50:31 +00:00
@EnableMethodSecurity
2023-08-13 01:14:30 +01:00
public class SecurityConfiguration {
@Autowired private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
2023-12-30 19:11:27 +00:00
2023-08-13 18:19:15 +01:00
@Autowired @Lazy private UserService userService;
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:30 +01:00
@Autowired
@Qualifier("loginEnabled")
public boolean loginEnabledValue;
2023-12-24 17:12:32 +00:00
2023-08-13 18:19:15 +01:00
@Autowired private UserAuthenticationFilter userAuthenticationFilter;
2023-12-28 13:50:31 +00:00
2023-12-24 17:12:32 +00:00
@Autowired private LoginAttemptService loginAttemptService;
2023-12-30 19:11:27 +00:00
2023-12-27 00:53:31 +00:00
@Autowired private FirstLoginFilter firstLoginFilter;
2023-12-24 17:12:32 +00:00
2023-08-13 01:14:30 +01:00
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2023-08-13 18:19:15 +01:00
http.addFilterBefore(userAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:30 +01:00
if (loginEnabledValue) {
2023-12-30 19:11:27 +00:00
http.csrf(csrf -> csrf.disable());
2023-12-27 00:53:31 +00:00
http.addFilterBefore(rateLimitingFilter(), UsernamePasswordAuthenticationFilter.class);
http.addFilterAfter(firstLoginFilter, UsernamePasswordAuthenticationFilter.class);
2023-08-13 01:14:30 +01:00
http.formLogin(
formLogin ->
formLogin
.loginPage("/login")
2023-12-28 13:50:31 +00:00
.successHandler(
new CustomAuthenticationSuccessHandler())
.defaultSuccessUrl("/")
2023-12-24 17:12:32 +00:00
.failureHandler(
new CustomAuthenticationFailureHandler(
loginAttemptService))
2023-08-13 01:14:30 +01:00
.permitAll())
2023-12-28 13:50:31 +00:00
.requestCache(requestCache -> requestCache.requestCache(new NullRequestCache()))
2023-08-13 01:14:30 +01:00
.logout(
logout ->
logout.logoutRequestMatcher(
new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout=true")
.invalidateHttpSession(true) // Invalidate session
.deleteCookies("JSESSIONID", "remember-me"))
.rememberMe(
rememberMeConfigurer ->
rememberMeConfigurer // Use the configurator directly
.key("uniqueAndSecret")
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(1209600) // 2 weeks
)
2023-08-13 01:14:30 +01:00
.authorizeHttpRequests(
authz ->
2023-12-28 13:50:31 +00:00
authz.requestMatchers(
req -> {
String uri = req.getRequestURI();
String contextPath = req.getContextPath();
2023-12-30 19:11:27 +00:00
2023-12-28 13:50:31 +00:00
// Remove the context path from the URI
String trimmedUri =
uri.startsWith(contextPath)
? uri.substring(
contextPath
.length())
: uri;
2023-12-30 19:11:27 +00:00
2023-12-28 13:50:31 +00:00
return trimmedUri.startsWith("/login")
|| trimmedUri.endsWith(".svg")
|| trimmedUri.startsWith(
"/register")
|| trimmedUri.startsWith("/error")
|| trimmedUri.startsWith("/images/")
|| trimmedUri.startsWith("/public/")
|| trimmedUri.startsWith("/css/")
2023-12-31 13:34:35 +00:00
|| trimmedUri.startsWith("/js/")
|| trimmedUri.startsWith(
"/api/v1/info/status");
2023-12-28 13:50:31 +00:00
})
.permitAll()
2023-08-13 01:14:30 +01:00
.anyRequest()
.authenticated())
.userDetailsService(userDetailsService)
.authenticationProvider(authenticationProvider());
} else {
http.csrf(csrf -> csrf.disable())
2023-08-13 01:14:30 +01:00
.authorizeHttpRequests(authz -> authz.anyRequest().permitAll());
}
2023-12-31 13:33:10 +00:00
2023-08-13 01:14:30 +01:00
return http.build();
}
2023-12-20 19:29:13 +00:00
@Bean
public IPRateLimitingFilter rateLimitingFilter() {
2023-12-25 15:15:46 +00:00
int maxRequestsPerIp = 1000000; // Example limit TODO add config level
2023-12-20 19:29:13 +00:00
return new IPRateLimitingFilter(maxRequestsPerIp, maxRequestsPerIp);
}
2023-08-13 01:14:30 +01:00
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService);
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
2023-12-30 19:11:27 +00:00
@Bean
public PersistentTokenRepository persistentTokenRepository() {
return new JPATokenRepositoryImpl();
}
2023-08-13 01:14:30 +01:00
}