有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

显示自定义页面时出现java Spring安全错误

我正在研究Spring安全集成, 我的内置安全页面应用程序运行良好。 但对于自定义登录页面,它的给出错误

The localhost page isn’t working

这是我的代码供参考

春季安全内部。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>

在登录中。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运行项目时,url是

http://localhost:8080/SpringMvcSecurity/spring_security_login

登录后如果我手动点击网址

http://localhost:8080/SpringMvcSecurity/login

它显示了登录页面 但是我不知道如果页面这次显示,那么为什么它没有在自定义配置中显示。 请帮忙

谢谢沙辛 有4个变化

  1. Need to change the url name in RequestMapping url for ex:customlogin
  2. same name need to replace in spring-security.xml
  3. add line in spring-security.xml
  4. replace the username password attribute names, the way those appears in jsp page.

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


共 (1) 个答案

  1. # 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>