У меня есть регистрационная форма в файле 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
Spring автоматическое перенаправление на auth/login, но я хочу перенаправить на Success.html, как это исправить ⇐ JAVA
Программисты JAVA общаются здесь
1733147170
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
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79244322/spring-auto-redirect-to-auth-login-but-l-want-redirect-to-successful-html-how-l[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия