重定向到上一个页面 Spring Security

huangapple go评论80阅读模式
英文:

Redirect to previous page Spring Security

问题

我有一个网络应用程序,在单独的页面上有一个登录选项。然而,当我登录时,我被送回主页,而不是回到我之前所在的页面。我正在使用Spring Security提供的默认登录/注销页面。

这是我的HttpSecurity配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
	http.authorizeRequests()
	.antMatchers("/api/*").hasRole("Admin")
	.antMatchers("/manager").hasAnyRole("Admin", "Manager")
	.antMatchers("/sales").hasAnyRole("Admin", "Manager", "Sales")
	.antMatchers("/checkout").authenticated()
	.and().formLogin().permitAll()
	.and().logout().permitAll()
	.logoutSuccessUrl("/");
}
英文:

I have a web app which has a log-in option on separate pages. However when I log in, I get sent back to the home page instead of going to back to the page I was on. I am using the default login/log-out pages provided by Spring Security.

Here's my HttpSecurity configuration:

@Override
protected void configure(HttpSecurity http) throws Exception {
	http.authorizeRequests()
	.antMatchers("/api/*").hasRole("Admin")
	.antMatchers("/manager").hasAnyRole("Admin", "Manager")
	.antMatchers("/sales").hasAnyRole("Admin", "Manager", "Sales")
	.antMatchers("/checkout").authenticated()
	.and().formLogin().permitAll()
	.and().logout().permitAll()
	.logoutSuccessUrl("/");
}

答案1

得分: 1

主要思路如何实现要求在这篇文章中解释,其中包括了代码示例的源代码。您只需要修改这个方法:

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException {
    if (response.isCommitted()) {
        logger.debug("Response has already been committed. Unable to redirect to " + request.getRequestURI());
        return;
    }
    redirectStrategy.sendRedirect(request, response, request.getRequestURI());
}

MySimpleUrlAuthenticationSuccessHandler 类中。

英文:

Main idea how to implement requirements explain in this article, which include examples source of code. You need only change this method

protected void handle(final HttpServletRequest request, final HttpServletResponse response, final 

Authentication authentication) throws IOException {

    if (response.isCommitted()) {
        logger.debug("Response has already been committed. Unable to redirect to " + request.getRequestURI());
        return;
    }
    redirectStrategy.sendRedirect(request, response, request.getRequestURI());
}`

in MySimpleUrlAuthenticationSuccessHandler class

huangapple
  • 本文由 发表于 2020年8月29日 22:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63648241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定