Files
wisemapping-open-source/wise-api/src/main/java/com/wisemapping/config/rest/RestAppConfig.java

72 lines
3.7 KiB
Java
Raw Normal View History

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-02-10 13:53:44 -08:00
import org.springframework.beans.factory.annotation.Value;
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;
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-02-11 12:21:38 -08:00
@SpringBootApplication(scanBasePackageClasses = {MindmapController.class, JwtAuthenticationFilter.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
2024-02-10 13:53:44 -08:00
@Value("${app.api.http-basic-enabled:false}")
private boolean enableHttpBasic;
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 {
2024-02-10 13:53:44 -08:00
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
2024-02-11 12:21:38 -08:00
.requestMatchers(mvc.pattern("/error")).permitAll()
2024-02-04 21:10:48 -08:00
.requestMatchers(mvc.pattern("/api/restful/authenticate")).permitAll()
2024-02-09 23:55:05 -08:00
.requestMatchers(mvc.pattern("/api/restful/users/")).permitAll()
2024-02-19 00:28:05 -08:00
.requestMatchers(mvc.pattern("/api/restful/app/config")).permitAll()
2024-03-25 22:40:35 -07:00
.requestMatchers(mvc.pattern("/api/restful/maps/*/metadata")).permitAll()
2024-02-09 23:55:05 -08:00
.requestMatchers(mvc.pattern("/api/restful/maps/*/document/xml-pub")).permitAll()
2024-02-04 21:10:48 -08:00
.requestMatchers(mvc.pattern("/api/restful/users/resetPassword")).permitAll()
2024-03-21 22:22:32 -07:00
.requestMatchers(mvc.pattern("/api/restful/oauth2/googlecallback")).permitAll()
.requestMatchers(mvc.pattern("/api/restful/oauth2/confirmaccountsync")).permitAll()
2024-02-04 21:10:48 -08:00
.requestMatchers(mvc.pattern("/api/restful/admin/**")).hasAnyRole("ADMIN")
2024-01-15 16:36:29 -08:00
.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-02-10 13:53:44 -08:00
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
// Http basic is mainly used by automation tests.
if (enableHttpBasic) {
http.httpBasic(withDefaults());
}
return http.build();
2023-11-27 21:08:28 -08:00
}
}