成功登录后Spring Boot Security重定向-未定义
我遵循了spring boot安全教程,但最终结果出现了一个问题,即 成功登录后,浏览器将重定向到/undefined
。
我什至克隆了本教程中引用的代码,以为我键入了错误的内容,或者忘记添加组件或其他内容。不,存在相同的问题。
在Stackoverflow上搜索时,我发现您需要configure
使用WebSecurityConfigurerAdapter
类似方法定义默认成功URL
:
.defaultSuccessUrl("/")
但仍然没有去。访问受保护的资源将导致登录页面,成功登录后,我不会重定向到受保护的资源。我进入“ / undefined”页面。但是,强制成功是可行的:
.defaultSuccessUrl("/", true)
…但这不是我所需要的,因为成功登录后,应该将用户重定向到(最初)请求的安全资源。
这是项目的相关部分:
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
,login
以及salut
(如果需要的话,我会添加自己的内容)
和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, "/");
}
})
以上是 成功登录后Spring Boot Security重定向-未定义 的全部内容, 来源链接: utcz.com/qa/426209.html