注意:分析的版本是Springsecurity的4.3.x。html
在ExceptionTranslationFilter中使用到AuthenticationEntryPoint,当ExceptionTranslationFilter截获AuthenticationExceptionjava
或者AccessDeniedException异常时,就会调用AuthenticationEntryPoint的commence。AuthenticationEntryPoint有不少实现类,咱们来看下CasAuthenticationEntryPoint,这个与单点登陆有关。先上一张图,以下图1所示。web
图1 CasAuthenticationEntryPoint的继承图spring
若是查看Springsecurity的cas那部分的文档,会看到以下List-1所示的内容,浏览器
List-1url
<bean id="casEntryPoint"class="org.springframework.security.cas.web.CasAuthenticationEntryPoint"> <property name="loginUrl" value="https://localhost:9443/cas/login"/> <property name="serviceProperties" ref="serviceProperties"/> </bean> <bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties"> <property name="service" value="https://localhost:8443/cas-sample/login/cas"/> <property name="sendRenew" value="false"/> </bean>
CasAuthenticationEntryPoint的commence方法中,以下List-2所示,方法createServiceUrl和createRedirectUrl是构建调用CAS server的url,方法preCommence是空方法体,最后设置respose的redirect地址,这样浏览器收到的是302状态码,而后浏览器访问List-2中的redirectUrl,redirectUrl的值是"https://localhost:9443/cas/login?service=https://localhost:8443/cas-sample/login/cas"。code
List-2 server
public final void commence(final HttpServletRequest servletRequest, final HttpServletResponse response, final AuthenticationException authenticationException) throws IOException, ServletException { final String urlEncodedService = createServiceUrl(servletRequest, response); final String redirectUrl = createRedirectUrl(urlEncodedService); preCommence(servletRequest, response); response.sendRedirect(redirectUrl); }
注:List-2中的createServiceUrl和createRedirectUrl在构造Http请求连接的时候,有点复杂,这里就再也不细讲。xml