【问题标题】:Can't get Spring security to work BASIC authentication无法让 Spring 安全性工作 BASIC 身份验证
【发布时间】:2013-04-28 18:16:56
【问题描述】:

我正在尝试添加 Spring 安全性,但无法使其正常工作。但是我原以为这会导致浏览器弹出一个对话框,询问用户名和密码,但没有显示任何内容。它只是给我带来了正常的页面,而没有强制执行任何安全措施。使用 Spring 3.1:

web.xml:

<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.xml:

<security:http>
    <security:http-basic />
    <security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="test" password="testpass" authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

【问题讨论】:

    标签: java spring rest spring-mvc


    【解决方案1】:

    你也需要这样的东西

     <security:form-login login-page='/Login' authentication-failure-url="/Login?login_error=1"/>
    

    您需要创建自己的登录页面,其中至少包含此内容,并映射到上述 url(显然不一定与我的相同):

    <form name="f" id="f" action="<c:url value='/j_spring_security_check'/>" method="POST">
      <label for="j_username">Username :&nbsp;</label>
      <input type='text' id='j_username' name='j_username'/><br/>
      <label for="j_password">Password :&nbsp;</label>
      <input type='password' id='j_password' name='j_password'><br>
    

    【讨论】:

    • 他正在尝试使用 http BASIC 身份验证而不是表单身份验证
    【解决方案2】:

    您需要在 web.xml 中添加 spring 安全上下文,以便网络应用程序可以正确加载您的 context.xml。

    这是how to do basic authentication using spring security or even with some customization上的一个例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-18
      • 2011-05-04
      • 1970-01-01
      • 2014-04-30
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      相关资源
      最近更新 更多