相关疑难解决方法(0)

成功登录后的Spring Boot安全重定向 - 未定义

我已经按照春季启动安全教程,但最终结果有一个问题,即成功登录后浏览器重定向到/undefined.

我甚至克隆了教程中引用的代码,认为我键入了错误,或忘记添加组件或其他东西.不,存在同样的问题.

在Stackoverflow上搜索我发现你需要configureWebSecurityConfigurerAdapter类似的方法中定义默认的成功URL,这样:

.defaultSuccessUrl("/")
Run Code Online (Sandbox Code Playgroud)

但仍然没有去.访问受保护资源会导致登录页面,并且在成功登录后,我不会被重定向到受保护资源.我进入'/ undefined'页面.然而,迫使成功起作用:

.defaultSuccessUrl("/", true)
Run Code Online (Sandbox Code Playgroud)

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


这是项目的相关部分:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

package ro.rinea.andrei.Controllers;

import org.springframework.stereotype.Controller;
import …
Run Code Online (Sandbox Code Playgroud)

java security spring spring-security spring-boot

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×1

security ×1

spring ×1

spring-boot ×1

spring-security ×1