java - Spring security 不知道 spring mvc View 前缀

标签 java spring jsp spring-mvc spring-security

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

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

@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 没有注意到 prefix path Spring MVC 的?

更新

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页。

最佳答案

我将 intercept-url 模式 更改为 pattern="/hello/" 并且它有效。

关于java - Spring security 不知道 spring mvc View 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676749/

相关文章:

java - 如何使用Xpath选择特定容器的span元素(<div>由CSS类定义)?

java - Jmeter 2.10 HTTP 记录器抛出 keytool 异常

java - 任何其他更好的方法来处理使用Java Stream或Optional.ofNullable

Spring boot 应用程序不会熬夜

java - 如何使用超链接将表中的索引作为 struts 中的参数发送

java - 自定义阻塞队列锁定问题

spring - 如何使用 spring-boot 微服务架构实现一项任务只运行在一个实例上

java - 创建名为 'entityManagerFactory' :java. lang.NoSuchMethodError : javax. persistence.Table.indexes() 的 bean 时出错

java - 使用单个提交按钮发送下拉列表的多个值

java - 在jsp中添加