2023-11-27 21:08:28 -08:00
|
|
|
package com.wisemapping.config.rest;
|
|
|
|
|
2024-02-04 19:45:14 -08:00
|
|
|
import com.wisemapping.filter.JwtAuthenticationFilter;
|
2024-01-20 14:51:46 -08:00
|
|
|
import com.wisemapping.rest.MindmapController;
|
2024-01-21 15:42:02 -08:00
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2023-11-27 21:08:28 -08:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2024-02-04 19:45:14 -08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-01-20 14:51:46 -08:00
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
2023-11-27 21:08:28 -08:00
|
|
|
import org.springframework.context.annotation.Bean;
|
2024-01-21 15:18:07 -08:00
|
|
|
import org.springframework.context.annotation.Import;
|
2023-11-27 21:08:28 -08:00
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
2024-01-21 15:18:07 -08:00
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
2023-11-27 21:08:28 -08:00
|
|
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
2024-02-04 19:45:14 -08:00
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
2023-11-27 21:08:28 -08:00
|
|
|
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
|
|
|
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
|
|
|
|
2024-01-15 07:59:04 -08:00
|
|
|
import static org.springframework.security.config.Customizer.withDefaults;
|
|
|
|
|
2023-11-27 21:08:28 -08:00
|
|
|
|
2024-01-21 15:42:02 -08:00
|
|
|
@SpringBootApplication(scanBasePackageClasses = MindmapController.class)
|
2024-02-03 15:35:09 -08:00
|
|
|
@Import({InterceptorsConfig.class})
|
2024-01-21 15:18:07 -08:00
|
|
|
@EnableWebSecurity
|
2023-11-27 21:08:28 -08:00
|
|
|
public class RestAppConfig {
|
2024-02-04 19:45:14 -08:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private JwtAuthenticationFilter jwtAuthenticationFilter;
|
|
|
|
|
2023-11-27 21:08:28 -08:00
|
|
|
@Bean
|
2024-01-15 16:36:29 -08:00
|
|
|
MvcRequestMatcher.Builder mvc(HandlerMappingIntrospector introspector) {
|
|
|
|
return new MvcRequestMatcher.Builder(introspector);
|
|
|
|
}
|
2024-01-20 14:51:46 -08:00
|
|
|
|
2024-01-15 16:36:29 -08:00
|
|
|
@Bean
|
|
|
|
SecurityFilterChain apiSecurityFilterChain(@NotNull final HttpSecurity http, @NotNull final MvcRequestMatcher.Builder mvc) throws Exception {
|
|
|
|
return http
|
2024-01-21 15:42:02 -08:00
|
|
|
.securityMatcher("/**")
|
2024-02-04 19:45:14 -08:00
|
|
|
.addFilterAfter(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
|
2024-01-15 16:36:29 -08:00
|
|
|
.authorizeHttpRequests(auth -> auth
|
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/users/")).permitAll()
|
2024-02-04 18:28:23 -08:00
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/authenticate")).permitAll()
|
2024-01-15 16:36:29 -08:00
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/users/resetPassword")).permitAll()
|
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/oauth2/googlecallback")).permitAll()
|
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/oauth2/confirmaccountsync")).permitAll()
|
|
|
|
.requestMatchers(mvc.pattern("/api/restfull/admin/**")).hasAnyRole("ADMIN")
|
|
|
|
.requestMatchers(mvc.pattern("/**")).hasAnyRole("USER", "ADMIN")
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
)
|
2024-01-21 15:42:02 -08:00
|
|
|
.logout(logout -> logout.permitAll()
|
|
|
|
.logoutSuccessHandler((request, response, authentication) -> {
|
|
|
|
response.setStatus(HttpServletResponse.SC_OK);
|
|
|
|
}))
|
2024-01-21 15:18:07 -08:00
|
|
|
.csrf(AbstractHttpConfigurer::disable)
|
2024-01-15 16:36:29 -08:00
|
|
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
|
.httpBasic(withDefaults())
|
|
|
|
.build();
|
2023-11-27 21:08:28 -08:00
|
|
|
}
|
|
|
|
}
|