当前位置: 首页 > 知识库问答 >
问题:

成功登录后的 Spring 引导安全性重定向 - 未定义

卜和悌
2023-03-14

我遵循了spring boot安全教程,但最终结果有一个问题,即在成功登录后,浏览器重定向到/undefined

我什至克隆了教程中引用的代码,认为我输入错误的内容,或者忘记添加组件或其他内容。不,同样的问题也存在。

在Stackoverflow上搜索时,我发现您需要在< code > WebSecurityConfigurerAdapter 的< code>configure方法中定义默认的成功URL,如下所示:

.defaultSuccessUrl("/")

但仍然不去。访问受保护的资源会导致登录页面,成功登录后,我不会被重定向到受保护的资源。我进入“/未定义”页面。但是,强制成功是有效的:

.defaultSuccessUrl("/", true)

…但这不是我需要的,因为成功登录后,用户应该被重定向到(最初)请求的安全资源。

以下是该项目的相关部分:

Web安全配置:

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";
    }
}

有为index登录 定义的视图(如果需要,我将添加它们的内容)

以及构建。放坡文件:

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')
}

共有2个答案

戴鸿羲
2023-03-14

我也有同样的问题,这是我使用的一个解决方法。首先有一个未受保护的根“/”的映射

@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;
}

确保根url在您的安全配置中是打开的,如...

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

现在它将点击根 /,并重定向到受限制的 home 并将它们发送到带有 home 返回 url 的登录页面。然后,当他们第一次登录时,它会正确写成 /home。

由于某种原因,Spring 安全性不尊重默认的成功 url,这可能是您的 Web 服务器配置问题导致它。在我的本地机器上我没有这个问题,但在其他一些机器上我有。解决方法在这两个地方都有效,因为您最终总是得到一个 returnUrl。

姬心思
2023-03-14

您可以添加一个成功处理程序来重定向,如下所示:

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, "/");
    }
})
 类似资料:
  • 我正在使用带有java配置和两个HttpSecurity配置的spring-security 3.2.0.rc2。一个用于REST API,一个用于UI。当我发布到/logout时,它重定向到/login?logout,但随后(错误地)重定向到/login。当我成功地输入用户名和密码时,我会被重定向到登录-注销,并且必须再次输入凭据才能进入主页面。因此,似乎login的permitAll不被用于l

  • 问题内容: 我知道之前曾有人问过这个问题,但是我在这里面临一个特殊的问题。 我使用Spring Security 3.1.3。 我的Web应用程序中有3种可能的登录案例: 通过登录页面登录:确定。 通过受限页面登录:也可以。 通过非受限页面登录:不好,…每个人都可以访问“产品”页面,并且用户可以在登录后发表评论。因此,同一页面中包含一个登录表单,以允许用户进行连接。 情况3)的问题是我无法设法将用

  • 我知道这个问题以前有人问过,但我现在面临一个特殊的问题。 我使用spring security 3.1.3。 我的web应用程序中有3种可能的登录情况: 通过登录页登录:确定。 通过受限页面登录:也可以。 通过非受限页面登录:不确定...每个人都可以访问“产品”页面,如果用户已经登录,他可以发表评论。因此登录表单包含在同一页面中,以便允许用户进行连接。 案例3)的问题是,我无法将用户重定向到“产品

  • 我正在尝试使用MVC客户端设置IdentityServer4。 一切正常,直到我想添加ASP身份。当我添加代码以使用SQL server和Identity时,成功登录后,Identity server不会将我重定向回客户端,但它只是“刷新”页面。 IdentityServer应用程序启动: 在IdentityServer中配置 在MVC客户端中启动: 来自IdentityServer的日志: 我只

  • MvcConfig方法如下所示:

  • 我在模态窗口中有登录表单。成功登录后,用户被重定向到< code>/页面。我正试图找到一种方法,在登录后留在联系页面或另一个页面。如何做到这一点?我的代码是: