对于多登录界面,要求实现不同的用户,比如前台用户和后台用户,分别在以下情况中实现到不同页面的转向:
1、在未登录时,访问受限页面
2、在登录以后,转向到不同的默认页面,比如前台用户转向到个人中心主页,后台用户转向到后台管理页面的首页。
3、在登录失败时,导向到错误页面。
4、在注销登录时,不同的用户转向到不同的注销成功界面。
第一个问题,因为Spring Security会通过AuthenticationEntryPoint来实现未登录用户访问被保护资源时自动跳转到登录页面,所以我们这里需要的就是扩展AuthenticaitonEntryPoint。
首先修改配置文件,如下:
<http entry-point-ref="loginPageEntryPoint">
<intercept-url pattern="/login.**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/admin/login.**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<intercept-url pattern="/**" access="ROLE_USER"/>
<beans:bean id="loginUrlEntryPoint" class="cn.csdn.blog.loafap.security.LoginUrlEntryPoint"></beans:bean>
注意<http> 的配置,因为我们要通过自定义过滤器来实现,所以这里去掉auto-config="true"这个属性。
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的配置:
<!-- 验证前台用户 -->
<beans:bean id="loginFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref=" "/>
<beans:property name="authenticationFailureHandler" ref="failureHandler"/>
<beans:property name="authenticationSuccessHandler" ref="successHandler"/>
<beans:property name="filterProcessesUrl" value="/login"/>
</beans:bean>
<beans:bean id="failureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<beans:property name="defaultFailureUrl" value="/login.jsp?error=true" />
</beans:bean>
<beans:bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="alwaysUseDefaultTargetUrl" value="true"/>
<beans:property name="defaultTargetUrl" value="/main.jsp"/>
</beans:bean>
<!-- 验证后台用户 -->
<beans:bean id="adminLoginFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="authenticationFailureHandler" ref="adminFailureHandler"/>
<beans:property name="authenticationSuccessHandler" ref="adminSuccessHandler"/>
<beans:property name="filterProcessesUrl" value="/admin/login"/>
</beans:bean>
<beans:bean id="adminFailureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<beans:property name="defaultFailureUrl" value="/admin/login.jsp?error=true" />
</beans:bean>
<beans:bean id="adminSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="alwaysUseDefaultTargetUrl" value="true"/>
<beans:property name="defaultTargetUrl" value="/admin/main.jsp"/>
</beans:bean>
对比一下是不是就能看出很明显的差别啦,感觉Spring Security3.x的配置要比2.x写的多了些,还是2.x配置项写的少些。
第四个问题,我们也是通过定义两个LogoutFilter来实现。这个和Spring Security2.x配置差不多,只是过滤器实际中在<http>中注册的,而不是直接在<bean>里注册。
<!-- 注销过滤器,完成前台用户注销时的定向功能 -->
<beans:bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg value="/login.jsp" />
<beans:constructor-arg>
<beans:list>
<beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
</beans:list>
</beans:constructor-arg>
<beans:property name="filterProcessesUrl" value="/logout" />
</beans:bean>
<!-- 注销过滤器,完成后台用户注销时的定向功能 -->
<beans:bean id="adminLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg value="/admin/login.jsp" />
<beans:constructor-arg>
<beans:list>
<beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" />
</beans:list>
</beans:constructor-arg>
<beans:property name="filterProcessesUrl" value="/admin/logout" />
</beans:bean>
上面改用到的过滤器我们都配置完成啦,现在我们在<http>中配置我们所需要的这4个过滤器,这点和2.x的配置是不同的。
<http entry-point-ref="loginUrlEntryPoint">
<intercept-url pattern="/login.**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/admin/login.**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<intercept-url pattern="/**" access="ROLE_USER"/>
<!-- 登录过滤器 -->
<custom-filter before="FORM_LOGIN_FILTER" ref="loginFilter"/>
<custom-filter position="FORM_LOGIN_FILTER" ref="adminLoginFilter"/>
<!-- 注销过滤器 -->
<custom-filter before="LOGOUT_FILTER" ref="logoutFilter"/>
<custom-filter position="LOGOUT_FILTER" ref="adminLogoutFilter"/>
</http>
注意Spring Security2.x中的AUTHENTICATION_PROCESSING_FILTER在Spring Security3.x中已被FORM_LOGIN_FILTER替换。