当前位置: 首页 > 知识库问答 >
问题:

Spring security重定向到登录并还原以前输入的表单数据

弘志勇
2023-03-14

概述

  1. 我用Spring Security保护了Spring Web应用程序
  2. 站点上有一个用于输入某些数据的表单,此表单是公共的,但数据将只为经过身份验证的用户处理
  3. 如果用户按下submit按钮,但还没有登录,他将被委托到登录页面。如果登录成功,用户将被重定向到数据处理结果可见的站点
    null
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties security;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("admin")
            .roles("ADMIN", "USER")
            .and()
            .withUser("user")
            .password("user")
            .roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

            http.authorizeRequests()
                .antMatchers("/", "/inputForm")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .successHandler(new SavedRequestAwareAuthenticationSuccessHandlerCustom())
                .and()
                .csrf()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/")
                .invalidateHttpSession(true)
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPointCustom("/login"));
    }
}
public class SavedRequestAwareAuthenticationSuccessHandlerCustom extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(
            HttpServletRequest request,
            HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        String text = (String) request.getSession().getAttribute("text");
        if (text != null) {
            request.getSession().removeAttribute("text");
            setDefaultTargetUrl("/user/dashboard/?text=" + text);
        }

        super.onAuthenticationSuccess(request, response, authentication);
    }
}
public class LoginUrlAuthenticationEntryPointCustom extends LoginUrlAuthenticationEntryPoint {

    public LoginUrlAuthenticationEntryPointCustom(String loginFormUrl) {
        super(loginFormUrl);
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
                    throws IOException,
                    ServletException {

        String text = request.getParameter("text");
        request.getSession().setAttribute("text", text);

        super.commence(request, response, authException);
    }
}

更新

我的配置似乎仍然有问题,因为在调试消息中可以看到,请求ist没有被“HttpSessionRequestCache”保存。如果我能做到这一点,我就不必处理自定义实现了。

O.S.S.W.Util.Matcher.andRequestMatcher:试图使用Ant[pattern='/**',GET]O.S.S.W.U.Matcher.AntPathRequestMatcher:请求'post/user/dashboard‘不匹配'GET/**O.S.S.W.Util.Matcher.andRequestMatcher:没有匹配O.S.S.W.Matcher.andRequestMatcher:没有保存为配置的RequestMatcher不匹配

共有1个答案

爱亮
2023-03-14

请确保form方法是post
,如下所示

<form th:action="@{/login}" method="post"> 
         <!-- form input -- >
</form>
 类似资料:
  • 基本上:我如何访问或确定以前请求的URL? 我真的不想求助于传递查询字符串参数,除非我真的不得不这样做。理想情况下,最好能够将< code>history集合作为< code>Router组件的一部分,类似于< code>backbone.history!

  • 下面是Spring Securityxml 当我提交表单时,即使我输入了正确的用户名和密码,它也会被重定向到url“http://localhost:8080/securitytest/login?authfailed”。

  • 我正在将DNN应用程序从07.00.02升级到07.03.04并在安装后将所有门户重定向到登录页面。所有门户都配置有一个登陆页面,该页面被配置为允许“所有用户”角色视图访问。还有人在升级后遇到过这个问题吗? 2015-02-05 05:45:01 127.0.0.1 GET/-80-127.0.0.1 Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/

  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我使用Laravel 5.2。我想问一个问题。如何更改重定向登录表单在laravel当我们已经做登录?我使用的是来自Php artisan make: auth的auth。 我已经登录并重定向到localhost:8000/admin 然后我再次打开localhost:8000/登录。它将重定向到“/”或localhost:8000/ 我的问题是如何将“/”更改为“/”管理员“?因此,即使我已经登

  • 在我的Laravel应用程序中,我有一个只能由Auth用户访问的私人页面。我也使用社交登录,所以当用户未登录就进入私人页面时,Laravel会将用户发送到登录页面,成功登录后,用户会被发送回原始目的地(私人页面)。只有当用户通过Laravel内置认证系统登录时,但当用户通过社交登录方法登录时,用户将被引导到主页。 这是我登录后的社交登录处理程序: 如果用户在会话中出现,我如何将其发送到原始目的地,