gpt4 book ai didi

java - 无法在 spring mvc 中显示带有错误消息的同一页面

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:30 24 4
gpt4 key购买 nike

这个想法很简单,如果我的登录失败,用户将被重定向到 /loginerror 但无法正常工作

登录 Controller

 @RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
return "login";
}

@RequestMapping(value = "/loginerror", method = RequestMethod.GET)
public String loginFailed(Model model) {
model.addAttribute("error", "true");
return "login";
}

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(Model model) {
return "login";
}

登录.jsp

<c:if test="${not empty error}">
<div class="alert alert-danger">
<spring:message
code="AbstractUserDetailsAuthenticationProvider.badCredentials" />
<br />
</div>
</c:if>
<form action="<c:url value= "/j_spring_security_check"></c:url>"
method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="User Name"
name='j_username' type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password"
name='j_password' type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit"
value="Login">
</fieldset>
</form>
</div>

安全上下文.xml

<security:http auto-config="true">
<security:intercept-url pattern="/all/add"
access="hasRole('ROLE_ADMIN')" />
<security:form-login login-page="/login"
default-target-url="/all/add" authentication-failure-url="/loginerror" />
<security:logout logout-success-url="/logout" />
<security:csrf disabled="true" />
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" authorities="ROLE_ADMIN"
password="admin" />
</security:user-service>
</security:authentication-provider>

</security:authentication-manager>

我想做的是在登录失败时将用户重定向到登录页面,但每次我都会收到错误404页面在url localhost......../j_spring_security_check

上找不到

编辑

spring 错误消息为 AbstractUserDetailsAuthenticationProvider.badCredentials=您输入的用户名或密码不正确。

将调试语句放入 /loginerror 方法中后,我发现该方法甚至没有被调用

如果url错误如何拦截?

web.xml

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

最佳答案

您使用的spring版本不支持 <form action="<c:url value= "/j_spring_security_check"></c:url>" method="post">使用/login 作为 url 代替 <form action="<c:url value= "/login"></c:url>" method="post">既然你问这个链接清楚地解释了差异Why doesn't my custom login page show with Spring Security 4?

关于java - 无法在 spring mvc 中显示带有错误消息的同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39653650/

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