我第一次使用 Spring Boot 為 REACT 網路應用程式設定用戶登錄系統。截至目前,我正在嘗試在成功驗證后將用戶重定向到我的 REACT 應用程式中的另一個頁面。我正在使用自定義 AuthenticationSuccessHandler 來處理實際的重定向。
@Override
protected void configure(HttpSecurity http) throws Exception {
CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter(authenticationManagerBean());
customAuthenticationFilter.setFilterProcessesUrl("/api/v*/login/**");
http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().antMatchers("/api/v*/login/**").permitAll();
http.authorizeRequests().antMatchers(HttpMethod.GET, "api/v*/users/**").hasAnyAuthority("PRODUCER");
http.authorizeRequests().antMatchers("/api/v*/registration/**").permitAll();
http.authorizeRequests().anyRequest().authenticated();
http.formLogin().loginPage("http://localhost:3000/login").successHandler(myAuthenticationSuccessHandler());
@Bean
public AuthenticationSuccessHandler myAuthenticationSuccessHandler(){
return new RedirectLoginSuccessHandler();
}
public class RedirectLoginSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication) throws IOException {
httpServletResponse.sendRedirect("http://localhost:3000");
}
}
API 在埠 :8080 上運行,react 登錄表單在 :3000/login 上運行,通過向 localhost:8080/api/v1/login 發送 POST 請求(獲得 JWT 訪問令牌作為回報)來完成身份驗證,然后試圖我希望用戶被重定向到 localhost:3000 的主頁,但重定向被忽略,除非我使用您從 Spring 獲得的登錄表單和 loginForm() 方法。
我還嘗試將重定向與 JWT 令牌一起發送作為對 api/v1/login 的 POST 請求的回應,但是在 CORS 這樣做時遇到了問題。我在這里做錯了什么?我在成功身份驗證后看到的重定向教程參考了要重定向的 html 檔案,例如 home.html,我假設他們能夠通過將 REACT 和 Spring boot 專案捆綁到一個單一存盤庫中來做到這一點。
Cors 問題
uj5u.com熱心網友回復:
也許嘗試這樣的事情......
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
// ... endpoints
.formLogin()
... config
.defaultSuccessUrl("http://localhost:3000l", true)
// ... other configuration
}
}
uj5u.com熱心網友回復:
使用formLogin,Spring Security 基本上為您提供了這些步驟(簡化):
- 當您呼叫 API 需要對用戶進行身份驗證時,如果發生 AccessDeniedException,a
AuthenticationEntryPoint會將您重定向到formLogin().loginPage("http://localhost:3000/login"). - 用戶填寫
username和 后password,您需要將它們發布到formLogin().loginProcessingUrl("/login123")(如果您不指定,則默認值應為“/login”)。 - 在
UsernamePasswordAuthenticationFilter得知后,對匹配器基地"/login123"將攔截您的文章,并驗證您的用戶username和password。 - 如果它們被成功驗證,您
.successHandler(myAuthenticationSuccessHandler())將被要求完成其作業。
...
在這里,因為
身份驗證是通過向 localhost:8080/api/v1/login 發送 POST 請求來完成的(獲得 JWT 訪問令牌作為回報)
你沒有按照步驟2的流量(你沒有張貼的用戶名/密碼“/ login123”),你不能得到Spring Security中支持的功能之后(this.rememberMeServices.loginSuccess,this.successHandler.onAuthenticationSuccess,...為例)。
對于您的情況,從 獲得 JWT 后a POST-request to localhost:8080/api/v1/login,您可以自己重定向,您不需要依賴.successHandler(myAuthenticationSuccessHandler()).
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/349030.html
