无法使用Spring-Security保护HTML文件

时间:2014-05-03 18:22:39

标签: java spring-mvc spring-security

我有一个我在google appengine上部署的webapp。我认为这个问题与GAE没有关系,但我有些遗漏......

基本上,我想强制用户进行身份验证,以查看/使用/secured目录下的任何内容。我有这个目录下的HTML页面,但用户可以轻松导航到它(未经过身份验证)。如何使用SS保护它?

我看了thisthat,尝试过但却没有帮助: - (

我的配置 - web.xml:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<!-- to integrate Spring with AppEngine project -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

<!-- if we work with Spring-security, we already have a listener -->
<!-- listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener-->


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

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

弹簧servlet.xml中:

<context:annotation-config />

<context:property-placeholder location="classpath:client.properties" />

<context:component-scan base-package="com.nice.coffee" />
<context:component-scan base-package="com.ohadr.auth_flows" />
<context:component-scan base-package="com.ohadr.crypto" />

<mvc:annotation-driven />
<mvc:default-servlet-handler />

<!-- dont use debug! https://jira.spring.io/browse/SEC-1885 >
<sec:debug />
 -->

 <mvc:resources mapping="/secured/**" location="/secured/" />


<sec:http pattern="/login/**" security="none" />
<sec:http pattern="/forgotPasswordPage" security="none" />
<sec:http pattern="/forgotPassword" security="none" />
<sec:http pattern="/createAccountPage" security="none" />
<sec:http pattern="/createAccount" security="none" />

<sec:http authentication-manager-ref="authenticationManager">
    <sec:intercept-url pattern="/**/ohad.html" access="ROLE_ADMIN" />  
    <sec:intercept-url pattern="/secured/**" access="ROLE_USER" />
    <sec:anonymous />

    <sec:form-login login-page="/login/login.htm"
        authentication-success-handler-ref="authenticationSuccessHandler"
        authentication-failure-handler-ref="authenticationFailureHandler" />

</sec:http>



<sec:authentication-manager alias="authenticationManager">
    <sec:authentication-provider
        user-service-ref="userDetailsService">
        <sec:password-encoder hash="sha-256">
            <sec:salt-source user-property="username" />
        </sec:password-encoder>
    </sec:authentication-provider>
</sec:authentication-manager>...

我的项目层次结构:

enter image description here

...提前致谢!

2 个答案:

答案 0 :(得分:1)

而不是将安全的pags放在src/main/webapp/secured下,直接提供,将它们放在src/main/resources/secured中,并将资源声明更改为

  

<mvc:resources mapping="/secured/**" location="classpath:/secured/" />

答案 1 :(得分:0)

看来我的问题出在这一行:

 <mvc:resources mapping="/secured/**" location="/secured/" />

spring-mvc是“混乱”,其中位置和映射都具有相同的名称。因此,当对资源的请求进入应用程序时,例如... / secured / my.html,spring-mvc根本不使用映射。

解决方案是更改位置名称(或映射,但我更改了位置名称),所以我最终得到了:

 <mvc:resources mapping="/secured/**" location="/secured_resources/" />

我所有的资源(html,JS等)都在名为“ secured_resources ”的目录下。然后,当请求到达应用程序时,例如... / secured / my.html,它使用MVC成功映射,因此浏览器被重定向到登录页面等。