我必须使用Security为我的项目创建一个登录模块,在这里我重写了WebSecurityConfigurerAdapter的WebSecurityConfigurerAdapter方法并创建了一个自定义登录页面。
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");
}在有注销按钮的地方,我包含了以下代码(因为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>当我单击“注销”按钮时,它会将我重定向到自定义登录页,但不会显示默认的注销消息。我怎么才能在这里得到那条信息?
发布于 2016-10-12 11:42:55
请参阅弹簧安全参考
下面可以看到用JSP为当前配置实现的登录页面示例: 。。 1 2无效用户名和密码。 3您已经注销。用户名4密码5<输入type=“隐藏的”6 name="${_csrf.parameterName}“value="${_csrf.token}"/>日志 1到
/login网址的帖子将试图对用户进行身份验证 2如果存在查询参数error,则尝试身份验证失败。 3如果查询参数logout存在,则用户已成功注销。
此外,还必须将查询参数logout添加到logoutSuccessUrl中。
修改后的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");
}发布于 2016-10-12 07:43:47
添加到您的customLogin.jsp页面:
<div th:if="${param.logout}" class="alert alert-success">Logged out Sucessfully!</div>发布于 2021-04-29 07:17:27
<!-- logout message --> <div th:if="${param.logout}"> <div class="alert alert-info">You have been logged out.</div> </div>
你可以使用它。
https://stackoverflow.com/questions/39992760
复制相似问题