简体   繁体   English

使用Spring Security更改网址

[英]Change the Url With Spring Security

I'm trying to do something very simple. 我正在尝试做一些非常简单的事情。 If the user fails the login the url should change to /login?error so I can display a message. 如果用户登录失败,则网址应更改为/login?error这样我可以显示一条消息。

But my attempt doesn't work and it stays in /login even when the user fails the login. 但是我的尝试不起作用,即使用户失败登录,它也仍然停留在/ login中。 What am I missing?? 我想念什么?

The error shows well if I put the url manually. 如果我手动输入网址,则错误显示得很好。

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/").hasAnyRole("Administrator")
            .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/dashboard")
                .failureUrl("/login?error")
            .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login");
}

}

Login Page: 登录页面:

<form action="login" method="post">
 <input type="text" name="username" id="username" placeholder="Username" required="required" />
<input type="password" name="password" id="password" placeholder="Password"  required="required" />
<button type="submit" id="btnLogin" class="btn btn-atp btn-block btn-large">Sign in</button>
<div class="errorMessage">
  <c:if test="${param.error != null}">
    <p>Invalid username and password.</p>
  </c:if>
  <c:if test="${param.logout != null}">
    <p>You have been logged out.</p>
  </c:if>
</div>

I think that you forgot add "permitAll" after your "formLogin" 我认为您忘记了在“ formLogin”之后添加“ permitAll”

https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#jc-form https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#jc-form

.formLogin()
 .loginPage("/login")
 .permitAll();  

permitAll - We must grant all users (ie unauthenticated users) access to our log in page. permitAll-我们必须授予所有用户(即未经身份验证的用户)访问我们登录页面的权限。 The formLogin().permitAll() method allows granting access to all users for all URLs associated with form based log in. formLogin()。permitAll()方法允许向所有用户授予与基于表单的登录相关联的所有URL的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM