@@ -70,22 +70,22 @@ public class AppConfig implements WebMvcConfigurer {
7070
7171 @ Value ("${app.security.corsAllowedOrigins:}" )
7272 private String corsAllowedOrigins ;
73-
73+
7474 @ Value ("${app.site.ui-base-url:}" )
7575 private String uiBaseUrl ;
7676
7777 @ Autowired
7878 private JwtAuthenticationFilter jwtAuthenticationFilter ;
79-
79+
8080 @ Autowired (required = false )
8181 private OAuth2AuthenticationSuccessHandler oauth2AuthenticationSuccessHandler ;
82-
82+
8383 @ Autowired
8484 private AuthenticationProvider dbAuthenticationProvider ;
8585
8686 @ Autowired (required = false )
8787 private ClientRegistrationRepository clientRegistrationRepository ;
88-
88+
8989 @ Bean
9090 public com .wisemapping .security .ChatGptOAuth2AuthorizationRequestResolver chatGptOAuth2AuthorizationRequestResolver () {
9191 if (clientRegistrationRepository != null ) {
@@ -95,15 +95,17 @@ public com.wisemapping.security.ChatGptOAuth2AuthorizationRequestResolver chatGp
9595 }
9696
9797 @ Bean
98- SecurityFilterChain apiSecurityFilterChain (@ NotNull final HttpSecurity http ) throws Exception {
98+ @ org .springframework .core .annotation .Order (1 )
99+ SecurityFilterChain restApiSecurityFilterChain (@ NotNull final HttpSecurity http ) throws Exception {
99100 http
100- .cors (Customizer .withDefaults ()) // enables WebMvcConfigurer CORS
101- .securityMatcher ("/**" )
101+ .securityMatcher ("/api/**" )
102+ .cors (Customizer .withDefaults ())
103+ .csrf (AbstractHttpConfigurer ::disable )
104+ .sessionManagement (session -> session .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
102105 .authenticationProvider (dbAuthenticationProvider )
103106 .addFilterAfter (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class )
104107 .authorizeHttpRequests (auth -> auth
105108 .requestMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
106- .requestMatchers ("/error" ).permitAll ()
107109 .requestMatchers ("/api/restful/authenticate" ).permitAll ()
108110 .requestMatchers ("/api/restful/users/" ).permitAll ()
109111 .requestMatchers ("/api/restful/app/config" ).permitAll ()
@@ -112,52 +114,50 @@ SecurityFilterChain apiSecurityFilterChain(@NotNull final HttpSecurity http) thr
112114 .requestMatchers ("/api/restful/maps/*/document/xml-pub" ).permitAll ()
113115 .requestMatchers ("/api/restful/users/resetPassword" ).permitAll ()
114116 .requestMatchers ("/api/restful/users/activation" ).permitAll ()
115- .requestMatchers ("/login/oauth2/**" ).permitAll ()
116- .requestMatchers ("/oauth2/**" ).permitAll ()
117117 .requestMatchers ("/api/restful/admin/**" ).hasAnyRole ("ADMIN" )
118- .requestMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
119118 .requestMatchers ("/**" ).hasAnyRole ("USER" , "ADMIN" )
120119 .anyRequest ().authenticated ())
121120 .exceptionHandling (exception -> exception
122121 .authenticationEntryPoint ((request , response , authException ) -> {
123- // For API endpoints, return 401 instead of redirecting to OAuth login
124- if (request .getRequestURI ().startsWith ("/api/" )) {
125- response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
126- response .setContentType ("application/json" );
127- response .getWriter ().write ("{\" msg\" :\" Unauthorized\" }" );
128- } else {
129- // For non-API endpoints, let OAuth2 handle it
130- response .sendError (HttpServletResponse .SC_UNAUTHORIZED );
131- }
132- }));
133-
134- // Only configure OAuth2 login if at least one registration is defined
135- boolean isOAuth2Enabled = (oauth2AuthenticationSuccessHandler != null ) &&
136- (clientRegistrationRepository != null );
137- if (isOAuth2Enabled ) {
138- var chatGptResolver = chatGptOAuth2AuthorizationRequestResolver ();
139- http .oauth2Login (oauth2 -> oauth2
140- .authorizationEndpoint (authorization -> {
141- if (chatGptResolver != null ) {
142- authorization .authorizationRequestResolver (chatGptResolver );
143- }
144- })
145- .successHandler (oauth2AuthenticationSuccessHandler )
146- .failureHandler ((request , response , exception ) -> {
147- // For API endpoints, return 401 instead of redirecting
148- if (request .getRequestURI ().startsWith ("/api/" )) {
149122 response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
150123 response .setContentType ("application/json" );
151- response .getWriter ().write ("{\" msg\" :\" OAuth authentication failed\" }" );
152- } else {
153- // Redirect to frontend with error
154- response .sendRedirect (uiBaseUrl + "/c/login?error=oauth_failed" );
155- }
156- })
157- );
124+ response .getWriter ().write ("{\" msg\" :\" Unauthorized\" }" );
125+ }));
126+
127+ // Http basic is mainly used by automation tests.
128+ if (enableHttpBasic ) {
129+ http .httpBasic (withDefaults ());
158130 }
159-
160- http .logout (logout -> logout .permitAll ()
131+
132+ return http .build ();
133+ }
134+
135+ @ Bean
136+ @ org .springframework .core .annotation .Order (2 )
137+ SecurityFilterChain webSecurityFilterChain (@ NotNull final HttpSecurity http ) throws Exception {
138+ http
139+ .cors (Customizer .withDefaults ())
140+ .securityMatcher ("/**" )
141+ .authenticationProvider (dbAuthenticationProvider )
142+ .addFilterAfter (jwtAuthenticationFilter , UsernamePasswordAuthenticationFilter .class )
143+ .authorizeHttpRequests (auth -> auth
144+ .requestMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
145+ .requestMatchers ("/error" ).permitAll ()
146+ .requestMatchers ("/login/oauth2/**" ).permitAll ()
147+ .requestMatchers ("/oauth2/**" ).permitAll ()
148+ .requestMatchers ("/**" ).hasAnyRole ("USER" , "ADMIN" )
149+ .anyRequest ().authenticated ())
150+ .exceptionHandling (exception -> exception
151+ .authenticationEntryPoint ((request , response , authException ) -> {
152+ // For non-API endpoints, let OAuth2 handle it or default behavior
153+ // We don't need the API specific 401 JSON response here usually,
154+ // but we can keep the default which might redirect to login page if we had one,
155+ // or 401 if not.
156+ // Given the previous config had a custom entry point that handled "else",
157+ // we can revert to default or simple error.
158+ response .sendError (HttpServletResponse .SC_UNAUTHORIZED );
159+ }))
160+ .logout (logout -> logout .permitAll ()
161161 .logoutSuccessHandler ((request , response , authentication ) -> {
162162 response .setStatus (HttpServletResponse .SC_OK );
163163 }))
@@ -181,9 +181,22 @@ SecurityFilterChain apiSecurityFilterChain(@NotNull final HttpSecurity http) thr
181181 // Custom headers
182182 .addHeaderWriter ((request , response ) -> response .setHeader ("Server" , "WiseMapping" )));
183183
184- // Http basic is mainly used by automation tests.
185- if (enableHttpBasic ) {
186- http .httpBasic (withDefaults ());
184+ // Only configure OAuth2 login if at least one registration is defined
185+ boolean isOAuth2Enabled = (oauth2AuthenticationSuccessHandler != null ) &&
186+ (clientRegistrationRepository != null );
187+ if (isOAuth2Enabled ) {
188+ var chatGptResolver = chatGptOAuth2AuthorizationRequestResolver ();
189+ http .oauth2Login (oauth2 -> oauth2
190+ .authorizationEndpoint (authorization -> {
191+ if (chatGptResolver != null ) {
192+ authorization .authorizationRequestResolver (chatGptResolver );
193+ }
194+ })
195+ .successHandler (oauth2AuthenticationSuccessHandler )
196+ .failureHandler ((request , response , exception ) -> {
197+ // Redirect to frontend with error
198+ response .sendRedirect (uiBaseUrl + "/c/login?error=oauth_failed" );
199+ }));
187200 }
188201
189202 return http .build ();
@@ -211,7 +224,8 @@ public void addCorsMappings(CorsRegistry registry) {
211224 @ Primary
212225 public TaskExecutor taskExecutor () {
213226 ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
214- // Limit to single thread to prevent parallel execution and reduce memory pressure
227+ // Limit to single thread to prevent parallel execution and reduce memory
228+ // pressure
215229 executor .setCorePoolSize (1 );
216230 executor .setMaxPoolSize (1 );
217231 executor .setQueueCapacity (100 );
0 commit comments