Spring автоматическое перенаправление на auth/login, но я хочу перенаправить на Success.html, как это исправитьJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Spring автоматическое перенаправление на auth/login, но я хочу перенаправить на Success.html, как это исправить

Сообщение Anonymous »

У меня есть регистрационная форма в файле Register.html, которая отправляет запрос POST в конечную точку /auth/register. После отправки формы я хочу перенаправить пользователя на Success.html вместо /auth/login. Однако вместо успешного.html он автоматически перенаправляется на /auth/login.
// SecurityConfig.java
package com.range.someting.Config;

import com.range.someting.Security.UserDetailsServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
@Configuration
public class SecurityConfig {

private final UserDetailsServiceImpl userDetailsServiceimpl;

public SecurityConfig(UserDetailsServiceImpl userDetailsServiceimpl) {
this.userDetailsServiceimpl = userDetailsServiceimpl;
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.formLogin(login -> login
.loginPage("/auth/login").permitAll())
.authorizeHttpRequests(auth ->
auth.requestMatchers(HttpMethod.GET, "/auth/register").permitAll()
.requestMatchers(HttpMethod.POST, "/auth/register").permitAll()
.requestMatchers("/auth/login").permitAll()
.anyRequest().authenticated()
)
.csrf(AbstractHttpConfigurer::disable);
return http.build();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
daoAuthenticationProvider.setUserDetailsService(userDetailsServiceimpl);
return daoAuthenticationProvider;
}
}

мой контроллер перенаправления аутентификации для html
// AuthenticationRedirectController.java
package com.range.someting.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/auth")
public class AuthenticationRedirectController {

@GetMapping("/login")
public String login() {
return "login";
}

@GetMapping("/register")
public String register() {
return "register";
}
}

вот мой пользовательский контроллер
// UserController.java
package com.range.someting.Controller;

import com.range.someting.Model.User;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/auth")
public class UserController {

@PostMapping("/register")
public String registerdata(@Valid @ModelAttribute User user, Model model) {
model.addAttribute("message", "Registration successful, please sign in.");
return "successful"; // Redirect to the "successful" view
}
}



Подробнее здесь: https://stackoverflow.com/questions/792 ... html-how-l
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»