对于多登录界面,要求实现不同的用户,比如前台用户和后台用户,分别在以下情况中实现到不同页面的转向:
1、在未登录时,访问受限页面
2、在登录以后,转向到不同的默认页面,比如前台用户转向到个人中心主页,后台用户转向到后台管理页面的首页。
3、在登录失败时,导向到错误页面。
4、在注销登录时,不同的用户转向到不同的注销成功界面。
第一个问题,因为Spring Security会通过AuthenticationEntryPoint来实现未登录用户访问被保护资源时自动跳转到登录页面,所以我们这里需要的就是扩展AuthenticaitonEntryPoint。
首先修改配置文件,如下:
注意
LoginUrlEntryPoint.java内容如下:
public class LoginUrlEntryPoint implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
String targetUrl = null;
String url = request.getRequestURI();
if(url.indexOf("admin") != -1){
//未登录而访问后台受控资源时,跳转到后台登录页面
targetUrl = "/admin/login.jsp";
}else{
//未登录而访问前台受控资源时,跳转到前台登录页面
targetUrl = "/login.jsp";
}
targetUrl = request.getContextPath() + targetUrl;
response.sendRedirect(targetUrl);
}
}
以上就实现了问题一的解决。
第二、三问题, 在 进行表单认证的时候,UsernamePasswordAuthenticationFilter 是验证执行机制的过滤器,这与Spring Security2.x 不同,spring security2.x 用的是AuthenticationProcessingFilter 过滤器。在Spring Security3.x中已不推荐使用AuthenticationProcessingFilter 过滤器啦。
Spring Security 3.x的配置:
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
对比一下是不是就能看出很明显的差别啦,感觉Spring Security3.x的配置要比2.x写的多了些,还是2.x配置项写的少些。
第四个问题,我们也是通过定义两个LogoutFilter来实现。这个和Spring Security2.x配置差不多,只是过滤器实际中在
上面改用到的过滤器我们都配置完成啦,现在我们在
注意Spring Security2.x中的AUTHENTICATION_PROCESSING_FILTER在Spring Security3.x中已被FORM_LOGIN_FILTER替换。