From 5e0500933d2f805fe879ced9171c65839c72579b Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Wed, 7 Dec 2022 16:13:57 +0300
Subject: Login: use `Referer` header to redirect after the successful login
---
src/main/java/com/juick/config/SecurityConfig.java | 36 +++++++++++++++------
.../java/com/juick/www/SiteAttributesHandler.java | 37 ++++++++++++++++++++++
src/main/java/com/juick/www/VaryHandler.java | 31 ------------------
src/main/java/com/juick/www/controllers/Site.java | 15 ++++-----
.../resources/templates/views/login_success.html | 13 --------
.../templates/views/partial/navigation.html | 2 +-
.../java/com/juick/server/tests/ServerTests.java | 4 +--
7 files changed, 72 insertions(+), 66 deletions(-)
create mode 100644 src/main/java/com/juick/www/SiteAttributesHandler.java
delete mode 100644 src/main/java/com/juick/www/VaryHandler.java
delete mode 100644 src/main/resources/templates/views/login_success.html
diff --git a/src/main/java/com/juick/config/SecurityConfig.java b/src/main/java/com/juick/config/SecurityConfig.java
index f93e12a8..b16dc755 100644
--- a/src/main/java/com/juick/config/SecurityConfig.java
+++ b/src/main/java/com/juick/config/SecurityConfig.java
@@ -34,7 +34,9 @@ import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
+import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.RememberMeServices;
+import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
@@ -126,30 +128,45 @@ public class SecurityConfig {
BasicAuthenticationFilter.class)
.authorizeHttpRequests(requests -> requests
.requestMatchers(HttpMethod.OPTIONS).permitAll()
- .requestMatchers("/api/", "/api/messages", "/api/avatar", "/api/messages/discussions",
- "/api/users", "/api/thread", "/api/tags", "/api/tlgmbtwbhk", "/api/fbwbhk",
- "/api/skypebotendpoint", "/api/_fblogin", "/api/_vklogin", "/api/_tglogin",
- "/api/_google", "/api/_applelogin", "/api/signup", "/api/inbox", "/api/events",
+ .requestMatchers("/api/", "/api/messages", "/api/avatar",
+ "/api/messages/discussions",
+ "/api/users", "/api/thread", "/api/tags",
+ "/api/tlgmbtwbhk", "/api/fbwbhk",
+ "/api/skypebotendpoint", "/api/_fblogin",
+ "/api/_vklogin", "/api/_tglogin",
+ "/api/_google", "/api/_applelogin", "/api/signup",
+ "/api/inbox", "/api/events",
"/api/info/**",
"/api/nodeinfo/2.0")
.permitAll()
.anyRequest().hasRole("USER"))
.anonymous(anonymous -> anonymous.principal(JuickUser.ANONYMOUS_USER)
.authorities(JuickUser.ANONYMOUS_AUTHORITY))
- .httpBasic(httpBasic -> httpBasic.authenticationEntryPoint(juickAuthenticationEntryPoint()))
+ .httpBasic(httpBasic -> httpBasic
+ .authenticationEntryPoint(juickAuthenticationEntryPoint()))
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
- .sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
- .exceptionHandling(exceptionHandling -> exceptionHandling.authenticationEntryPoint(juickAuthenticationEntryPoint()))
+ .sessionManagement(sessionManagement -> sessionManagement
+ .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
+ .exceptionHandling(exceptionHandling -> exceptionHandling
+ .authenticationEntryPoint(juickAuthenticationEntryPoint()))
.csrf().disable()
.headers().defaultsDisabled().cacheControl();
return http.build();
}
+ @Bean
+ public AuthenticationSuccessHandler successHandler() {
+ SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
+ handler.setUseReferer(true);
+ return handler;
+ }
+
@Bean
public SecurityFilterChain wwwChain(HttpSecurity http) throws Exception {
http.addFilterBefore(wwwAuthenticationFilter(), BasicAuthenticationFilter.class)
.authorizeHttpRequests(authorize -> authorize
- .requestMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", "/post2",
+ .requestMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post",
+ "/post2",
"/comment")
.authenticated()
.requestMatchers("/actuator/**").hasRole("ADMIN")
@@ -168,10 +185,9 @@ public class SecurityConfig {
.logoutSuccessUrl("/")
.deleteCookies("hash", COOKIE_NAME))
.formLogin(form -> form.loginPage("/login")
- .defaultSuccessUrl("/")
- .loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
+ .successHandler(successHandler())
.failureUrl("/login?error=1")
.permitAll())
.csrf(csrf -> csrf.ignoringRequestMatchers("/settings/unsubscribe", "/h2-console/**"))
diff --git a/src/main/java/com/juick/www/SiteAttributesHandler.java b/src/main/java/com/juick/www/SiteAttributesHandler.java
new file mode 100644
index 00000000..e06a2070
--- /dev/null
+++ b/src/main/java/com/juick/www/SiteAttributesHandler.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2008-2020, Juick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see