我有一个简单的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/