Spring Boot Security提供了:
Spring Boot Security架构
是基于Servlet中的Filter实现的。整个Security是通过过滤器链(FilterChain)来完成认知、授权、攻击保护。
Authentication
,通过AuthenticationProvider
来完成用户资源权限UserDetails
SecurityMetadataSource
,一般是URL等信息AuthencationManager
来获得Authentication
,如果已存在则从上下文获取。AccessDecisionManger
来决策,是否通过。FilterSecurityInterceptor
实现了Filter
。vote
投票决定是否通过。RequestMatcher
校验当前请求是否需要执行当前过滤器。建议登录地址(/login)AuthenticationManagerResolver
获取AuthenticationManager
AuthenticationManger
获取Authentication
。完成用户认证和获取用户信息AuthenticationFailureHandler
返回失败信息AuthenticationSuccessHandler
返回成功信息SecurityContext
获取上下文存储的Authentication
信息。LogoutHandler
中的logout
完成,退出逻辑处理。LogoutSuccessHandler
中onLogoutSuccess
方法。完成退出成功后的处理doFilter
完成校验请求链接是否是:loginError
,logoutSuccess
,isLoginUrlRequest
,跳转到指定链接中。且为GET
请求。/logout
,且为GET
请求Spring Boot AutoConfiguration项目下“META-INF/spring.factories"
提供了支持Servlet和Reactive两种Web类型的支持
#Security自动配置基于Servlet
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
#UserDetailsService自动配置基于Servlet
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
#安全过滤器自动配置基于Servlet
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
- 完成认证异常类,ApplicationEventPublisher
- 通过引入类:SpringBootWebSecurityConfiguration,WebSecurityEnablerConfiguration,SecurityDataConfiguration。
完成自动配置
@Configuration(proxyBeanMethods = false)
/**
* 认证异常类的注册
*/
@ConditionalOnClass(DefaultAuthenticationEventPublisher.class)
@EnableConfigurationProperties(SecurityProperties.class)
@Import({ SpringBootWebSecurityConfiguration.class, WebSecurityEnablerConfiguration.class,
SecurityDataConfiguration.class })
public class SecurityAutoConfiguration {
@Bean
@ConditionalOnMissingBean(AuthenticationEventPublisher.class)
public DefaultAuthenticationEventPublisher authenticationEventPublisher(ApplicationEventPublisher publisher) {
return new DefaultAuthenticationEventPublisher(publisher);
}
}
如果有一个bean类型是
WebSecurityConfigurationAdapter
, 添加·EnableWebSecurity·注解。主要将确保注解是存在的当为默认安全自动配置。而且如果用户添加了定制安全且忘记添加了当前注解。
如果EnableWebSecurity
已经添加了Bean或如果一个bean的名称是BeanIds#SPRING_SECURITY_FILTER_CHAIN
在当前用户的bean配置中,将不使用当前配置。
@Configuration(proxyBeanMethods = false)
//Web安全配置适配器
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class)
//当Web类型为Servlet时生效
@ConditionalOnWebApplication(type = Type.SERVLET)
public class SpringBootWebSecurityConfiguration {
@Configuration(proxyBeanMethods = false)
@Order(SecurityProperties.BASIC_AUTH_ORDER)
static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {
}
}
初始化,HttpSecurity前置构建行动
完成FilterSecurityInterceptor
,过滤安全拦截器。完成对访问决策资源的验证(一般是用户角色与URL的控制)
public void init(WebSecurity web) throws Exception {
HttpSecurity http = this.getHttp();
web.addSecurityFilterChainBuilder(http).postBuildAction(() -> {
FilterSecurityInterceptor securityInterceptor = (FilterSecurityInterceptor)http.getSharedObject(FilterSecurityInterceptor.class);
web.securityInterceptor(securityInterceptor);
});
}
获取HttpSecurity,在这个完成整个HttpSecurity的配置,包括登录、退出等。
protected final HttpSecurity getHttp() throws Exception {
if (this.http != null) {
return this.http;
} else {
AuthenticationEventPublisher eventPublisher = this.getAuthenticationEventPublisher();
this.localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);
AuthenticationManager authenticationManager = this.authenticationManager();
this.authenticationBuilder.parentAuthenticationManager(authenticationManager);
Map<Class<?>, Object> sharedObjects = this.createSharedObjects();
this.http = new HttpSecurity(this.objectPostProcessor, this.authenticationBuilder, sharedObjects);
if (!this.disableDefaults) {
((HttpSecurity)((DefaultLoginPageConfigurer)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)
this.http.csrf().and())//跨站点请求伪造
.addFilter(new WebAsyncManagerIntegrationFilter())//Web异步管理拦截器过滤
.exceptionHandling().and()) //异常类过滤器设置
.headers().and()) //头过滤器设置
.sessionManagement().and()) //会话管理
.securityContext().and())//安全上下文
.requestCache().and())//请求缓存
.anonymous().and())//匿名
.servletApi().and())//api
.apply(new DefaultLoginPageConfigurer())).and())//登录配置
.logout();//退出登录
ClassLoader classLoader = this.context.getClassLoader();
List<AbstractHttpConfigurer> defaultHttpConfigurers = SpringFactoriesLoader.loadFactories(AbstractHttpConfigurer.class, classLoader);
Iterator var6 = defaultHttpConfigurers.iterator();
while(var6.hasNext()) {
AbstractHttpConfigurer configurer = (AbstractHttpConfigurer)var6.next();
this.http.apply(configurer);
}
}
this.configure(this.http);
return this.http;
}
}
HttpSecurity安全配置
- 通过formLogin方法完成FormLoginConfigurer的配置,其中其中包含
UsernamePasswordAuthenticationFilter
(这个类完成身份认证),设置了DefaultLoginPageGeneratingFilter(登录页面过滤器)
protected void configure(HttpSecurity http) throws Exception {
this.logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");
((HttpSecurity)((HttpSecurity)((ExpressionUrlAuthorizationConfigurer.AuthorizedUrl)
http.authorizeRequests().anyRequest()).authenticated().and()).formLogin().and()).httpBasic();
}
在所有Bean中未找到下列Bean则,加载当前配置
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(AuthenticationManager.class)
@ConditionalOnBean(ObjectPostProcessor.class)
@ConditionalOnMissingBean(
value = { AuthenticationManager.class, AuthenticationProvider.class, UserDetailsService.class },
type = { "org.springframework.security.oauth2.jwt.JwtDecoder",
"org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector" })
public class UserDetailsServiceAutoConfiguration {
private static final String NOOP_PASSWORD_PREFIX = "{noop}";
private static final Pattern PASSWORD_ALGORITHM_PATTERN = Pattern.compile("^\\{.+}.*$");
private static final Log logger = LogFactory.getLog(UserDetailsServiceAutoConfiguration.class);
@Bean
@ConditionalOnMissingBean(
type = "org.springframework.security.oauth2.client.registration.ClientRegistrationRepository")
@Lazy
public InMemoryUserDetailsManager inMemoryUserDetailsManager(SecurityProperties properties,
ObjectProvider<PasswordEncoder> passwordEncoder) {
SecurityProperties.User user = properties.getUser();
List<String> roles = user.getRoles();
return new InMemoryUserDetailsManager(
User.withUsername(user.getName()).password(getOrDeducePassword(user, passwordEncoder.getIfAvailable()))
.roles(StringUtils.toStringArray(roles)).build());
}
private String getOrDeducePassword(SecurityProperties.User user, PasswordEncoder encoder) {
String password = user.getPassword();
if (user.isPasswordGenerated()) {
logger.info(String.format("%n%nUsing generated security password: %s%n", user.getPassword()));
}
if (encoder != null || PASSWORD_ALGORITHM_PATTERN.matcher(password).matches()) {
return password;
}
return NOOP_PASSWORD_PREFIX + password;
}
}
通过这个类,配置HttpSecurity。完成整个Filter链的构建。请求匹配,过滤比较.
配置示例
HttpSecurity http = new HttpSecurity(this.objectPostProcessor, this.authenticationBuilder, sharedObjects);
if (!this.disableDefaults) {
((HttpSecurity)((DefaultLoginPageConfigurer)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)((HttpSecurity)
http.csrf().and())//跨站点请求伪造
.addFilter(new WebAsyncManagerIntegrationFilter())//Web异步管理拦截器过滤
.exceptionHandling().and()) //异常类过滤器设置
.headers().and()) //头过滤器设置
.sessionManagement().and()) //会话管理
.securityContext().and())//安全上下文
.requestCache().and())//请求缓存
.anonymous().and())//匿名
.servletApi().and())//api
.apply(new DefaultLoginPageConfigurer())).and())//登录配置
.logout();//退出登录