# spring-ss **Repository Path**: Terry_/spring-ss ## Basic Information - **Project Name**: spring-ss - **Description**: 学习spring secret - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-28 - **Last Updated**: 2021-09-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring security ## 常见内建过滤器 > BasicAuthenticationFilter 如果在请求中找到一个Basic Auth Http 头,如果找到,则尝试用该头中的用户名和密码验证用户 > UsernamePasswordAuthenticationFilter 如果在请求参数或者POST 的Request Body 中找到用户名/密码,则尝试用这些值对用户进行身份验证 > DefaultLoginPageGeneratingFilter 默认登录页面生成过滤器。用于生成一个登录页面,如果你没有明确的禁用这个功能,那么就会生成一个登录页面。这就是为什么在启用Spring Security时,会得到一个默认登录页面的原因 > DefaultLogoutPageGenerationFilter 如果没用禁用该功能,则会生成一个注消页面 > FilterSecurityInterceptor 过滤安全拦截器,用于授权逻辑 ## SecurityConfig 一般配置 基础配置,继承WebSecurityConfigurerAdapter,添加注解@EnableWebSecurity ```java @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests(req -> req.antMatchers("/hello").authenticated()) .formLogin(form -> form.disable()) .httpBasic(Customizer.withDefaults()) .csrf(csrf -> csrf.disable()); } } ``` ### 如下,以and 的方式 ```java @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").hasRole("USER") .anyRequest().authenticated() // 重新返回一个httpSecurity .and() .formLogin().loginPage("/login").usernameParameter("username1") // 就可以不断的链接起来 .and() .httpBasic().realmName("BA"); } ``` ### 函数式的风格 ```java @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests(req -> req.antMatchers("/hello").authenticated()) .formLogin(form -> form.disable()) .httpBasic(Customizer.withDefaults()) .csrf(csrf -> csrf.disable()); } ``` 为了方便调试和学习。可以打开Debug @EnableWebSecurity(debug = true) 这样就能打印出很多调试信息,如下 ```java ******************************************************************** ********** Security debugging is enabled. ************* ********** This may include sensitive information. ************* ********** Do not use in a production system! ************* ******************************************************************** ``` 当然,Debug 级别的日志。需要在我们的配置文件里把Debug日志级别打开 logging: level: org.springframework.security: DEBUG 当使用默认配置时,系统会为我们生成默认帐户与密码,控制台上会以下打印 Using generated security password: fbbf345c-6fa3-4510-83e8-a9b90de7b4ea 为了测试方便,我们可以在配置文件中,加入以下配置 ```yaml spring: application: name: ss security: user: name: user password: 12345678 roles: USER,ADMIN ``` 对于静态资源,我们可以不启用websecurity 过滤器链,以降底服务消耗 ```java //配置真正路径的安全性过滤链 @Override public void configure(WebSecurity web) throws Exception { //public 路径,不启用安全过滤器链。(静态文件) web.ignoring().mvcMatchers("/public/**"); } ``` ### 核心组件 SecurityContext 存储当前认让的用户的详细信息 SecurityContextHolder 工具类,提供了对安全上下文的访问。默认情况下,它使用一个ThreadLocal 对象来存储安全上下文,这意味着它是线程安全的 Authentication * 存储了当前用户(与应用程序义互的主体)的详细信息 * Principal 可以理解为用户的信息(比较简单的情况下,有可能是用户名) * Credentials 可以理解为密码(凭证) * Authorities 可以理解为权限 ### 密码 #### 编码格式和密码迁移 DelegatingPasswordEncoder 以{id}encodedPassword形式存储 密码匹配:encoder.matches(CharSequence String) 密码升级:实现UserDetailsPasswordService中 updatePassword