[10] AnonymousAuthenticationFilter

本文深入解析AnonymousAuthenticationFilter工作原理,阐述其如何在SecurityContext中创建匿名身份认证信息,包括使用UUID作为key,设置principal为“anonymousUser”,以及授予ROLE_ANONYMOUS权限。

AnonymousAuthenticationFilter

介绍

该过滤器功能比较简单,请求经过过滤器时,判断一下SecurityContext上下文中身份认证信息是否为null,如果为null,则创建一个匿名的身份认证信息并放到SecurityContext上下文环境中。

代码分析

请求经过AnonymousAuthenticationFilter时,当SecurityContext上下文中身份认证信息是否为null时,则创建匿名的身份认证信息,由3个主要字段组从1. key是一个UUID 2.principal=“anonymousUser” 3.authorities包含一个ROLE_ANONYMOUS,代码如下:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        //上下文中身份认证信息为空,创建匿名的身份认证信息并设置到上下文认证环境中
        SecurityContextHolder.getContext().setAuthentication(
                createAuthentication((HttpServletRequest) req));
		...
    }
	...
    chain.doFilter(req, res);
}

protected Authentication createAuthentication(HttpServletRequest request) {
    //1. key是一个UUID 2.principal="anonymousUser" 3.authorities包含一个ROLE_ANONYMOUS
    AnonymousAuthenticationToken auth = new AnonymousAuthenticationToken(key,
            principal, authorities);
    //WebAuthenticationDetailsSource是WebAuthenticationDetailsSource实例,buildDetails()也十分简单,获取了request的remoteAddress以及sessionId
    auth.setDetails(authenticationDetailsSource.buildDetails(request));

    return auth;
}
2025-06-10 14:49:19,740 INFO Root WebApplicationContext: initialization completed in 2972 ms 2025-06-10 14:49:25,224 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@f73dcd6, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5c87bfe2, org.springframework.security.web.context.SecurityContextPersistenceFilter@4879f0f2, org.springframework.security.web.header.HeaderWriterFilter@75f5fd58, org.springframework.security.web.csrf.CsrfFilter@7446d8d5, org.springframework.security.web.authentication.logout.LogoutFilter@1fdfafd2, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@354fc8f0, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4678a2eb, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2fea7088, org.springframework.security.web.session.SessionManagementFilter@306851c7, org.springframework.security.web.access.ExceptionTranslationFilter@4fbda97b] 2025-06-10 14:49:25,510 INFO Adding welcome page: class path resource [static/index.html] 2025-06-10 14:49:25,850 INFO Exposing 1 endpoint(s) beneath base path '/actuator' 2025-06-10 14:49:25,868 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 14:49:25,869 INFO Will not secure Ant [pattern='/**'] 2025-06-10 14:49:25,869 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 14:49:25,870 INFO Will not secure Mvc [pattern='/prometheus'] 2025-06-10 14:49:25,870 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 14:49:25,871 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}'] 2025-06-10 14:49:25,871 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 14:49:25,871 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}'] 2025-06-10 14:49:25,941 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos' 2025-06-10 14:49:25,956 INFO No TaskScheduler/ScheduledExecutorService bean found for scheduled processing 2025-06-10 14:49:25,972 INFO Nacos started successfully in stand alone mode. use embedded storage 该启动日志是否开启鉴权
06-11
2025-07-17 10:42:54,903 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register 2025-07-17 10:42:54,904 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register 2025-07-17 10:43:01,400 INFO Initializing ExecutorService 'applicationTaskExecutor' 2025-07-17 10:43:01,683 INFO Adding welcome page: class path resource [static/index.html] 2025-07-17 10:43:02,355 INFO Creating filter chain: Ant [pattern='/**'], [] 2025-07-17 10:43:02,412 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7493d937, org.springframework.security.web.context.SecurityContextPersistenceFilter@2262f0d8, org.springframework.security.web.header.HeaderWriterFilter@1f172892, org.springframework.security.web.csrf.CsrfFilter@223967ea, org.springframework.security.web.authentication.logout.LogoutFilter@c497a55, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@59e082f8, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@656ec00d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5dc7841c, org.springframework.security.web.session.SessionManagementFilter@2bdb5e0f, org.springframework.security.web.access.ExceptionTranslationFilter@719c1faf] 2025-07-17 10:43:02,647 INFO Initializing ExecutorService 'taskScheduler' 2025-07-17 10:43:02,701 INFO Exposing 16 endpoint(s) beneath base path '/actuator' 2025-07-17 10:43:02,986 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos' 2025-07-17 10:43:02,997 INFO Nacos started successfully in stand alone mode. use embedded storage
07-18
2025-06-10 15:03:45,131 INFO Root WebApplicationContext: initialization completed in 2983 ms 2025-06-10 15:03:50,852 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@58ebfd03, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5b07730f, org.springframework.security.web.context.SecurityContextPersistenceFilter@5c3b6c6e, org.springframework.security.web.header.HeaderWriterFilter@502f1f4c, org.springframework.security.web.csrf.CsrfFilter@a8e6492, org.springframework.security.web.authentication.logout.LogoutFilter@41813449, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@75f5fd58, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@306851c7, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@1fdfafd2, org.springframework.security.web.session.SessionManagementFilter@75c9e76b, org.springframework.security.web.access.ExceptionTranslationFilter@27305e6] 2025-06-10 15:03:51,269 INFO Adding welcome page: class path resource [static/index.html] 2025-06-10 15:03:51,758 INFO Exposing 1 endpoint(s) beneath base path '/actuator' 2025-06-10 15:03:51,790 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 15:03:51,791 INFO Will not secure Ant [pattern='/**'] 2025-06-10 15:03:51,792 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 15:03:51,794 INFO Will not secure Mvc [pattern='/prometheus'] 2025-06-10 15:03:51,796 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 15:03:51,796 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}'] 2025-06-10 15:03:51,797 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead. 2025-06-10 15:03:51,798 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}'] 2025-06-10 15:03:51,876 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos' 2025-06-10 15:03:51,897 INFO No TaskScheduler/ScheduledExecutorService bean found for scheduled processing 2025-06-10 15:03:51,920 INFO Nacos started successfully in stand alone mode. use embedded storage 这是未设置nacos.core.auth.enabled=false 的nacos启动日志
06-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值