SpringSecurity以及SSO

本文介绍了SpringSecurity的DelegatingFilterProxy和WebSecurityConfigurerAdapter的使用,以及如何集成CAS实现SSO。详细讲解了四种常见的鉴权方式,包括MspCasAuthenticationFilter、PseudoCasAuthenticationFilter、SystemSsoAuthenticationFilter和OAuth2ClientAuthenticationProcessingFilter的工作原理。同时,探讨了SpringSecurity与CAS的交互过程,如CasAuthenticationEntryPoint的登录重定向和TGT、ST的生命周期管理。
摘要由CSDN通过智能技术生成

一、DelegatingFilterProxy

  • 提示:
    DelegatingFilterProxy extends GenericFilterBean

​ GenericFilterBean implements Filter, BeanNameAware, EnvironmentAware,
​ EnvironmentCapable, ServletContextAware, InitializingBean, DisposableBean

​ GenericFilterBean 是 Servlet-api 中filter的子类 并且实现了 InitializingBean

在这里插入图片描述

  • GenericFilterBean
    //这个是	GenericFilterBean  的内部方法  
    @Override
	public void afterPropertiesSet() throws ServletException {
   
		initFilterBean();
	}
    /** 
     * 这个使用模板设计模式   交给子类实现 而 DelegatingFilterProxy  正是其子类
     */
	protected void initFilterBean() throws ServletException {
   
	}
  • DelegatingFilterProxy
	@Override
	protected void initFilterBean() throws ServletException {
   
		synchronized (this.delegateMonitor) {
   
			if (this.delegate == null) {
   
				// If no target bean name specified, use filter name.
				if (this.targetBeanName == null) {
   
                    //这个getFilterName 可以看一下下面的代码块
					this.targetBeanName = getFilterName();
                    
				}
				// Fetch Spring root application context and initialize the delegate early,
				// if possible. If the root application context will be started after this
				// filter proxy, we'll have to resort to lazy initialization.
				WebApplicationContext wac = findWebApplicationContext();
				if (wac != null) {
   
					this.delegate = initDelegate(wac);
				}
			}
		}
	}

	
	protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
   
		String targetBeanName = getTargetBeanName();
		Assert.state(targetBeanName != null, "No target bean name set");
        //这个targetBeanName 的具体值 取决于使用的安全框架 我们这里说的是SpringSecurity 默认的配置是						  		  springSecurityFilterChain
        //从Spring容器中 取出Filter的实现
		Filter delegate = wac.getBean(targetBeanName, Filter.class);
		if (isTargetFilterLifecycle()) {
   
			delegate.init(getFilterConfig());
		}
		return delegate;
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
   
		//DCL来初始化delegate
		// Lazily initialize the delegate if necessary.
		Filter delegateToUse = this.delegate;
		if (delegateToUse == null) {
   
			synchronized (this.delegateMonitor) {
   
				delegateToUse = this.delegate;
				if (delegateToUse == null) {
   
					WebApplicationContext wac = findWebApplicationContext();
					if (wac == null) {
   
						throw new IllegalStateException("No WebApplicationContext found: " +
								"no ContextLoaderListener or DispatcherServlet registered?");
					}
					delegateToUse = initDelegate(wac);
				}
				this.delegate = delegateToUse;
			}
		}
		//这个地方就是代理了 DelegatingFilterProxy 代理Filter ,Filter的实现类已经交给了Spring容器管理,通过		                 DelegatingFilterProxy 来将Filter产生关联。
		// Let the delegate perform the actual doFilter operation.
		invokeDelegate(delegateToUse, request, response, filterChain);
	}
protected void invokeDelegate(
		Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {
   

	delegate.doFilter(request, response, filterChain);
}
	//	这个getFilterName 的代码块
	@Nullable
	protected String getFilterName() {
   
        //意思就是有配置从配置中取没有配置就用beanName
        //其实SpringSecurity就是用的BeanName   稍后我们贴代码
		return (this.filterConfig != null ? this.filterConfig.getFilterName() : this.beanName);
	}

到这我们只要理解
DelegatingFilterProxy是 SpringWeb模块的一个代理,为了能够更好的解耦以便于集成非Spring Security的其他安全框架,并且将由Spring容器管理的Filter实现类给代理了。

二、SpringSecurity

1、 WebSecurityConfigurerAdapter
我们通过自定义类来实现WebSecurityConfigurerAdapter 进行拓展 如: WebSecurityConfigurerImpl extends WebSecurityConfigurerAdapter。并 通过 @EnableWebSecurity 来启用SpringSecurity
跟进该注解我们可以找到 WebSecurityConfiguration见下面代码块:

  • WebSecurityConfiguration
	
	public static final String DEFAULT_FILTER_NAME = "springSecurityFilterChain";
	
	//注意这个地方使用@Bean注解注入了springSecurityFilterChain 这个bean 该Bean的实现类是FilterChainProxy
	//这样 在一 中我们在Spring容器中获取springSecurityFilterChain这个bean的时候就得到了FilterChainProxy的实例。
	@Bean(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME)
	public Filter springSecurityFilterChain() throws Exception {
   
		boolean hasConfigurers = this.webSecurityConfigurers != null && !this.webSecurityConfigurers.isEmpty();
		boolean hasFilterChain = !this.securityFilterChains.isEmpty();
		Assert.state(!(hasConfigurers && hasFilterChain),
				"Found WebSecurityConfigurerAdapter as well as SecurityFilterChain. Please select just one.");
		if (!hasConfigurers && 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security可以与SSO(Single Sign-On,单点登录)集成,以实现在多个应用程序中共享用户身份认证信息的功能。 整合SSO的思路是通过使用安全令牌(如JWT)来生成和验证用户身份认证信息,并在多个应用程序之间传递这些令牌。以下是实现此功能的一般步骤: 1. 配置SSO服务器:首先,您需要设置一个单独的SSO服务器,用于处理用户的身份认证和授权。这个服务器将负责生成和验证令牌,并提供一个API供其他应用程序进行身份认证。 2. 配置应用程序:接下来,在每个需要集成SSO的应用程序中,您需要配置Spring Security以使用SSO服务器进行身份认证。这通常涉及到配置Spring Security过滤器链,以便在用户登录时将身份认证请求转发到SSO服务器,并在成功认证后接收和解析令牌。 3. 配置认证提供者:您还需要编写一个自定义的认证提供者来处理从SSO服务器接收到的令牌,并将用户的角色信息添加到Spring Security的认证对象中。这可以通过实现`AuthenticationProvider`接口并覆盖`authenticate`和`supports`方法来实现。在`authenticate`方法中,您可以解析令牌并从SSO服务器获取用户的角色信息,然后将这些角色信息添加到Spring Security的认证对象中。 4. 配置安全规则:最后,您需要配置每个应用程序的安全规则,以限制对受保护资源的访问。这可以通过使用`@PreAuthorize`注解或在配置文件中定义的安全规则来实现。 这些是整合Spring SecuritySSO的一般步骤。具体实现的代码细节会因您所使用的SSO服务器和具体需求而有所不同。您可以参考和中的示例代码来了解更多细节,并根据您的具体需求进行适当修改。 : spring security 整合sso全记录 : spring boot整合spring security 实现SSO单点登陆 完整DEMO.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值