我正在开发Spring Security Integration, 我内置安全页面的应用程序运行正常。 但是对于自定义登录页面,它给出了错误
The localhost page isn’t working
这是我的参考代码
在spring-security.xml中
<http use-expressions="true">
>
<intercept-url pattern="/**" access="isAuthenticated()"/> <!-- this means all URL in this app will be checked if user is authenticated -->
<form-login
login-page="/login"
default-target-url="/"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-url="/logout" logout-success-url="/"/> <!-- the logout url we will use in JSP -->
</http>
在login.jsp
中<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Custom Login Page</title>
</head>
<body onload='document.loginForm.j_username.focus();'>
<h3>Custom Login Page</h3>
<%
String errorString = (String)request.getAttribute("error");
if(errorString != null && errorString.trim().equals("true")){
out.println("Incorrect login name or password. Please retry using correct login name and password.");
}
%>
<form name='loginForm' action="<c:url value='j_spring_security_check' />"
method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='j_username' value=''>
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='j_password' />
</td>
</tr>
<tr>
<td><input name="submit" type="submit"
value="submit" />
</td>
<td><input name="reset" type="reset" />
</td>
</tr>
</table>
</form>
</body>
</html>
在控制器中:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(ModelMap mode,
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
System.out.println("\n\t -------------");
ModelAndView model = new ModelAndView("login");
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
return model;
}
当我使用内置形式的pulgin运行项目时,网址是
http://localhost:8080/SpringMvcSecurity/spring_security_login
并在登录后如果我手动点击网址
http://localhost:8080/SpringMvcSecurity/login
它显示了登录页面 但是,如果页面显示此时间,那么我不会为什么不显示自定义配置。 请帮忙。
谢谢shazin 有4个变化
- 需要更改RequestMapping网址中的网址名称,例如:customlogin
- 同名需要在spring-security.xml中替换
- 在spring-security.xml中添加行
- 替换用户名密码属性名称,以及jsp页面中显示的方式。
醇>
<intercept-url pattern="/customlogin" access="permitAll" />
答案 0 :(得分:1)
shazin its shows neither exception nor 404 its displays message saying, localhost redirected you too many times..that's it. thanks for your reply
这是因为您的@RequestMapping
值和.jsp名称为login
。改变至少一个如下。
@RequestMapping(value = "/customlogin", method = RequestMethod.GET)
public ModelAndView login(ModelMap mode,
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
System.out.println("\n\t -------------");
ModelAndView model = new ModelAndView("login");
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
return model;
}
并在您的配置中
<http pattern="/customlogin*" security="none"/>
<http use-expressions="true">
>
<intercept-url pattern="/**" access="isAuthenticated()"/> <!-- this means all URL in this app will be checked if user is authenticated -->
<form-login
login-page="/customlogin"
default-target-url="/"
authentication-failure-url="/customlogin?error"
username-parameter="j_username"
password-parameter="j_password" />
<logout logout-url="/logout" logout-success-url="/"/> <!-- the logout url we will use in JSP -->
</http>