Spring security 成功登录后如何设置重定向?
我使用的是带有SpringBootStarter安全依赖项的SpringBoot 我有一个应用程序,如果提供正确的凭据,它将成功登录。然而,每当我登录时,我不会被重定向到任何地方。我如何配置它 表格如下:Spring security 成功登录后如何设置重定向?,spring-security,spring-boot,Spring Security,Spring Boot,我使用的是带有SpringBootStarter安全依赖项的SpringBoot 我有一个应用程序,如果提供正确的凭据,它将成功登录。然而,每当我登录时,我不会被重定向到任何地方。我如何配置它 表格如下: <form th:action="@{/login}" method="post"> <div><label> User Name : <input type="text" name="username"/> </labe
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
成功登录后定义重定向需要应用于Spring Security,而不是Spring MVC
th:action
定义了将处理身份验证请求的Spring安全端点。它没有定义重定向URL。开箱即用的Spring Boot Security将为您提供/login
端点。默认情况下,Spring Security将在登录到您尝试访问的安全资源后重定向。如果希望始终重定向到特定URL,可以通过HttpSecurity配置对象强制重定向
假设您使用的是最新版本的Spring Boot,那么您应该能够使用JavaConfig
下面是一个简单的例子:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
// the boolean flags force the redirection even though
// the user requested a specific secured resource.
http.formLogin().defaultSuccessUrl("/success.html", true);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService);
}
}
请注意,您需要定义一个proprer端点来为/success.html
URL提供内容。默认情况下,src/main/resources/public/
中的一个静态资源可以用于测试目的。我个人更愿意定义一个由SpringMVC控制器提供的安全URL,该控制器使用Thymeleaf提供内容。您不希望任何匿名用户能够访问成功页面。Thymeleaf作为一些有用的特性,在呈现HTML内容时与Spring安全性交互
问候,,
Daniel您还可以动态定义登录后重新定向。结果证明这是疯狂的简单 假设您有一个控制器,它具有复杂的条件,您需要确保用户正确登录 通过将“请求”缓存中的值设置为当前请求/响应,然后执行重新定向,Spring security将在登录成功后转发到缓存的请求
RequestCache requestCache = new HttpSessionRequestCache();
requestCache.saveRequest(request,response);
return "redirect:/login";
不,这似乎在任何地方都没有记录。我找到的唯一参考资料如下:
SavedRequests和RequestCache接口
ExceptionTranslationFilter职责的另一个职责是在调用AuthenticationEntryPoint之前保存当前请求。这允许在用户进行身份验证后恢复请求(请参阅前面的web身份验证概述)。一个典型的例子是,用户使用表单登录,然后通过默认的SavedRequestStataWareAuthenticationSuccessHandler(见下文)重定向到原始URL
RequestCache封装了存储和检索HttpServletRequest实例所需的功能。默认情况下,使用HttpSessionRequestCache,它将请求存储在HttpSession中。RequestCacheFilter的任务是,当用户被重定向到原始URL时,从缓存中实际还原保存的请求
它对我有用。登录成功后,Spring security将重定向到“/”,然后检查用户是否经过身份验证,在本例中,将其重定向到我的仪表板页面
@RequestMapping("/")
public String index(Model model) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken))
return "dashboard";
// if it is not authenticated, then go to the index...
// other things ...
return "index";
}
@RequestMapping("/")
public String index(Model model) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken))
return "dashboard";
// if it is not authenticated, then go to the index...
// other things ...
return "index";
}