SpringSecurity

1、SpringSecurity

SpringSecurity本质上是一个基于ServletFilter实现的过滤器链框架,当容器创建时相关的过滤器都跟着被创建,其中常用的过滤器有下面几个

  1. org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter         
  2. org.springframework.security.web.header.HeaderWriterFilter        
  3. org.springframework.security.web.csrf.CsrfFilter        
  4. org.springframework.security.web.authentication.logout.LogoutFilter        
  5. org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter     
  6. org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter        
  7. org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter       
  8. org.springframework.security.web.savedrequest.RequestCacheAwareFilter        
  9. org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter       
  10. org.springframework.security.web.authentication.AnonymousAuthenticationFilter        
  11. org.springframework.security.web.session.SessionManagementFilter        
  12. org.springframework.security.web.access.ExceptionTranslationFilter        
  13. org.springframework.security.web.access.intercept.FilterSecurityInterceptor

具体功能讲解可参考文章

2、过滤器的加载过程

  1. DelegatingFilterProxy被SpringBoot加载,相当于web.xml的内容被执行
  2. Delegating.initFilterBean():初始化方法,该方法获取了FilterChainProxy
  3. Delegating.doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain):对FilterChainProxy进行操作
  4. FilterChainProxy.doFilter(request,response,filterChain):创建新chain对象并由其执行SpringSecurity的过滤器集合

3、相关接口

1、UserDetailsService

当什么也没有配置的时候,账号和密码是由SpringSecurity定义的。而在实际开发中,该定义会被业务逻辑和数据库重写。基于此有了UserDetailsService接口,如下

package org.springframework.security.core.userdetails;

public interface UserDetailsService {
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}

2、PasswordEncoder

在实际开发中需要在配置中开启密码加密,而PasswordEncoder就是一个加密类的实现接口,在配置类中定义即可,如下

package org.springframework.security.crypto.password;

public interface PasswordEncoder {
    String encode(CharSequence rawPassword);

    boolean matches(CharSequence rawPassword, String encodedPassword);

    default boolean upgradeEncoding(String encodedPassword) {
        return false;
    }
}

3、基于上述两个接口实现一个自定义类

第一步创建配置类,设置使用哪个UserDetailsService类

第二步编写实现类,返回User对象,Use对象有用户名和操作权限

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值