我已关注spring boot security tutorial,但最终结果是成功登录后中的问题,浏览器会重定向到/undefined
。
我甚至克隆了教程中引用的代码,认为我键入了错误,或忘记添加组件或其他内容。不,存在同样的问题。
在Stackoverflow上搜索我发现您需要在configure
的{{1}}方法中定义默认成功网址,如下所示:
WebSecurityConfigurerAdapter
但仍然没有去。访问受保护资源会导致登录页面,并且在成功登录后,我不会被重定向到受保护资源。我进入'/ undefined'页面。然而,迫使成功起作用:
.defaultSuccessUrl("/")
...但这不是我需要的,因为在成功登录后,用户应该被重定向到最初请求的安全资源。
以下是该项目的相关部分:
WebSecurityConfig:
.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
定义了视图(如果需要,我会添加其内容)
和build.gradle文件:
salut
答案 0 :(得分:10)
您可以添加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, "/");
}
})
答案 1 :(得分:1)
我遇到了同样的问题,这是我使用过的解决方法。 首先有一个不受保护的根“/”的映射
@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 /,并重定向到受限制的主页,并将其发送到返回网址的登录页面。然后它会在首次登录时正确地写为/ home
出于某种原因,Spring安全性不尊重默认的成功URL,并且它可能是导致它的Web服务器的配置问题。在我的本地机器上我没有这个问题,但在我做的其他机器上。解决方法适用于这两个地方,因为您总是以returnUrl结束。