WebSecurityConfigurerAdapter已废弃,官方推荐使用HttpSecurity 或WebSecurity。
都继承了SecurityBuilder
public interface SecurityBuilderO> {
O build() throws Exception;
}
亮点:通过这种方式很容易知道知道自己构建的Object
HttpSecurity
public interface SecurityFilterChain {
/**
** 判断请求是否能够匹配上这些过滤器
**/
boolean matches(HttpServletRequest request);
/**
** 过滤器列表
**/
ListFilter> getFilters();
}
public abstract class AbstractSecurityBuilderO> implements SecurityBuilderO> {
private AtomicBoolean building = new AtomicBoolean();
private O object;
/**
* 防止重复构建
**/
@Override
public final O build() throws Exception {
if (this.building.compareAndSet(false, true)) {
this.object = doBuild();
return this.object;
}
throw new AlreadyBuiltException("This object has already been built");
}
/**
* Gets the object that was built. If it has not been built yet an Exception is
* thrown.
* @return the Object that was built
*/
public final O getObject() {
if (!this.building.get()) {
throw new IllegalStateException("This object has not been built");
}
return this.object;
}
/**
* 给子类自己实现
*/
protected abstract O doBuild() throws Exception;
}
@Override
protected final O doBuild() throws Exception {
synchronized (this.configurers) {
this.buildState = BuildState.INITIALIZING;
beforeInit();
init();
this.buildState = BuildState.CONFIGURING;
beforeConfigure();
configure();
this.buildState = BuildState.BUILDING;
O result = performBuild();
this.buildState = BuildState.BUILT;
return result;
}
}
protected void beforeInit() throws Exception {
}
protected void beforeConfigure() throws Exception {
}
/**
* 重要方法
**/
protected abstract O performBuild() throws Exception;
@SuppressWarnings("unchecked")
@Override
protected DefaultSecurityFilterChain performBuild() {
......
return new DefaultSecurityFilterChain(this.requestMatcher, sortedFilters);
}
@Override
protected Filter performBuild() throws Exception {
......
FilterChainProxy filterChainProxy = new FilterChainProxy(securityFilterChains);
......
Filter result = filterChainProxy;
if (this.debugEnabled) {
.....
result = new DebugFilter(filterChainProxy);
}
return result;
}
可以通过@EnableWebSecurity(debug = true)开启debug模式
new DebugFilter(filterChainProxy)对filterChainProxy进行装饰
public FilterChainProxy(ListSecurityFilterChain> filterChains) {
this.filterChains = filterChains;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
......
doFilterInternal(request, response, chain);
......
}
private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
FirewalledRequest firewallRequest = this.firewall.getFirewalledRequest((HttpServletRequest) request);
ListFilter> filters = getFilters(firewallRequest);
......
// 虚拟过滤器,挨个应用filters
VirtualFilterChain virtualFilterChain = new VirtualFilterChain(firewallRequest, chain, filters);
virtualFilterChain.doFilter(firewallRequest, firewallResponse);
}
// 获取匹配的filter
private ListFilter> getFilters(HttpServletRequest request) {
int count = 0;
for (SecurityFilterChain chain : this.filterChains) {
......
if (chain.matches(request)) {
return chain.getFilters();
}
}
return null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
if (this.currentPosition == this.size) {
if (logger.isDebugEnabled()) {
logger.debug(LogMessage.of(() -> "Secured " + requestLine(this.firewalledRequest)));
}
// Deactivate path stripping as we exit the security filter chain
this.firewalledRequest.reset();
this.originalChain.doFilter(request, response);
return;
}
this.currentPosition++;
Filter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
if (logger.isTraceEnabled()) {
logger.trace(LogMessage.format("Invoking %s (%d/%d)", nextFilter.getClass().getSimpleName(),
this.currentPosition, this.size));
}
nextFilter.doFilter(request, response, this);
}
AbstractConfiguredSecurityBuilder.class
private final LinkedHashMapClass? extends SecurityConfigurerO, B>>, ListSecurityConfigurerO, B>>> configurers = new LinkedHashMap>();
private final ListSecurityConfigurerO, B>> configurersAddedInInitializing = new ArrayList>();
private final MapClass?>, Object> sharedObjects = new HashMap>();
private final boolean allowConfigurersOfSameType;
// 构建状态
private BuildState buildState = BuildState.UNBUILT;
// 后置处理
private ObjectPostProcessorObject> objectPostProcessor;
private C extends SecurityConfigurerO, B>> void add(C configurer) {
Assert.notNull(configurer, "configurer cannot be null");
Class? extends SecurityConfigurerO, B>> clazz = (Class? extends SecurityConfigurerO, B>>) configurer
.getClass();
synchronized (this.configurers) {
if (this.buildState.isConfigured()) {
throw new IllegalStateException("Cannot apply " + configurer + " to already built object");
}
ListSecurityConfigurerO, B>> configs = null;
if (this.allowConfigurersOfSameType) {
configs = this.configurers.get(clazz);
}
configs = (configs != null) ? configs : new ArrayList>(1);
configs.add(configurer);
this.configurers.put(clazz, configs);
if (this.buildState.isInitializing()) {
this.configurersAddedInInitializing.add(configurer);
}
}
}
/**
* 由于配置一个SecurityBuilder
* Allows for configuring a {@link SecurityBuilder}. All {@link SecurityConfigurer} first
* have their {@link #init(SecurityBuilder)} method invoked. After all
* {@link #init(SecurityBuilder)} methods have been invoked, each
* {@link #configure(SecurityBuilder)} method is invoked.
*
* @param The object being built by the {@link SecurityBuilder} B
* @param The {@link SecurityBuilder} that builds objects of type O. This is also the
* {@link SecurityBuilder} that is being configured.
* @author Rob Winch
* @see AbstractConfiguredSecurityBuilder
*/
public interface SecurityConfigurerO, B extends SecurityBuilderO>> {
/**
* 设置一些共享状态,而不是属性
* Initialize the {@link SecurityBuilder}. Here only shared state should be created
* and modified, but not properties on the {@link SecurityBuilder} used for building
* the object. This ensures that the {@link #configure(SecurityBuilder)} method uses
* the correct shared objects when building. Configurers should be applied here.
* @param builder
* @throws Exception
*/
void init(B builder) throws Exception;
/**
* Configure the {@link SecurityBuilder} by setting the necessary properties on the
* {@link SecurityBuilder}.
* @param builder
* @throws Exception
*/
void configure(B builder) throws Exception;
}
private enum BuildState {
/**
* This is the state befor服务器托管网e the {@link Builder#build()} is invoked
*/
UNBUILT(0服务器托管网),
/**
* The state from when {@link Builder#build()} is first invoked until all the
* {@link SecurityConfigurer#init(SecurityBuilder)} methods have been invoked.
*/
INITIALIZING(1),
/**
* The state from after all {@link SecurityConfigurer#init(SecurityBuilder)} have
* been invoked until after all the
* {@link SecurityConfigurer#configure(SecurityBuilder)} methods have been
* invoked.
*/
CONFIGURING(2),
/**
* From the point after all the
* {@link SecurityConfigurer#configure(SecurityBuilder)} have completed to just
* after {@link AbstractConfiguredSecurityBuilder#performBuild()}.
*/
BUILDING(3),
/**
* After the object has been completely built.
*/
BUILT(4);
private final int order;
BuildState(int order) {
this.order = order;
}
public boolean isInitializing() {
return INITIALIZING.order == this.order;
}
/**
* Determines if the state is CONFIGURING or later
* @return
*/
public boolean isConfigured() {
return this.order >= CONFIGURING.order;
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
摘要 当一组自利的用户在分布式系统中共享多个资源时,我们面临资源分配问题,即所谓的负载均衡问题。特别地,负载均衡被定义为将负载分配到分布式系统的服务器上,以便最小化作业响应时间并提高服务器的利用率。在本文中,研究了一个由有限数量的服务器和有限数量的用户组成的分…