【问题标题】:Spring security not awares of spring mvc's view prefixSpring security 不知道 spring mvc 视图前缀
【发布时间】:2015-08-21 00:10:28
【问题描述】:

我有一个简单的spring mvc + spring security 应用程序

我声明当用户尝试.../hello 时,来自/WEB-INF/pages/ 路径的hello.jsp 页面正确显示:

@RequestMapping(value = "/hello")
public ModelAndView doLogin() {
    return new ModelAndView("hello");
}

dispatcher-servlet.xml:

<mvc:annotation-driven/>

<context:component-scan base-package="com"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

现在我在 Spring Security 中声明,当用户想要 .../hello必须成为用户。

但是当我尝试http://localhost:8080/hello/ 时,hello.jsp 页面会显示而不会强制进行身份验证。

当我尝试http://localhost:8080/pages/hello.jsp 时,它会将我重定向到login 页面,但是为什么spring security 没有注意到spring mvc 的prefix path

更新

security-config.xml

<security:http auto-config="true" use-expressions="true">
    <security:intercept-url pattern="/pages/hello.jsp" access="hasRole('ROLE_USER')"/>
</security:http>

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

当我使用pattern="/hello" 并且用户尝试http://localhost:8080/hello/ 时,用户不会重定向到login 页面。

【问题讨论】:

    标签: java spring jsp spring-mvc spring-security


    【解决方案1】:

    我将intercept-url pattern 更改为pattern="/hello/",它可以工作。

    【讨论】:

      【解决方案2】:

      尝试从

      更改此设置
       <security:intercept-url pattern="/pages/hello.jsp" access="hasRole('ROLE_USER')"/>
      

       <security:intercept-url pattern="/pages/hello*" access="hasRole('ROLE_USER')"/>
      

      在您的拦截配置中,您特别要求只检查 hello.jsp 而不是 hello。 Spring Security 不会根据您的视图解析器自动更改逻辑。

      【讨论】:

      • 由于浏览器会自动在 url 的末尾添加一个额外的/,我添加了一个/ 和模式的末尾,并从 url 中删除了/pages/
      猜你喜欢
      • 2012-01-09
      • 2014-03-04
      • 2012-03-29
      • 1970-01-01
      • 2018-04-15
      • 2012-12-05
      • 2011-01-08
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多