gpt4 book ai didi

java - 如何在自定义登录页面获取注销消息

转载 作者:行者123 更新时间:2023-12-02 03:18:19 24 4
gpt4 key购买 nike

我必须使用 Spring Security 为我的项目创建一个登录模块,其中我重写了 WebSecurityConfigurerAdapterconfigure(HttpSecurity http) 方法并创建了一个自定义登录页面。

customLogin.jsp:

<body>

<h3>Login Here</h3>
<font color="red">
${SPRING_SECURITY_LAST_EXCEPTION.message}
</font>

<form action="<%=request.getContextPath()%>/appLogin" method="POST">
Enter UserName: <input type="text" name="app_username"/><br/><br/>
Enter Password: <input type="password" name="app_password"/> <br/><br/>
<input type="submit" value="Login"/>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>

<body>

这是我的配置方法:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().
antMatchers("/secure/**").access("hasRole('ROLE_ADMIN')").
and().formLogin(). // login configuration
loginPage("/customLogin.jsp").
loginProcessingUrl("/appLogin").
usernameParameter("app_username").
passwordParameter("app_password").
defaultSuccessUrl("/secure/home").
and().logout(). // logout configuration
logoutUrl("/appLogout").
logoutSuccessUrl("/customLogin.jsp");
}

在有注销按钮的地方,我添加了以下代码(因为 Spring Security 自动启用 CSRF,这会自动禁用 GET 注销):

 <form action="<%=request.getContextPath()%>/appLogout" method="POST">
<input type="submit" value="Logout"/>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>

当我单击注销按钮时,它会将我重定向到自定义登录页面,但不显示默认注销消息。我怎样才能在这里收到该消息?

最佳答案

参见Spring Security Reference :

An example log in page implemented with JSPs for our current configuration can be seen below:

[..]

<c:url value="/login" var="loginUrl"/>
<form action="${loginUrl}" method="post"> 1
<c:if test="${param.error != null}"> 2
<p>
Invalid username and password.
</p>
</c:if>
<c:if test="${param.logout != null}"> 3
<p>
You have been logged out.
</p>
</c:if>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/> 4
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/> 5
</p>
<input type="hidden" 6
name="${_csrf.parameterName}"
value="${_csrf.token}"/>
<button type="submit" class="btn">Log in</button>
</form>

1 A POST to the /login URL will attempt to authenticate the user
2 If the query parameter error exists, authentication was attempted and failed
3 If the query parameter logout exists, the user was successfully logged out

此外,您还必须向 logoutSuccessUrl 添加查询参数 logout

您修改后的 Java 配置是:

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().
antMatchers("/secure/**").access("hasRole('ROLE_ADMIN')").
and().formLogin(). // login configuration
loginPage("/customLogin.jsp").
loginProcessingUrl("/appLogin").
usernameParameter("app_username").
passwordParameter("app_password").
defaultSuccessUrl("/secure/home").
and().logout(). // logout configuration
logoutUrl("/appLogout").
logoutSuccessUrl("/customLogin.jsp?logout");
}

关于java - 如何在自定义登录页面获取注销消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39992760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com