zoukankan      html  css  js  c++  java
  • springsecurity 源码解读之 AnonymousAuthenticationFilter

    我们知道springsecutity 是通过一系列的 过滤器实现的,我们可以看看这系列的过滤器到底长成什么样子呢?

    一堆过滤器,这个过滤器的设计设计上是 责任链设计模式。

    这里我们可以看到有一个 AnonymousAuthenticationFilter 过滤器。

    顾名思义我们知道这个是一个叫 匿名登录人过滤器,他的作用是什么呢?

    我们看看代码 ,他做了什么?

    if (applyAnonymousForThisRequest((HttpServletRequest) req)) {
                if (SecurityContextHolder.getContext().getAuthentication() == null) {
                    SecurityContextHolder.getContext().setAuthentication(createAuthentication((HttpServletRequest) req));
    
                    if (logger.isDebugEnabled()) {
                        logger.debug("Populated SecurityContextHolder with anonymous token: '"
                            + SecurityContextHolder.getContext().getAuthentication() + "'");
                    }
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("SecurityContextHolder not populated with anonymous token, as it already contained: '"
                            + SecurityContextHolder.getContext().getAuthentication() + "'");
                    }
                }
            }

    很简单,当他发现没有登录的时候,手工设置了一个匿名的登录人Authentication。

    我们可以在其后的过滤器中,如果没有登录时,我们可以知道当前访问的是匿名用户。

    我们可以通过如下代码获取当前人是匿名用户。

    Authentication auth= SecurityContextHolder.getContext().getAuthentication();
    if (auth==null || "anonymousUser".equals(auth.getPrincipal().toString())) {
  • 相关阅读:
    4.8日学习
    Apache安装
    HTML5 review
    个人阅读作业LAST
    个人阅读作业Week7
    结对编程:界面模块总结
    个人博客作业Week3
    结对编程博客
    个人博客week2
    软工第一次作业简单总结
  • 原文地址:https://www.cnblogs.com/yg_zhang/p/10652245.html
Copyright © 2011-2022 走看看