【问题标题】:Spring Security on Rest services休息服务上的 Spring Security
【发布时间】:2012-10-13 01:32:59
【问题描述】:

我已经使用 Spring MVC、Restful webservices 为移动客户端实现了服务器后端。我想用弹簧安全保护应用程序。我已经放了一个自定义的 login.jsp 来处理登录,如下所示。

<body onload='document.f.j_username.focus();'>
<h3>Login with Username and Password (Custom Page)</h3>

<c:if test="${not empty error}">
    <div class="errorblock">
        Your login attempt was not successful, try again.<br /> Caused :
        ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
    </div>
</c:if>

<form name='f' 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 colspan='2'><input name="submit" type="submit"
                value="submit" />
            </td>
        </tr>
        <tr>
            <td colspan='2'><input name="reset" type="reset" />
            </td>
        </tr>
    </table>

</form>

我的web.xml如下。

<display-name>Spring MVC Application</display-name>

<!-- Spring MVC -->
<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/mvc-dispatcher-servlet.xml,
        /WEB-INF/spring-security-context.xml
    </param-value>
</context-param>

<!-- Spring Security -->
<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>

spring-security-context.xml 如下。

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


<http auto-config="true">
    <intercept-url pattern="/getHierarchy" access="ROLE_USER" />
    <form-login login-page="/login" default-target-url="/getHierarchy"
        authentication-failure-url="/loginfailed" />
    <logout logout-success-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="admin" password="admin" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

但我在 eclipse 上收到警告

在 Java 构建路径中找不到“c:if” (org.apache.taglibs.standard.tag.rt.core.IfTag) 的标记处理程序类

当我提交表单时,在 j_spring_security_check 上出现 not found 错误。我在类路径中包含了 jstl1.2。为什么即使我已包含 taglib,我也会收到未找到标记处理程序的警告?

【问题讨论】:

  • 您是否发布了 COMPLETE login.jsp 或者您是否跳过了标题?
  • 是的,我跳过了标题...其中只有几个样式标签。仅此而已。 Taglib 声明如下。 &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  • 您写道:I have included jstl1.2 in the classpath. 它到底包含在哪个文件夹中?例如\myProject\libs\myProject\webapp\WEB-INF\lib?
  • 它在 \myProject\webapp\WEB-INF\lib 文件夹中

标签: spring spring-security taglib


【解决方案1】:

为什么即使我已包含 taglib,我也会收到未找到标记处理程序的警告? 应部署到服务器的 Eclipse 动态 Web 项目中的 jar 必须位于 \myProject\WebContent\WEB-INF\libs (不在 \myProject\webapp\WEB-INF\lib 中)

在 Eclipse 动态 Web 项目中,此文件夹中的 jar 将自动附加到您的构建路径,您不得手动添加它们。


当我提交表单时,在 j_spring_security_check 上出现 not found 错误:

检查您的登录页面是否已映射到 url /login 您只需在浏览器中输入 url 即可对其进行测试


附加提示

另一件可能不是那么理想的事情是这个陈述

<form name='f' action="<c:url value='j_spring_security_check' />"
   method='POST'>

试着这样写:

<c:url value='j_spring_security_check' var='loginUrl'/>
<form name='f' action="${loginUrl}" method='POST'>

这避免了两个标签的这种讨厌的编织。也许问题只是eclipse编辑器有点混乱。


【讨论】:

  • 在我的开发项目中,jar 位于\myProject\WebContent\WEB-INF\libs,但问题仍然存在。但是当我如下更改安全上下文文件中的 http 标记时,&lt;http auto-config="true"&gt; &lt;intercept-url pattern="/getHierarchy" access="ROLE_USER" /&gt; &lt;http-basic/&gt; &lt;/http&gt; 它工作正常。我猜标签库有问题。
  • 我补充了一点,不过这只是一个提示,应该和你没有找到的问题无关。
猜你喜欢
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2016-07-14
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2016-04-02
相关资源
最近更新 更多