Spring Security - 即使凭据正确,身份验证也不起作用

Posted

技术标签:

【中文标题】Spring Security - 即使凭据正确,身份验证也不起作用【英文标题】:Spring Security - Authentication not working even the credentials are correct 【发布时间】:2016-08-26 12:07:19 【问题描述】:

我在我的应用程序中使用 Spring Security,我正在拦截一些 URL 以进行身份​​验证。虽然 URL "/securedMapping1" 通过显示登录页面提示用户登录,但是登录不起作用。即使我提供了正确的凭据,我也会通过调用失败的身份验证 URL 即 authentication-failure-url="/login?error =true" 每次都会调用,无论凭据是否正确。谁能帮我弄清楚出了什么问题?以下是重要文件中的代码:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">

  <servlet>
      <servlet-name>spring-sec</servlet-name>
      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>    

  <servlet-mapping>
      <servlet-name>spring-sec</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-sec-servlet.xml</param-value>
  </context-param> 

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

spring-sec-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.1.xsd  
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
     http://www.springframework.org/schema/security
     http://www.springframework.org/schema/security/spring-security-4.0.xsd">

  <context:component-scan base-package="com.mir.*" />
  <context:annotation-config />
  <bean       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix"><value>/WEB-INF/pages/</value></property>
      <property name="suffix"><value>.jsp</value></property>        
  </bean>

  <security:http auto-config="true" use-expressions="true">
    <security:intercept-url pattern="/securedMapping1" access="hasRole('ROLE_ADMIN')"/>

    <security:intercept-url pattern="/" access="permitAll" />
    <security:intercept-url pattern="/hello" access="permitAll" />
    <security:form-login login-page="/login" 
        login-processing-url="/j_spring_security_check"
        default-target-url="/dashboard" 
        authentication-failure-url="/login?error=true"/>
    <security:logout logout-success-url="/logout" />
  </security:http>

  <security:authentication-manager>
     <security:authentication-provider>
        <security:user-service>
            <security:user name="admin" password="test123" authorities="ROLE_ADMIN" />
        </security:user-service>
     </security:authentication-provider>
  </security:authentication-manager>
</beans>

MyAppController.java

@Controller
public class MyAppController 

    public MyAppController() 
        System.out.println("Constructor...");
    

   @RequestMapping("/hello")
   public String hello(Model model) 
       model.addAttribute("greeting", "Hello Guest");
       return "helloworld";
   

   @RequestMapping("/securedMapping1")
   public String method1(Model model) 
       model.addAttribute("greeting", "Hello "+getPrincipal()+ ", --> Accessed via secured URL.");
       return "helloworld";
   

   @RequestMapping("/dashboard")
   public String method2(Model model) 
       model.addAttribute("greeting", "Hello --- DEFAULT TARGET URL ---");
       return "helloworld";
   

   @RequestMapping("/login")
   public String method3(Model model) 
       System.out.println(" Going to display Login page...");
       return "login";
   

// Logout page
@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logout(ModelMap model) 
    return "login";


login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring4Sec MVC</title>
</head>
<body>
  <h1>Login</h1>

  <c:if test="$not empty param.error">
    <font color="red">
        Login Error <br/>
        Reason: "$sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message" <br/>
        User: <c:out value="$SPRING_SECURITY_LAST_USERNAME"/>
    </font>
  </c:if>

  <form action="<c:url value="/j_spring_security_check"/>" method="post">
      <input type="hidden" name="$_csrf.parameterName" value="$_csrf.token"/>
      user: <input type="text" name="j_username"/>
      password: <input type="password" name="j_password"/>
      <input type="submit" value="Login">
  </form>
</body>
</html>

helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring4Sec MVC</title>
</head>
<body>
  <h1>$greeting</h1>
  <a href="<c:url value="/securedMapping1" />">Secure</a>
</body>
</html>

【问题讨论】:

发现它就像一个魅力。非常感谢@dur。我已经不知所措了一段时间。这是 login.jsp 中修改后的代码部分:user: &lt;input type="text" name="username"/&gt; password: &lt;input type="password" name="password"/&gt; 【参考方案1】:

见Spring Security reference:

- password-parameter 包含密码的请求参数的名称。默认为“密码”。

- username-parameter 包含用户名的请求参数的名称。默认为“用户名”。

【讨论】:

以上是关于Spring Security - 即使凭据正确,身份验证也不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Security - 即使凭据正确,身份验证也不起作用

即使凭据为真,Spring Boot Security 也会在 API 调用上引发 401 身份验证错误

Spring Security OAuth2 用户凭据不正确

无法使用Spring Security弹出用户凭据请求?

Spring Security Active Directory 错误凭据处理(错误 49)

Spring Boot Security:编码密码看起来不像 BCrypt