使用Spring Security登录后无法将用户重定向到所需页面

法罕·希尔吉尔·安萨里(Farhan Shirgill Ansari)

在此Web应用程序中一切正常。浏览器被阻止缓存页面。登录和注销也可以。注销后,我能够成功地将用户重定向到登录页面。

Web应用程序控件的流程可以通过此简单流程图说明。

> loginForm> a> b> c> registerForm>配置文件

这些是应用程序中的基本6页。

loginForm.jsp:

<body onload='document.f.username.focus();'>
    <h3>Login with Username & Password</h3>
    <form name='f' method="post">
        <table>
            <tr>
                <td>User:</td>
                <td><input type="text" name="username" value='' /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <td colspan="2"><input name='submit' type="submit" value="Login" /></td>
            </tr>
             <tr>
                <td><input type="hidden" name="${_csrf.parameterName}" 
                                         value="${_csrf.token}">
            </td></tr>
        </table>
    </form>
</body>

a.jsp:

<h1>Your Profile - a</h1>
    <form method="post">
        <table>
            <tr>
                <td><input type="submit" value="Register" /></td>
            </tr>
        </table>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
    </form>

b.jsp:

<h1>Your Profile - b</h1>
    <form method="post">
        <table>
            <tr>
                <td><input type="submit" value="Register" /></td>
            </tr>
        </table>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
    </form>

c.jsp:

<h1>Your Profile - c</h1>
    <form method="post">
        <table>
            <tr>
                <td><input type="submit" value="Register" /></td>
            </tr>
        </table>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
    </form>

registerForm.jsp:

<form method="post">
        <table>
            <tr>
                <td>First Name:</td>
                <td><input type="text" name="firstName" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" name="lastName" /></td>
            </tr>
            <tr>
                <td>User Name:</td>
                <td><input type="text" name="userName" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>

                <td><input type="submit" value="Register" /></td>
            </tr>
        </table>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}">
    </form>

profile.jsp:还包含注销链接。

<h1><a href="#" onclick="javascript:logoutForm.submit();">logout</a></h1>

        <c:url var="logoutUrl" value="/logout" />
        <form action="${logoutUrl}" method="post" id="logoutForm">
            <input type="hidden" name="${_csrf.parameterName}"
                value="${_csrf.token}" />
        </form>
    <table>
        <tr>
            <td>First Name:</td>
            <td><c:out value="${spitter.firstName}" /></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><c:out value="${spitter.lastName}" /></td>
        </tr>
        <tr>
            <td>User Name:</td>
            <td><c:out value="${spitter.userName}" /></td>
        </tr>
    </table>

LoginController.java

@Controller
public class LoginController {

    @RequestMapping(value = "/loginPage", method = RequestMethod.GET)
    public String showLoginForm() {

        System.out.println("Inside GET loginPage");

        return "loginForm";
    }

    @RequestMapping(value = "/loginPage", method = RequestMethod.POST)
    public String processLoginForm() {

        System.out.println("Inside POST loginPage");
        return "redirect:/spitter/a";
    }
}

当我通过键入请求loginForm.jsp时,将调用第一个方法

http:// localhost:8080 / web / loginPage

当我单击loginForm.jsp上的Submit按钮,它应该导致调用LoginController.java中存在第二个方法但是,这永远不会发生。为什么?我有时看到将其重定向profile.jsp,或者有时将浏览器栏中的URL反映为

http:// localhost:8080 / web /

这是HTTP状态404-/ web /

其他控制器:

SpittrController.java:

@Controller
@RequestMapping(value = "/spitter")
public class SpittrController {


    @RequestMapping(value = "/a", method = RequestMethod.GET)
    public String a() {

        return "a";
    }

    @RequestMapping(value = "/a", method = RequestMethod.POST)
    public String processA() {

        return "redirect:/spitter/b";
    }

    @RequestMapping(value = "/b", method = RequestMethod.GET)
    public String b() {

        return "b";
    }

    @RequestMapping(value = "/b", method = RequestMethod.POST)
    public String processB() {

        return "redirect:/spitter/c";
    }

    @RequestMapping(value = "/c", method = RequestMethod.GET)
    public String c() {

        return "c";
    }

    @RequestMapping(value = "/c", method = RequestMethod.POST)
    public String processC() {

        return "redirect:/spitter/register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegistrationForm() {

        return "registerForm";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String processingRegistration(@Valid Spitter spitter, Errors errors) {

        if (errors.hasErrors()) {
            return "registerForm";
        }

        spittleRepository.save(spitter);
        return "redirect:/spitter/" + spitter.getUserName();

    }

    @RequestMapping(value = "/{username}", method = RequestMethod.GET)
    public String showSpitterProfile(@PathVariable("username") String username,
                                     Model model) {

        Spitter spitter = spittleRepository.findByUsername(username);
        if (spitter != null) {
            model.addAttribute(spitter);
        }

        return "profile";
    }
}

Java配置以启用Spring MVC Web安全性:

@Configuration
@EnableWebSecurity
public class SecurityConfig
    extends WebSecurityConfigurerAdapter {

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

        http.formLogin().loginPage("/loginPage").and()

            .authorizeRequests()

            .antMatchers(HttpMethod.GET, "/spitter/a").authenticated()
            .antMatchers(HttpMethod.POST, "/spitter/a").authenticated()

            .antMatchers(HttpMethod.GET, "/spitter/b").authenticated()
            .antMatchers(HttpMethod.POST, "/spitter/b").authenticated()

            .antMatchers(HttpMethod.GET, "/spitter/c").authenticated()
            .antMatchers(HttpMethod.POST, "/spitter/c").authenticated()

            .antMatchers(HttpMethod.GET, "/spitter/register").authenticated()
            .antMatchers(HttpMethod.POST, "/spitter/register").authenticated()

            .antMatchers(HttpMethod.GET, "/spitter/**").authenticated()



            .and().logout().logoutSuccessUrl("/loginPage");

    }

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

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

}

应用程序的Eclipse结构:

在此处输入图片说明

刷新

您应该添加http.formLogin().loginPage("/loginPage").defaultSuccessUrl("/spitter/a").and()‌​.Web安全配置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Security不断将我重定向到登录页面

在Spring应用程序中使用onAuthenticationSuccess将用户重定向到登录页面后的原始URL

成功登录后,Spring Security有时会重定向到默认页面而不是登录前页面

如何在登录首页后重定向用户,并使用Spring Security抛出200而不是302?

Spring Security:登录后如何重定向到REST URL

使用Azure AD时将用户重定向到自定义登录页面

Spring Security:如果是401,则重定向到登录页面

使用Okta登录ASP.NET Core后,将用户重定向到默认页面

使用this.props.history.push('/')的用户注销重定向不会重定向到所需页面

使用vue.js按下提交后将用户重定向到其他页面

Spring Security-能够在初始重定向到主页后返回登录页面

使用devise在Rails 3中发送密码重置指令后,如何将用户重定向到登录页面

使用IdentityServices注销后如何重定向到“登录”页面

使用重定向:uri登录后将Spring Security重定向到页面

Spring Security和Angular javascript重定向到登录页面

在登录身份验证和使用PHP PDO的if语句后,将用户重定向到另一个页面

无法使用超级链接将用户重定向到另一个页面

Spring Security:登录后重定向到登录页面

用户使用Ion Auth CodeIgniter登录后如何将用户重定向到另一个页面

如果用户未经授权,Spring Security如何重定向到“登录”页面

使用Spring Security Core插件在Grails中登录后如何重定向到页面

Spring Security在登录后始终重定向到“ /”

登录后,Spring Security再次重定向到登录页面

使用spring登录后重定向到请求的页面

成功登录后,我尝试将用户重定向到另一个页面(homeDemo.php),但它似乎没有重定向。我正在使用php

Spring Security-用户身份验证有时会失败,并且用户将被重定向到登录页面

使用 PassportJS 在 Facebook 登录后将用户重定向到原始 URL

使用他们的用户角色从同一登录页面将用户重定向到仪表板

当他尝试进入登录页面时,使用 java spring-security 重定向已经登录的用户