我有这些规则来访问我的应用程序中的页面:
<http auto-config="true" use-expressions="true">
<!-- NON AUTHENTICATION PAGES -->
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/about" access="permitAll" />
<!-- LOGIN FILTER -->
<intercept-url pattern="/login" access="!isAuthenticated()" />
<intercept-url pattern="/j_spring_security_check" access="!isAuthenticated()" />
<intercept-url pattern="/logout" access="!isAuthenticated()" />
<!-- RESOURCES AND OTHER URLs FILTER -->
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<!-- FORM LOGIN -->
<form-login login-page="/login" default-target-url="/upload" authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
我需要的是在访问失败时重定向到某个 url(例如 /access-denied)并在控制器中处理此事件。
@RequestMapping(value = "/access-denied", method = RequestMethod.GET)
public String accessDenied(Model model, RedirectAttributes ra) {
// do what I want
return "redirect:login";
}
例如,用户输入 /upload 并且他没有登录,因此它将被重定向到 /access-denied。