Spring Security Config : HttpSecurity安全配置器 PortMapperConfigurer

本文详细介绍了Spring Security中PortMapper的配置与使用方法,包括其默认实现PortMapperImpl的功能,即在HTTP与HTTPS协议间进行端口跳转:如80到443,8080到8443。同时,解析了PortMapperConfigurer的内部工作原理,展示了如何通过HttpSecurity代码片段自定义PortMapper实例。

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

概述

介绍

并不做任何配置,只是创建一个共享对象 : PortMapper。缺省使用的PortMapper实现类是PortMapperImpl,用于在HTTPHTTPS两种协议的端口之间做跳转 : 80–> 443, 8080 --> 8443 。

继承关系

PortMapperConfigurer

使用


    // HttpSecurity 代码片段
	public PortMapperConfigurer<HttpSecurity> portMapper() throws Exception {
		return getOrApply(new PortMapperConfigurer<>());
	}

源代码

源代码版本 Spring Security Config 5.1.4.RELEASE

package org.springframework.security.config.annotation.web.configurers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.PortMapper;
import org.springframework.security.web.PortMapperImpl;


public final class PortMapperConfigurer<H extends HttpSecurityBuilder<H>> extends
		AbstractHttpConfigurer<PortMapperConfigurer<H>, H> {
	private PortMapper portMapper;
	private Map<String, String> httpsPortMappings = new HashMap<>();

	/**
	 * Creates a new instance
	 */
	public PortMapperConfigurer() {
	}

	/**
	 * Allows specifying the PortMapper instance.
	 * @param portMapper
	 * @return the PortMapperConfigurer for further customizations
	 */
	public PortMapperConfigurer<H> portMapper(PortMapper portMapper) {
		this.portMapper = portMapper;
		return this;
	}

	/**
	 * Adds a port mapping
     * 返回一个 HttpPortMapping 对象,用于往当前配置器的 httpsPortMappings
     * 属性中添加一项
	 * @param httpPort the HTTP port that maps to a specific HTTPS port.
	 * @return HttpPortMapping to define the HTTPS port
	 */
	public HttpPortMapping http(int httpPort) {
		return new HttpPortMapping(httpPort);
	}

	@Override
	public void init(H http) throws Exception {
       // 使用 #getPortMapper 获取 PortMapper 对象,并设置为共享对象
		http.setSharedObject(PortMapper.class, getPortMapper());
	}

	/**
	 * Gets the PortMapper to use. If #portMapper(PortMapper) was not
	 * invoked, builds a PortMapperImpl using the port mappings specified with
	 *  #http(int).
	 *
	 * @return the PortMapper to use
	 */
	private PortMapper getPortMapper() {
      // 如果调用者设置了 PortMapper 则使用设置的值,否则使用缺省值,一个新的
      // PortMapperImpl 对象,并应用属性 httpsPortMappings
		if (portMapper == null) {
			PortMapperImpl portMapper = new PortMapperImpl();
			portMapper.setPortMappings(httpsPortMappings);
			this.portMapper = portMapper;
		}
		return portMapper;
	}

	/**
	 * Allows specifying the HTTPS port for a given HTTP port when redirecting between
	 * HTTP and HTTPS.
	 *
	 * @author Rob Winch
	 * @since 3.2
	 */
	public final class HttpPortMapping {
		private final int httpPort;

		/**
		 * Creates a new instance
		 * @param httpPort
		 * @see PortMapperConfigurer#http(int)
		 */
		private HttpPortMapping(int httpPort) {
			this.httpPort = httpPort;
		}

		/**
		 * Maps the given HTTP port to the provided HTTPS port and vice versa.
		 * @param httpsPort the HTTPS port to map to
		 * @return the PortMapperConfigurer for further customization
		 */
		public PortMapperConfigurer<H> mapsTo(int httpsPort) {
			httpsPortMappings.put(String.valueOf(httpPort), String.valueOf(httpsPort));
			return PortMapperConfigurer.this;
		}
	}
}

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值