Springsecurity之AuthenticationEntryPoint

本文深入探讨了Spring Security中CasAuthenticationEntryPoint的实现机制,详细分析了其如何处理AuthenticationException及AccessDeniedException,通过构建调用CAS server的URL实现单点登录流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    注意:分析的版本是Springsecurity的4.3.x。

    在ExceptionTranslationFilter中使用到AuthenticationEntryPoint,当ExceptionTranslationFilter截获AuthenticationException

或者AccessDeniedException异常时,就会调用AuthenticationEntryPoint的commence。AuthenticationEntryPoint有很多实现类,我们来看下CasAuthenticationEntryPoint,这个与单点登录有关。先上一张图,如下图1所示。

  

                                                图1 CasAuthenticationEntryPoint的继承图

    如果查看Springsecurity的cas那部分的文档,会看到如下List-1所示的内容,

    List-1

<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"。

    List-2 

	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请求链接的时候,有点复杂,这里就不再细讲。

转载于:https://my.oschina.net/u/2518341/blog/2874750

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值