Spring Security Url Pattern无法正常工作

时间:2018-01-30 20:11:39

标签: java spring spring-mvc spring-security

这是我的web.xml

<filter>
    <display-name>springSecurityFilterChain</display-name>
    <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>

这是我的security-context.xml

     <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user authorities="admin" name="logan"
                    password="user" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
    <security:http use-expressions="true">
        <security:intercept-url pattern="/" access="permitAll" />
        <security:intercept-url pattern="/admin/*" access="denyAll" />
        <security:form-login />
    </security:http>
</beans>

我不知道为什么我仍然可以在/ admin之后访问所有的东西,比如/ admin / home将我重定向到仪表板而不是将我重定向到登录页面,这是如何工作的?春天我很新。

2 个答案:

答案 0 :(得分:1)

这仍然是猜测,但我认为&#34;只是&#34;添加&#34; asterix&#34; *过滤映射可以解决问题:

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Cewl,这也意味着你做了其他一切正确的事...... 在&#34;怎么&#34; &安培; &#34;为什么&#34; (这是web.xml / servlet规范具体的......假设3.0):

答案 1 :(得分:1)

您是否将文件包含在web.xml中,如:

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

您还可以尝试在网址格式中添加/*

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>