首页 > 解决方案 > 登录后重定向不正确 - Spring Boot

问题描述

在我的Spring Boot应用程序中,我遇到了一个奇怪的问题。

登录后,我的 Web 应用程序重定向到 JS 页面 ( bootstrap-table.js)。该页面不是每次都出现,而是仅在第一次登录时出现,在后续登录中,我们将重定向到正确的页面“home.html”。

在我的 WebApp 中,我使用:thymeleafbootstrap4.

这是我的安全配置:

    @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
    private UserDetailsService userDetailsService;

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

        http
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/resources/**").permitAll()
            .antMatchers("/resetpwd/**").permitAll()
            .anyRequest().authenticated()
        .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .failureUrl("/?login_error")
            .defaultSuccessUrl("/home")
        .and()
        .logout()
            .logoutUrl("/logout")
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
            .permitAll()
            .logoutSuccessUrl("/")
            .invalidateHttpSession(true)
            .deleteCookies("remember-me")
        .and()
            .rememberMe()
        .and()
            .sessionManagement();

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
           .ignoring()
           .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/webjars/**");
    }

}

我已经在 中定义了cssjs文件default.html,登录表单在 header.html 中:

        <ul class="nav navbar-nav pull-right  ml-auto " sec:authorize="isAnonymous()">

             <li class="nav-item">
                <a class="nav-link dropdown-toggle " href="#" data-toggle="dropdown" th:text="#{login}"> </a>

                <ul class="dropdown-menu form-wrapper">                 
                    <li>

                  <form th:action="@{/login}"  data-parsley-validate="" method="POST" class="form login" >
                    <div class="col-sm-12 text-center" th:text="#{login}" ></div>
                    <p th:if="${loginError}" style="color: red;" th:text="#{login_unsuccessful}"></p>

                    <br />
                    <div class="form-group">
                            <div class="input-group mb-2">
                                <div class="input-group-prepend">
                                    <div class="input-group-text"><i class="fas fa-user text-info"></i></div>
                                </div>
                                <input type="text" class="form-control" id="username" name='username' placeholder="Username" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group mb-2">
                                <div class="input-group-prepend">
                                    <div class="input-group-text"><i class="fas fa-key text-info"></i></div>
                                </div>
                                <input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
                            </div>
                        </div>
                    <input type="submit" class="btn btn-primary btn-block text-center" value="Login">
                    <div class="form-footer">
                        <a data-target="#modalPwd" data-toggle="modal" href="#modalPwd" id="pwdDmntct" th:text="#{pwdDimenticata}"></a><br/><br/>
                    </div>
                </form>
             </li>
            </ul>
        </li>
        </ul>

在默认的正文中,我有 JS 的定义。

<body>

    <!-- page content -->
    <div class="container-fluid">
        <nav th:replace="~{/fragments/header :: header}"></nav>
        <div class="container-fluid">           

            <div layout:fragment="content"></div>
        </div>
    </div>          
    <!-- /page content -->

    <script th:src="@{/js/parsley/parsley.js}" type="text/javascript"></script>
    <script th:src="@{/js/parsley/it.js}" type="text/javascript"></script>
    <script th:src="@{/bootstrap-table/bootstrap-table.min.js}" type="text/javascript"></script>
    <script th:src="@{/bootstrap-table/bootstrap-table-it-IT.min.js}" type="text/javascript"></script>
    <script th:src="@{/webjars/bootstrap-datepicker/js/bootstrap-datepicker.min.js}" type="text/javascript"></script>
    <script th:src="@{/webjars/bootstrap-datepicker/locales/bootstrap-datepicker.it.min.js}" type="text/javascript"></script>
    <script th:src="@{/webjars/select2/dist/js/select2.min.js}" type="text/javascript"></script>

    <script type="text/javascript" th:inline="javascript">
        $(function () {
            if($('form').length){
                $('form').parsley().on('form:validated', function(e) {
                    if (e.validationResult) {
                        $('input[type=submit]').attr('disabled', 'disabled');
                    }
                });
            }
            $('body').on('click', '[data-toggle="modal"]', function(){
                $($(this).data("target")+' .modal-content').load($(this).attr('href'));
            });
        });
    </script>

</body>

谁能帮我理解这个问题?谢谢

标签: javascriptspring-bootspring-securitybootstrap-4

解决方案


您可能被重定向到“bootstrap-table.js”,因为您正尝试使用已注销的用户访问它。您会被重定向到登录页面,然后您会被重定向回您在登录重定向之前尝试访问的 url。

如果您想在用户每次成功登录时重定向到“/home”,无论他想在登录前访问哪个页面,您都应该使用.defaultSuccessUrl("/home", true),而不是.defaultSuccessUrl("/home")


推荐阅读