我的 Web 应用程序有一堆“普通”资源(html 页面等)以及一些由前面提到的 html 页面从 JavaScript 调用的 REST 资源。
如果会话超时,用户将被重定向到登录表单。这对于“普通”资源来说非常有用,但对于 REST 资源则不然。我只需要 403 响应,以便 JavaScript 可以接管并要求用户重新进行身份验证。
网络上有无数如何配置这些方法的示例,但我找不到有关如何组合这些方法的示例。我的所有 API URL 都以“/api/”开头,因此我需要所有这些 URL 的 403 以及所有剩余 URL 的重定向。我该如何设置?
我花了一点时间研究 Spring 源代码才让它工作。您可以按如下方式设置身份验证入口点:
<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint">
<!-- this is the configuration for /api/ URLs -->
<constructor-arg>
<map>
<entry>
<key>
<bean class="org.springframework.security.web.util.matcher.RegexRequestMatcher">
<constructor-arg value="^/api/.*" /><!-- match URLs starting with "/api/" -->
<constructor-arg><null /></constructor-arg><!-- no matter what the HTTP method is -->
</bean>
</key>
<!-- if the key above has matched, send 403 response -->
<bean class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" />
</entry>
</map>
</constructor-arg>
<!-- and in the default case just redirect to login form -->
<property name="defaultEntryPoint">
<bean class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<constructor-arg value="/spring_security_login" />
</bean>
</property>
</bean>
然后可以在 Spring Security 配置中使用它:
<http ... entry-point-ref="authenticationEntryPoint">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)