2016-09-29 58 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