2016-11-15 30 views
-2

我正在嘗試使用Spring Security創建簡單的應用程序並使用登錄功能。但我無法達到預期的效果。 我的.jsp頁面上的JSTL標記沒有通過測試,而腳本代碼卻在我的應用程序中避免使用。爲什麼我的JSTL if-check不起作用?

我的JSP頁面。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<html> 
<head> 
    <title>Log in page</title> 
</head> 
<body> 
<form action="<c:url value="/login"/>" method="POST"> 
    <p> 
     <label for="username">Username</label> 
     <input type="text" id="username" name="username"/> 
    </p> 
    <p> 
     <label for="password">Password</label> 
     <input type="password" id="password" name="password"/> 
    </p> 
    <button type="submit" class="btn">Log in</button> 
</form> 

// This block of code is never executed 
<c:if test="${error != null}"> 
    Some test message 
</c:if> 

// While this one works fine 
<% 
    if (request.getParameter("error") != null) { 
     out.write("error's value isn't null\n"); 
    } 
%> 
</body> 
</html> 

WebSecurityConfigurerAdapter重寫配置方法:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 

     http.antMatchers("/product") 
      .access("hasRole('ROLE_MANAGER')"); 

     http.authorizeRequests().and().formLogin() 
      .loginPage("/login") 
      .defaultSuccessUrl("/admin") 
      .failureUrl("/login?error") 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .and().logout().logoutSuccessUrl("/login?logout"); 
} 

Spring MVC的控制器的方法,用於映射 「/登錄」 請求。

@RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String loginPage(Model model, 
          @RequestParam(value = "error", required = false) String error { 
     if (error != null) { 
      model.addAttribute("error", "Username or password is incorrect."); 
     } 

     return "login"; 
    } 

這是我所得到的請求http://localhost/login?error

Image

回答

0

我已經改變

<%@ page contentType="text/html;charset=UTF-8" language="java"%> 

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> 

正如我未理解,EL評估已停用,所以我手動激活了它。