簡體   English   中英

成功登錄后的Spring Boot安全重定向 - 未定義

[英]Spring Boot Security redirect after successful login - undefined

我已經按照Spring boot安全教程進行了操作,但最終結果有一個問題,即成功登錄后瀏覽器會重定向到/undefined

我甚至克隆了教程中引用的代碼,認為我鍵入了錯誤,或忘記添加組件或其他東西。 不,存在同樣的問題。

在Stackoverflow上搜索我發現你需要在WebSecurityConfigurerAdapterconfigure方法中定義默認的成功URL, WebSecurityConfigurerAdapter所示:

.defaultSuccessUrl("/")

但仍然沒有去。 訪問受保護資源會導致登錄頁面,並且在成功登錄后,我不會被重定向到受保護資源。 我進入'/ undefined'頁面。 然而,迫使成功起作用:

.defaultSuccessUrl("/", true)

...但這不是我需要的,因為在成功登錄后,用戶應該被重定向到最初請求的安全資源。


這是項目的相關部分:

WebSecurityConfig:

package ro.rinea.andrei.Security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }
}

控制器:

package ro.rinea.andrei.Controllers;

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

@Controller
public class WebController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/salut")
    public String salut() {
        return "salut";
    }

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

有為indexloginsalut定義的視圖(如果需要,我將添加其內容)

和build.gradle文件:

buildscript {
    ext {
        springBootVersion = '1.4.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'tstBut'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-mobile')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-web-services')
    compile('org.springframework.boot:spring-boot-starter-security')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

您可以添加successHandler來重定向,如下所示:

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
   ...
   .formLogin()
   .loginPage("/login")
   .successHandler(new AuthenticationSuccessHandler() {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {
        redirectStrategy.sendRedirect(request, response, "/");
    }
})

我有同樣的問題,這是我使用的解決方法。 首先有一個未受保護的根“/”的映射

@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public ModelAndView projectBase() {
    return new ModelAndView("redirect:/home");
}

讓它重定向到您希望用戶最初像家一樣去的地方

@RequestMapping(value = { "/home" }, method = RequestMethod.GET)
public ModelAndView getHome() {
    ModelAndView model = new ModelAndView("account/home");
    model.addObject("user", userFacade.getJsonForUser(userFacade.getUserForClient()));
    return model;
}

確保您的安全配置中的根網址已打開,例如...

 http.
    authorizeRequests()
    .antMatchers("/").permitAll()

現在會發生什么事情,它將觸及root /,並重定向到受限制的主頁,並將其發送到登錄頁面,並返回主頁的返回URL。 然后它會在首次登錄時正確地寫為/ home

出於某種原因,Spring安全性不尊重默認的成功URL,並且它可能是導致它的Web服務器的配置問題。 在我的本地機器上我沒有這個問題,但在我做的其他機器上。 解決方法適用於這兩個地方,因為您總是以returnUrl結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM