【问题标题】:Problem trying to use spring security with my jsp aplication尝试在我的 jsp 应用程序中使用 Spring Security 时出现问题
【发布时间】:2011-10-04 22:33:59
【问题描述】:

按照教程使用 spring security 似乎很容易,但是当我尝试对这个应用程序做同样的事情时,我可以让它工作(我必须承认我没有写过)。

/-----------------编辑-------------------/

现在在一些伟人的帮助下,我终于让应用程序要求用户进行身份验证,但现在我遇到了一个新问题:当我尝试使用下面列出的用户之一访问应用程序时(applicationContext-security.xml),我收到这条消息:

这里是web.xml 代码:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>  
    <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> 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-security.xml</param-value>
    </context-param>  
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>  
    <error-page>
        <error-code>403</error-code>
        <location>/sinacceso.jsp</location>
    </error-page>
</web-app>

context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Test01"/>

dispatcher-servlet.xml:

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- GENERALES -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>
    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>

    <bean name="/index.htm" class="paginas.index"/>
    <bean name="secure/villancicos.htm" class="paginas.villancicos"/>
    <bean name="secure/administracion.htm" class="paginas.administracion"/>
</beans>

applicationContext-security.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"
             xmlns:p="http://www.springframework.org/schema/p"
             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.1.xsd">

    <http>
        <intercept-url pattern="/index.htm" filters="none" />
        <intercept-url pattern="/secure/administracion.htm" access="ROLE_SUPERVISOR" />
        <intercept-url pattern="/secure/**" access="ROLE_USER" />
        <form-login />
        <anonymous />
        <http-basic />
        <logout logout-success-url="/index.htm" />
    </http>
    <authentication-manager alias="myAuthenticationManager">
        <authentication-provider>
            <password-encoder hash="md5"/>
            <user-service>
                <user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
                <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
                <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
                <user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

此应用程序当前使用带有 spring-security-2.0.4 的 spring 2.5(与我一直关注的教程中的相同版本)。

【问题讨论】:

  • 看起来您使用的是版本 3 架构:spring-beans-3.0.xsd 和 spring-security-3.0.xsd 和
  • @Ritesh- 我应该使用哪些? :( 因为我已经尝试过使用 spring-beans-2.0.xsd 和 spring-security-2.0.1.xsd,但我一直收到同样的错误 :(
  • 好吧,如果您使用的是 Spring 2.5,那么它应该是 spring-beans-2.5.xsd。 spring-security-2.0.4.xsd 应该与 spring-security-2.0.4 一起使用。另外,您是否在任何日志文件中看到任何错误堆栈?
  • 您要部署到哪个应用服务器?
  • @Ritesh- 我试过你告诉我的,但没有任何改变,至于 tomcat 日志,我已经用完整的日志输出更新了我的问题。请看一下:(

标签: spring jsp spring-security


【解决方案1】:

您是否尝试过错误提示并为您的身份验证管理器添加了别名属性?比如:

<authentication-manager alias="myAuthenticationManager">
    <authentication-provider>
        ...

【讨论】:

  • 谢谢。我按照你的建议做了,它奏效了,我的意思是我不再收到错误消息,但我还必须改回模式的版本(否则,我收到了更多错误消息),所以现在我使用 spring-beans-3.0.xsd 和 spring-security-3.0.xsd,但即使经过所有这些更改,我也无法让 spring security 工作,它只是没有'什么都不做(我仍然可以在不登录的情况下访问页面):(
  • @eddy 您如何访问该应用程序?你能举一个你页面的URL作为例子吗?喜欢server/myApp/Layout/myPage.htm
  • @eddy:该 URL 的路径中没有 /Layout,并且这些是您告诉 Spring Security 拦截的唯一 URL。
  • @Ryan- 你说得对,我删除了 /Layout 现在每次我尝试访问除索引页面以外的任何内容时,都会要求我登录,这很好,但现在似乎有别的东西了不能正常工作,请看一下我添加的错误消息的屏幕截图
猜你喜欢
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 2011-11-11
  • 2020-06-03
相关资源
最近更新 更多