Spring Security Web : AuthenticationEntryPoint 认证入口点及其实现类简介

AuthenticationEntryPoint简介

AuthenticationEntryPointSpring Security Web一个概念模型接口,顾名思义,他所建模的概念是:“认证入口点”。
它在用户请求处理过程中遇到认证异常时,被ExceptionTranslationFilter用于开启特定认证方案(authentication schema)的认证流程。

该接口只定义了一个方法 :

void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException;

这里参数request是遇到了认证异常authException用户请求,response是将要返回给客户的相应,方法commence实现,也就是相应的认证方案逻辑会修改response并返回给用户引导用户进入认证流程。

在该方法被调用前, ExceptionTranslationFilter会做好如下工作 :

  1. 填充属性HttpSession,使用属性名称为AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY

AuthenticationEntryPoint源代码

package org.springframework.security.web;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.access.ExceptionTranslationFilter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public interface AuthenticationEntryPoint {

	/**
	 * Commences an authentication scheme.
	 * 
	 * ExceptionTranslationFilter will populate the HttpSession
	 * attribute named
	 * AbstractAuthenticationProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY
	 * with the requested target URL before calling this method.
	 * 
	 * Implementations should modify the headers on the ServletResponse as
	 * necessary to commence the authentication process.
	 *
	 * @param request that resulted in an AuthenticationException
	 * @param response so that the user agent can begin authentication
	 * @param authException that caused the invocation
	 *
	 */
	void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException;
}

Spring Security Web内置AuthenticationEntryPoint实现类

Spring Security WebAuthenticationEntryPoint提供了一些内置实现 :

  • Http403ForbiddenEntryPoint

    设置响应状态字为403,并非触发一个真正的认证流程。通常在一个预验证(pre-authenticated authentication)已经得出结论需要拒绝用户请求的情况被用于拒绝用户请求。

  • HttpStatusEntryPoint

    设置特定的响应状态字,并非触发一个真正的认证流程。

  • LoginUrlAuthenticationEntryPoint

    根据配置计算出登录页面url,将用户重定向到该登录页面从而开始一个认证流程。

  • BasicAuthenticationEntryPoint

    对应标准Http Basic认证流程的触发动作,向响应写入状态字401和头部WWW-Authenticate:"Basic realm="xxx"触发标准Http Basic认证流程。

  • DigestAuthenticationEntryPoint

    对应标准Http Digest认证流程的触发动作,向响应写入状态字401和头部WWW-Authenticate:"Digest realm="xxx"触发标准Http Digest认证流程。

  • DelegatingAuthenticationEntryPoint

    这是一个代理,将认证任务委托给所代理的多个AuthenticationEntryPoint对象,其中一个被标记为缺省AuthenticationEntryPoint

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值