2016-09-29 61 views
0

我正在使用Spring Security Integration, 我的内置安全页面的应用程序工作正常。 但对于自定义登录页面的给错误Spring Security在显示自定义页面时出错

The localhost page isn’t working 

这里是我的参考

弹簧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个变化

  1. 需要RequestMapping网址更改URL名称为前:customlogin
  2. 同名需要弹簧security.xml文件
  3. 添加替换line in spring-security.xml
  4. 替换用户名密码属性名称,这些出现在jsp页面的方式。

<intercept-url pattern="/customlogin" access="permitAll" />

+0

请说明你有问题。它会返回404吗?或例外? – shazin

+0

shazin其显示既不例外,也不显示404消息说,localhost重定向你太多次了。就是这样。感谢您的回复 – Kamini

+0

请不要在您的问题中发布解决方案。写一个新的答案并接受它。 – dit

回答

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> 
+0

好的,谢谢我会检查并更新你。 – Kamini

+0

它显示相同的错误。 :( – Kamini

+0

Shazin在添加下面的行后开始工作 请编辑您的答案并添加此行并更改security.xml中的j_username和j_password – Kamini